name: Build and release

on:
  push:
    branches:
      - master
    tags:
      - 'v*'
  pull_request:
    branches:
      - master
  schedule:
    - cron: '0 2 * * *'

env:
  CABAL_CACHE_DISABLE: ${{ vars.CABAL_CACHE_DISABLE }}
  CABAL_CACHE_NONFATAL: yes

jobs:
  build-linux:
    name: Build linux binary
    runs-on: ${{ matrix.os }}
    env:
      CABAL_VER: 3.10.1.0
      JSON_VERSION: "0.0.7"
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      S3_HOST: ${{ secrets.S3_HOST }}
    strategy:
      fail-fast: true
      matrix:
        include:
          - os: ubuntu-latest
            ARTIFACT: "i386-linux-ghcup"
            GHC_VER: 8.10.7
            ARCH: 32
          - os: ubuntu-latest
            ARTIFACT: "x86_64-linux-ghcup"
            GHC_VER: 8.10.7
            ARCH: 64
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          submodules: 'true'

      - if: matrix.ARCH == '32'
        name: Run build (32 bit linux)
        uses: docker://hasufell/i386-alpine-haskell:3.12
        with:
          args: sh .github/scripts/build.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: Alpine
          AWS_SECRET_ACCESS_KEY: ${{ env.AWS_SECRET_ACCESS_KEY }}
          AWS_ACCESS_KEY_ID: ${{ env.AWS_ACCESS_KEY_ID }}
          S3_HOST: ${{ env.S3_HOST }}

      - if: matrix.ARCH == '64'
        name: Run build (64 bit linux)
        uses: docker://hasufell/alpine-haskell:3.12
        with:
          args: sh .github/scripts/build.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: Alpine
          AWS_SECRET_ACCESS_KEY: ${{ env.AWS_SECRET_ACCESS_KEY }}
          AWS_ACCESS_KEY_ID: ${{ env.AWS_ACCESS_KEY_ID }}
          S3_HOST: ${{ env.S3_HOST }}

      - if: always()
        name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: artifacts
          path: |
            ./out/*


  build-arm:
    name: Build ARM binary
    runs-on: ${{ matrix.os }}
    env:
      CABAL_VER: 3.10.1.0
      JSON_VERSION: "0.0.7"
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      S3_HOST: ${{ secrets.S3_HOST }}
    strategy:
      fail-fast: true
      matrix:
        include:
          - os: [self-hosted, Linux, ARM64]
            ARTIFACT: "armv7-linux-ghcup"
            GHC_VER: 9.2.2
            ARCH: ARM
          - os: [self-hosted, Linux, ARM64]
            ARTIFACT: "aarch64-linux-ghcup"
            GHC_VER: 9.2.6
            ARCH: ARM64
    steps:
      - uses: docker://arm64v8/debian:10
        name: Cleanup (aarch64 linux)
        with:
          args: "find . -mindepth 1 -maxdepth 1 -exec rm -rf -- {} +"

      - name: git config
        run: |
          git config --global --get-all safe.directory | grep '^\*$' || git config --global --add safe.directory "*"
        shell: bash

      - name: Checkout code
        uses: actions/checkout@v3
        with:
          submodules: 'true'

      - if: matrix.ARCH == 'ARM'
        uses: docker://hasufell/arm32v7-debian-haskell:10
        name: Run build (armv7 linux)
        with:
          args: sh .github/scripts/build.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: Ubuntu
          AWS_SECRET_ACCESS_KEY: ${{ env.AWS_SECRET_ACCESS_KEY }}
          AWS_ACCESS_KEY_ID: ${{ env.AWS_ACCESS_KEY_ID }}
          S3_HOST: ${{ env.S3_HOST }}

      - if: matrix.ARCH == 'ARM64'
        uses: docker://hasufell/arm64v8-debian-haskell:10
        name: Run build (aarch64 linux)
        with:
          args: sh .github/scripts/build.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: Ubuntu
          AWS_SECRET_ACCESS_KEY: ${{ env.AWS_SECRET_ACCESS_KEY }}
          AWS_ACCESS_KEY_ID: ${{ env.AWS_ACCESS_KEY_ID }}
          S3_HOST: ${{ env.S3_HOST }}

      - if: always()
        name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: artifacts
          path: |
            ./out/*

  build-macwin:
    name: Build binary (Mac/Win)
    runs-on: ${{ matrix.os }}
    env:
      CABAL_VER: 3.10.1.0
      MACOSX_DEPLOYMENT_TARGET: 10.13
      JSON_VERSION: "0.0.7"
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      S3_HOST: ${{ secrets.S3_HOST }}
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: [self-hosted, macOS, ARM64]
            ARTIFACT: "aarch64-apple-darwin-ghcup"
            GHC_VER: 9.2.6
            ARCH: ARM64
          - os: macOS-11
            ARTIFACT: "x86_64-apple-darwin-ghcup"
            GHC_VER: 9.2.6
            ARCH: 64
          - os: windows-latest
            ARTIFACT: "x86_64-mingw64-ghcup"
            GHC_VER: 8.10.7
            ARCH: 64
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          submodules: 'true'

      - if: matrix.ARCH == 'ARM64' && runner.os == 'macOS'
        name: Run build
        run: |
          bash .github/scripts/brew.sh git coreutils llvm@11 autoconf automake
          export PATH="$HOME/.brew/bin:$HOME/.brew/sbin:$HOME/.brew/opt/llvm@11/bin:$PATH"
          export CC="$HOME/.brew/opt/llvm@11/bin/clang"
          export CXX="$HOME/.brew/opt/llvm@11/bin/clang++"
          export LD=ld
          export AR="$HOME/.brew/opt/llvm@11/bin/llvm-ar"
          export RANLIB="$HOME/.brew/opt/llvm@11/bin/llvm-ranlib"
          bash .github/scripts/build.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: na
          AWS_SECRET_ACCESS_KEY: ${{ env.AWS_SECRET_ACCESS_KEY }}
          AWS_ACCESS_KEY_ID: ${{ env.AWS_ACCESS_KEY_ID }}
          S3_HOST: ${{ env.S3_HOST }}
          HOMEBREW_CHANGE_ARCH_TO_ARM: 1

      - if: matrix.ARCH == '64' && runner.os == 'macOS'
        name: Run build (windows/mac)
        run: |
          bash .github/scripts/brew.sh coreutils
          export PATH="$HOME/.brew/bin:$HOME/.brew/sbin:$PATH"
          bash .github/scripts/build.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: na
          AWS_SECRET_ACCESS_KEY: ${{ env.AWS_SECRET_ACCESS_KEY }}
          AWS_ACCESS_KEY_ID: ${{ env.AWS_ACCESS_KEY_ID }}
          S3_HOST: ${{ env.S3_HOST }}
          HOMEBREW_CHANGE_ARCH_TO_ARM: 1

      - if: runner.os == 'Windows'
        name: Run build (windows/mac)
        run: |
          bash .github/scripts/brew.sh git coreutils autoconf automake
          bash .github/scripts/build.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: na
          AWS_SECRET_ACCESS_KEY: ${{ env.AWS_SECRET_ACCESS_KEY }}
          AWS_ACCESS_KEY_ID: ${{ env.AWS_ACCESS_KEY_ID }}
          S3_HOST: ${{ env.S3_HOST }}
          HOMEBREW_CHANGE_ARCH_TO_ARM: 1

      - if: always()
        name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: artifacts
          path: |
            ./out/*

  test-linux:
    name: Test linux
    needs: "build-linux"
    runs-on: ${{ matrix.os }}
    env:
      CABAL_VER: 3.10.1.0
      JSON_VERSION: "0.0.7"
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            ARTIFACT: "i386-linux-ghcup"
            GHC_VER: 8.10.7
            ARCH: 32
            DISTRO: Alpine
          - os: ubuntu-latest
            ARTIFACT: "x86_64-linux-ghcup"
            GHC_VER: 8.10.7
            ARCH: 64
            DISTRO: Alpine
          - os: ubuntu-latest
            ARTIFACT: "x86_64-linux-ghcup"
            GHC_VER: 8.10.7
            ARCH: 64
            DISTRO: Ubuntu

    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          submodules: 'true'

      - uses: actions/download-artifact@v3
        with:
          name: artifacts
          path: ./out

      - if: matrix.ARCH == '32' && matrix.DISTRO == 'Alpine'
        name: Run test (32 bit linux Alpine)
        uses: docker://hasufell/i386-alpine-haskell:3.12
        with:
          args: sh .github/scripts/test.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: ${{ matrix.DISTRO }}

      - if: matrix.ARCH == '64' && matrix.DISTRO == 'Alpine'
        name: Run test (64 bit linux Alpine)
        uses: docker://hasufell/alpine-haskell:3.12
        with:
          args: sh .github/scripts/test.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: ${{ matrix.DISTRO }}

      - if: matrix.DISTRO != 'Alpine'
        name: Run test (64 bit linux)
        run: |
          sudo apt-get install -y libnuma-dev zlib1g-dev libgmp-dev libgmp10 libssl-dev liblzma-dev libbz2-dev git wget lsb-release software-properties-common gnupg2 apt-transport-https gcc autoconf automake build-essential curl gzip
          sh .github/scripts/test.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: ${{ matrix.DISTRO }}

      - if: failure()
        name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: testfiles
          path: |
            ./test/golden/unix/GHCupInfo*json

  test-arm:
    name: Test ARM
    needs: "build-arm"
    runs-on: ${{ matrix.os }}
    env:
      CABAL_VER: 3.10.1.0
      JSON_VERSION: "0.0.7"
    strategy:
      matrix:
        include:
          - os: [self-hosted, Linux, ARM64]
            ARTIFACT: "armv7-linux-ghcup"
            GHC_VER: 9.2.2
            ARCH: ARM
            DISTRO: Ubuntu
          - os: [self-hosted, Linux, ARM64]
            ARTIFACT: "aarch64-linux-ghcup"
            GHC_VER: 9.2.6
            ARCH: ARM64
            DISTRO: Ubuntu

    steps:
      - uses: docker://arm64v8/debian:10
        name: Cleanup (aarch64 linux)
        with:
          args: "find . -mindepth 1 -maxdepth 1 -exec rm -rf -- {} +"

      - name: Checkout code
        uses: actions/checkout@v3
        with:
          submodules: 'true'

      - uses: actions/download-artifact@v3
        with:
          name: artifacts
          path: ./out

      - if: matrix.ARCH == 'ARM'
        uses: docker://hasufell/arm32v7-debian-haskell:10
        name: Run test (armv7 linux)
        with:
          args: sh .github/scripts/test.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: Ubuntu

      - if: matrix.ARCH == 'ARM64'
        uses: docker://hasufell/arm64v8-debian-haskell:10
        name: Run test (aarch64 linux)
        with:
          args: sh .github/scripts/test.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: Ubuntu

      - if: failure()
        name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: testfiles
          path: |
            ./test/golden/unix/GHCupInfo*json

  test-macwin:
    name: Test Mac/Win
    needs: "build-macwin"
    runs-on: ${{ matrix.os }}
    env:
      CABAL_VER: 3.10.1.0
      MACOSX_DEPLOYMENT_TARGET: 10.13
      JSON_VERSION: "0.0.7"
    strategy:
      matrix:
        include:
          - os: [self-hosted, macOS, ARM64]
            ARTIFACT: "aarch64-apple-darwin-ghcup"
            GHC_VER: 9.2.6
            ARCH: ARM64
            DISTRO: na
          - os: macOS-11
            ARTIFACT: "x86_64-apple-darwin-ghcup"
            GHC_VER: 9.2.6
            ARCH: 64
            DISTRO: na
          - os: windows-latest
            ARTIFACT: "x86_64-mingw64-ghcup"
            GHC_VER: 8.10.7
            ARCH: 64
            DISTRO: na

    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          submodules: 'true'

      - uses: actions/download-artifact@v3
        with:
          name: artifacts
          path: ./out

      - if: runner.os == 'macOS'
        name: Run test
        run: |
          bash .github/scripts/brew.sh coreutils
          export PATH="$HOME/.brew/bin:$HOME/.brew/sbin:$PATH"
          bash .github/scripts/test.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: ${{ matrix.DISTRO }}
          HOMEBREW_CHANGE_ARCH_TO_ARM: 1

      - if: runner.os != 'macOS'
        name: Run test
        run: bash .github/scripts/test.sh
        env:
          ARTIFACT: ${{ matrix.ARTIFACT }}
          ARCH: ${{ matrix.ARCH }}
          GHC_VER: ${{ matrix.GHC_VER }}
          DISTRO: ${{ matrix.DISTRO }}
          HOMEBREW_CHANGE_ARCH_TO_ARM: 1

      - if: failure() && runner.os == 'Windows'
        name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: testfiles
          path: |
            ./test/golden/windows/GHCupInfo*json

      - if: failure() && runner.os != 'Windows'
        name: Upload artifact
        uses: actions/upload-artifact@v3
        with:
          name: testfiles
          path: |
            ./test/golden/unix/GHCupInfo*json
  hls:
    name: hls
    needs: build-linux
    runs-on: ubuntu-latest
    env:
      GHC_VERSION: "8.10.7"
      HLS_TARGET_VERSION: "1.8.0.0"
      CABAL_VERSION: "3.8.1.0"
      JSON_VERSION: "0.0.7"
      ARTIFACT: "x86_64-linux-ghcup"
      DISTRO: Ubuntu
      ARCH: 64
      AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
      AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
      S3_HOST: ${{ secrets.S3_HOST }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          submodules: 'true'

      - uses: actions/download-artifact@v3
        with:
          name: artifacts
          path: ./out

      - name: Run hls build
        run: |
          sudo apt-get install -y libnuma-dev zlib1g-dev libgmp-dev libgmp10 libssl-dev liblzma-dev libbz2-dev git wget lsb-release software-properties-common gnupg2 apt-transport-https gcc autoconf automake build-essential curl gzip
          sh .github/scripts/hls.sh

  release:
    name: release
    needs: ["test-linux", "test-arm", "test-macwin", "hls"]
    runs-on: ubuntu-latest
    if: startsWith(github.ref, 'refs/tags/v')
    steps:
      - name: Download artifacts
        uses: actions/download-artifact@v3
        with:
          name: artifacts
          path: ./out

      - name: Release
        uses: softprops/action-gh-release@v1
        with:
          draft: true
          files: |
            ./out/*
