######################################################
##                                                  ##
##  !!!! Autogenerated YAML file, do not edit !!!!  ##
##                                                  ##
##  Edit source in /src/github/workflows/ instead!  ##
##                                                  ##
######################################################
defaults:
  run:
    shell: bash
jobs:
  build:
    if: ${{ always() }}
    env:
      ARGS: --disable-executable-profiling --disable-library-profiling --enable-split-sections
      LINUX_ARGS: --enable-executable-static
      WIN64_ARGS: --flags=enable-cluster-counting
      MACOS_ARGS: --flags=enable-cluster-counting
    strategy:
      matrix:
        ghc-ver:
        - 9.2.1
        cabal-ver:
        - 3.6
        os:
        - windows-2022
        - macos-latest
        - ubuntu-20.04
      fail-fast: false
    runs-on: ${{ matrix.os }}
    needs: auto-cancel
    steps:
    - uses: actions/checkout@v2
      name: Check out the master branch
    - run: |
        sha=`echo $(git rev-parse --short=7 HEAD)`
        nightly=`echo Agda-nightly`

        echo ::set-output name=sha::${sha}
        echo ::set-output name=nightly::${nightly}

        if [[ "$OSTYPE" == "msys"* ]]; then
          echo "::set-output name=args::${ARGS} ${WIN64_ARGS}"
          filename="${nightly}-win64.zip"
          echo ::set-output name=exe::agda.exe
          echo ::set-output name=filename::${filename}
          echo ::set-output name=content-type::application/zip
          echo "::set-output name=compress-cmd::7z a ${filename} ${nightly} -bb -mx=9"
        elif [[ "$OSTYPE" == "darwin"* ]]; then
          echo "::set-output name=args::${ARGS} ${MACOS_ARGS}"
          filename="${nightly}-macOS.tar.xz"
          echo ::set-output name=exe::agda
          echo "::set-output name=filename::${filename}"
          echo "::set-output name=content-type::application/x-xz"
          echo "::set-output name=compress-cmd::tar -a -cvf ${filename} ${nightly}"
        elif [[ "$OSTYPE" == "linux-gnu"* ]]; then
          echo "::set-output name=args::${ARGS} ${MACOS_ARGS}"
          filename="${nightly}-linux.tar.xz"
          echo ::set-output name=exe::agda
          echo "::set-output name=filename::${filename}"
          echo "::set-output name=content-type::application/x-xz"
          echo "::set-output name=compress-cmd::tar -a -cvf ${filename} ${nightly}"
          echo "::set-output name=args::${ARGS} ${LINUX_ARGS}"
        fi
      name: Set up platform-dependent variables
      id: vars
    - run: |
        echo nightly=${{ steps.vars.outputs.nightly }}
        echo compress-cmd=${{ steps.vars.outputs.compress-cmd }}
        echo filename=${{ steps.vars.outputs.filename }}
        echo content-type=${{ steps.vars.outputs.content-type }}
        echo args=${{ steps.vars.outputs.args }}
      name: Display build variables
    - with:
        cabal-version: ${{ matrix.cabal-ver }}
        ghc-version: ${{ matrix.ghc-ver }}
      uses: haskell/actions/setup@v1
      id: setup-haskell
    - if: ${{ runner.os == 'Windows' }}
      shell: pwsh
      run: |
        $env:PATH = "C:\msys64\mingw64\bin;C:\msys64\usr\bin;$origPath;$env:PATH"
        pacman -v --debug --noconfirm -Sy mingw-w64-x86_64-pkg-config mingw-w64-x86_64-icu
      name: Install the icu4c library (Windows)
    - run: |
        cabal configure ${{ steps.vars.outputs.args }}

        #    - uses: actions/cache@v2
        #      name: Cache dependencies
        #      id: cache
        #      with:
        #        path: |
        #          ${{ steps.setup-haskell.outputs.cabal-store }}
        #        # A unique cache is used for each stack.yaml.
        #        key: nightly-${{ runner.os }}-ghc-${{ matrix.ghc-ver }}-cabal-${{ hashFiles('**/plan.json') }}
        #
      name: Configure the build plan
    - if: ${{ !steps.cache.outputs.cache-hit }}
      run: cabal build exe:agda exe:agda-mode --only-dependencies -v0
      name: Build dependencies
    - with:
        command: cabal build exe:agda exe:agda-mode
        max_attempts: 3
        timeout_minutes: 60
        retry_on: error
      uses: nick-invision/retry@v2
      name: Build Agda
    - env:
        ICU_DIR: /usr/local/opt/icu4c/lib
        DLL: /c/msys64/mingw64/bin/libicu*.dll
        ICU_VER: '69'
      run: |
        mkdir -p ${{ steps.vars.outputs.nightly }}/bin
        cp -a src/data ${{ steps.vars.outputs.nightly }}

        if [[ "$OSTYPE" == "msys"* ]]; then
          find dist-newstyle/build \( -name 'agda.exe' -o -name 'agda-mode.exe' \) -type f -exec cp {} ${{ steps.vars.outputs.nightly }}/bin \;
          cp -a .github/*.bat ${{ steps.vars.outputs.nightly }}
          cp ${DLL} ${{ steps.vars.outputs.nightly }}/bin/
          C:/ProgramData/Chocolatey/bin/strip.exe ${{ steps.vars.outputs.nightly }}/bin/*
        else
          find dist-newstyle/build \( -name 'agda' -o -name 'agda-mode' \) -type f -exec cp {} ${{ steps.vars.outputs.nightly }}/bin \;
          strip ${{ steps.vars.outputs.nightly }}/bin/*
          cp -a .github/*.sh ${{ steps.vars.outputs.nightly }}
          if [[ "$OSTYPE" == "darwin"* ]]; then
          # Change the path to the dynamic library icu4c to the run-time search path:
          #
          # 1. the same directory of executable, i.e. @executable_path
          # 2. @executable_path/../lib
          # 3. the default location of system-wide icu4c installed by homebrew, ie. /usr/local/opt/icu4c/lib
          #
          mkdir ${{ steps.vars.outputs.nightly }}/lib
          cp ${ICU_DIR}/libicuuc.${ICU_VER}.dylib ${ICU_DIR}/libicui18n.${ICU_VER}.dylib ${ICU_DIR}/libicudata.${ICU_VER}.dylib ${{ steps.vars.outputs.nightly }}/lib
          install_name_tool -change ${ICU_DIR}/libicuuc.${ICU_VER}.dylib @rpath/libicuuc.${ICU_VER}.dylib ${{ steps.vars.outputs.nightly }}/bin/agda
          install_name_tool -change ${ICU_DIR}/libicui18n.${ICU_VER}.dylib @rpath/libicui18n.${ICU_VER}.dylib ${{ steps.vars.outputs.nightly }}/bin/agda
          install_name_tool -add_rpath @executable_path -add_rpath @executable_path/../lib -add_rpath ${ICU_DIR} ${{ steps.vars.outputs.nightly }}/bin/agda
          otool -L ${{ steps.vars.outputs.nightly }}/bin/agda
          fi
        fi
        file ${{ steps.vars.outputs.nightly }}/bin/agda
      name: Move artefacts to ${{ steps.vars.outputs.nightly }}
    - if: ${{ runner.os != 'macOS' }}
      with:
        strip: false
        file: ${{ steps.vars.outputs.nightly }}/bin/${{ steps.vars.outputs.exe }}
      uses: svenstaro/upx-action@v2
      name: Compress the Agda executable
    - run: |
        ${{ steps.vars.outputs.nightly }}/bin/agda --version
      name: Display the version information
    - run: |
        ${{ steps.vars.outputs.compress-cmd }}
      name: Pack artefacts
    - with:
        path: ${{ steps.vars.outputs.filename }}
        name: ${{ steps.vars.outputs.filename }}
        if-no-files-found: error
        retention-days: 3
      uses: actions/upload-artifact@v2
    outputs:
      sha: ${{ steps.vars.outputs.sha }}
  auto-cancel:
    if: ${{ github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion
      == 'success' }}
    runs-on: Ubuntu-latest
    steps:
    - with:
        access_token: ${{ github.token }}
      uses: styfle/cancel-workflow-action@0.6.0
  deploy:
    if: ${{ github.ref == 'refs/heads/master' && github.event_name == 'workflow_run'
      }}
    runs-on: ubuntu-latest
    needs: build
    steps:
    - uses: actions/checkout@v2
    - with:
        path: artifacts
      uses: actions/download-artifact@v2
    - with:
        tag_name: nightly
        delete_release: true
      env:
        GITHUB_TOKEN: ${{ github.token }}
      uses: dev-drprasad/delete-tag-and-release@v0.1.2
      name: Delete the nightly tag if any
    - env:
        GITHUB_TOKEN: ${{ github.token }}
      run: |
        assets=()
        for asset in artifacts/**/*; do
          assets+=("-a" "$asset")
        done
        hub release create "${assets[@]}" -m "Nightly Build (${{ needs.build.outputs.sha }}@master)" 'nightly'
      name: Create a nightly release and upload artifacts as release assets
name: Deploy
'on':
  workflow_dispatch: null
  workflow_run:
    branches:
    - master
    types:
    - completed
    workflows:
    - Build, Test, and Benchmark
