---
name: Linux build and test
on:
  push:
    branches:
      - 'master'
      - 'stable/*'
      - WS
  pull_request:
    branches:
      - '**'
  release:
    types:
      - created

jobs:
  test:
    name: Run tests
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [ubuntu-20.04, ubuntu-22.04]
        build_static: [true, false]
        download_requirements: [sudo apt install -y -qq gfortran liblapack-dev libmetis-dev libnauty2-dev]
        include:
          - os: macos-12
            build_static: false
            flags: CC=clang CXX=clang++ OSX=12
            download_requirements: brew install metis bash
          - os: macos-12
            build_static: false
            flags: CC=gcc-11 CXX=g++-11 OSX=12
            download_requirements: brew install metis bash
          - os: macos-12
            build_static: false
            flags: CC=gcc-12 CXX=g++-12 OSX=12
            download_requirements: brew install metis bash
    steps:
      - name: Checkout source
        uses: actions/checkout@v3
        with:
          path: ${{ github.event.repository.name }}
      - name: Install required packages from package manager
        run: ${{ matrix.download_requirements }}
      - name: Checkout coinbrew
        uses: actions/checkout@v3
        with:
          repository: coin-or/coinbrew
          path: coinbrew
      - name: Build project
        run: |
          export ${{ matrix.flags }}
          ADD_ARGS=()
          ADD_ARGS+=( --skip='ThirdParty/Metis ThirdParty/Mumps ThirdParty/Blas ThirdParty/Lapack' )
          ADD_BUILD_ARGS=()
          ADD_BUILD_ARGS+=( --tests main --enable-relocatable --disable-mysql )
          ADD_BUILD_ARGS+=( --verbosity 2 )
          [[ ${{ matrix.build_static }} == "true" ]] && \
          ADD_BUILD_ARGS+=( --disable-shared )
          bash coinbrew/coinbrew fetch ${{ github.event.repository.name }} --skip-update \
          "${ADD_ARGS[@]}"
          bash coinbrew/coinbrew build ${{ github.event.repository.name }} \
          "${ADD_ARGS[@]}" "${ADD_BUILD_ARGS[@]}" \
          ADD_CXXFLAGS="${ADD_CXXFLAGS}" CC=${CC} CXX=${CXX}
          [[ ${CC} ]] && CC="${CC}" || CC=""
          echo "CC=${CC}" >> $GITHUB_ENV
      - name: Archive dist contents
        run: |
          cp ${{ github.event.repository.name }}/README.md dist/
          cp ${{ github.event.repository.name }}/LICENSE dist/
          tar -czvf release.tar.gz -C dist .
      - name: Checkout package name generation script
        uses: actions/checkout@v3
        with:
          repository: coin-or-tools/platform-analysis-tools
          path: tools
          ref: 0.0.1
      - name: Retrieve platform info
        run: |
          python3 -m venv venv
          source venv/bin/activate
          pip install -r tools/requirements.txt
          [[ ${{ matrix.build_static }} == "true" ]] && buildtype=static || buildtype=
          platform_str=`python3 tools/hsf_get_platform.py -b $buildtype`
          echo "platform_string=${platform_str}" >> $GITHUB_ENV
      - name: Upload Artifact
        if: ${{ github.event_name == 'pull_request'}}
        uses: actions/upload-artifact@v3
        with:
          name: ${{ github.event.repository.name }}-${{ github.head_ref }}-${{ env.platform_string }}.tar.gz
          path: release.tar.gz
          if-no-files-found: error
      - name: Upload package to release
        if: ${{ github.event_name == 'release'}}
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ github.event.release.upload_url }}
          asset_path: ./release.tar.gz
          asset_name: ${{ github.event.repository.name }}-${{ github.event.release.tag_name }}-${{ env.platform_string }}.tar.gz
          asset_content_type: application/gzip
