# Runs on any push to ci-linux-x64.
# Produces optimised static x64 linux  binaries,
# using the GHC version below (9.0.2) and cabal.
# Currently runs no tests.
# Currently using ghc 9.0 to avoid segfaults with the ghc 9.2 binaries on alpine,
# possibly https://gitlab.haskell.org/ghc/ghc/-/issues/20266

name: binaries-linux-x64-static

on:
  push:
    branches: [ binaries-linux-x64-static, binaries ]
    #tags:
    #  - '[0-9]+.[0-9]+'
    #  - '[0-9]+.[0-9]+-*'
    #  - '[0-9]+.[0-9]+.[0-9]+'
    #  - '[0-9]+.[0-9]+.[0-9]+-*'
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    container: alpine:edge
    steps:

    - name: Check out
      uses: actions/checkout@v3
      # have to fetch everything for git describe for --version
      with: 
        fetch-depth: 0  

    - name: Check embedded files
      run: |
        tools/checkembeddedfiles
      if: env.CONTINUE

    - name: Cache ghcup-installed tools
      id:   ghcup
      uses: actions/cache@v3
      with:
        path: ~/.ghcup
        key: ${{ runner.os }}-ghcup-${{ hashFiles('**.yaml') }}
        restore-keys: |
             ${{ runner.os }}-ghcup

    - name: Cache cabal-installed libs
      id:   cabal
      uses: actions/cache@v3
      with:
        path: ~/.cabal
        key: ${{ runner.os }}-cabal-${{ hashFiles('**.yaml') }}
        restore-keys: |
             ${{ runner.os }}-cabal

    - name: Install general tools with system package manager
      run: |
        apk --no-cache add binutils-gold curl gcc g++ git gmp-dev ncurses-dev ncurses-static libffi-dev make xz tar perl zlib-dev zlib-static

    - name: Add .ghcup/bin to PATH for following steps
      run: |
        echo "$HOME/.ghcup/bin/" >> $GITHUB_PATH

    - name: Install haskell tools with ghcup if needed
      # originally based on fossas/haskell-static-alpine
      run: |
        if [[ ! -x ~/.ghcup/bin/ghcup ]]; then
          mkdir -p ~/.ghcup/bin && curl https://downloads.haskell.org/~ghcup/x86_64-linux-ghcup > ~/.ghcup/bin/ghcup && chmod +x ~/.ghcup/bin/ghcup
        fi
        ghcup --version
        if [[ ! -x ~/.ghcup/bin/ghc-9.0.2 ]]; then
          ~/.ghcup/bin/ghcup install ghc 9.0.2 && ~/.ghcup/bin/ghcup set ghc 9.0.2
        fi
        ghc --version
        if [[ ! -x ~/.ghcup/bin/cabal-3.8.1.0 ]]; then
          ~/.ghcup/bin/ghcup install cabal 3.8.1.0 && ~/.ghcup/bin/ghcup set cabal 3.8.1.0
        fi
        cabal --version

    - name: Update cabal package index
      run: |
        cabal update

    - name: Build on alpine
      run: |
        cabal build --enable-executable-static hledger || (echo "ERROR: building hledger failed"; false)
        cabal build --enable-executable-static hledger-ui || (echo "ERROR: building hledger failed"; false)
        cabal build --enable-executable-static hledger-web || (echo "ERROR: building hledger-web failed"; false)

    - name: Gather binaries
      run: |
        mkdir tmp
        cp dist-newstyle/build/x86_64-linux/ghc-*/hledger-*/x/hledger/build/hledger/hledger tmp
        cp dist-newstyle/build/x86_64-linux/ghc-*/hledger-ui-*/x/hledger-ui/build/hledger-ui/hledger-ui tmp
        cp dist-newstyle/build/x86_64-linux/ghc-*/hledger-web-*/x/hledger-web/build/hledger-web/hledger-web tmp
        cd tmp
        strip hledger
        strip hledger-ui
        strip hledger-web
        tar cvf hledger-linux-x64.tar hledger hledger-ui hledger-web

    # upload-artifact loses execute permissions, so we tar the binaries to preserve them.
    # github UI always zips artifacts when they are downloaded, so we don't bother compressing the tar.
    # Unfortunately it means users must both unzip and untar.
    - name: Upload binaries
      uses: actions/upload-artifact@v3
      with:
        name: hledger-linux-x64
        path: tmp/hledger-linux-x64.tar
