name: macOS

on:
  push:
    branches: master
    tags: 'v*.*.*'
    paths-ignore:
      - '**.md'
      - '**.txt'
      - 'LICENSE'
      - '.gitignore'
  pull_request:
    branches: master
    paths-ignore:
      - '**.md'
      - '**.txt'
      - 'LICENSE'
      - '.gitignore'

defaults:
  run:
    shell: bash

env:
  BUILD_TARGET: macos-64bit
  SCRIPT_NAME: osx
  MAKE: make
  BT_INSTALLBASE: ${{ github.workspace }}/target/
  QT_VERSION: 6.4.0
  QT_HOSTOS: mac_x64
  QT_TOOLCHAIN: clang_64
  QT_MODULES: qtbase qttools qttranslations qt5compat qtdeclarative
  QT_INSTALLBASE: ${{ github.workspace }}/Qt/
  QMAKE_EXTRA_ARGUMENTS: CONFIG+=install_flat CONFIG+=use_jack CONFIG+=no_warnings_are_errors

jobs:
  build:
    runs-on: macos-12

    steps:
      - name: Identify build type.
        id: identify-build
        run: |
          case ${GITHUB_REF} in
            refs/tags/* )
              TAG=${GITHUB_REF#refs/tags/}
              echo "Release ${TAG}"
              echo "build-tag=${TAG}" >> $GITHUB_OUTPUT
              echo "release=true" >> $GITHUB_OUTPUT
              ;;
            refs/pull/*)
              PR=$(echo ${GITHUB_REF} | cut -d/ -f3)
              echo "Test PR #${PR}"
              echo "build-tag=pr-${PR}" >> $GITHUB_OUTPUT
              echo "release=false" >> $GITHUB_OUTPUT
              ;;
            refs/heads/* )
              BRANCH=${GITHUB_REF#refs/heads/}
              echo "Test ${BRANCH}"
              echo "build-tag=${BRANCH}" >> $GITHUB_OUTPUT
              echo "release=false" >> $GITHUB_OUTPUT
              ;;
            * )
              echo "Test (unknown)"
              echo "build-tag=unknown" >> $GITHUB_OUTPUT
              echo "release=false" >> $GITHUB_OUTPUT
              ;;
          esac

      - name: Checking out repository.
        uses: actions/checkout@v4
        with:
          submodules: recursive

      ## macOS-specific steps

      - name: Pin Xcode version
        run: sudo xcode-select -s "/Applications/Xcode_14.2.app"

      # Cache Qt installations, very costly & flakey to fetch
      - name: Fetching Qt cache.
        id: qt-cache
        uses: actions/cache@v4
        with:
          path: ${{ env.QT_INSTALLBASE }}
          key: ${{ runner.os }} Qt${{ env.QT_VERSION }} ${{ env.QT_TOOLCHAIN }} ${{ env.QT_MODULES }}

      - name: Installing Qt.
        run: |
          ./scripts/fetch_qt.sh ${QT_INSTALLBASE} ${QT_VERSION} ${QT_HOSTOS} ${QT_TOOLCHAIN} ${QT_MODULES}
      - name: Adding Qt to PATH.
        run: |
          find ${QT_INSTALLBASE} -type d -name bin >> ${GITHUB_PATH}

      - name: Installing dependencies.
        run: |
          export HOMEBREW_NO_INSTALL_CLEANUP=1
          brew update
          brew install pkg-config jack coreutils

      ## End macOS-specific steps

      - name: Configuring.
        run: |
          lupdate Project.pro
          qmake Project.pro PREFIX=${BT_INSTALLBASE} \
            CONFIG+=release CONFIG-=debug ${QMAKE_EXTRA_ARGUMENTS}
          ${MAKE} qmake_all

      - name: Building.
        run: |
          ${MAKE} -j2

      - name: Installing.
        run: |
          ${MAKE} -j2 install

      - name: Test packaging.
        if: env.DONT_PACKAGE != 'true'
        run: |
          pushd ${BT_INSTALLBASE}
          bash ${GITHUB_WORKSPACE}/scripts/package_${SCRIPT_NAME:-${BUILD_TARGET%%-*}}.sh
          popd

      - name: Finalize packaging.
        id: packaging
        if: steps.identify-build.outputs.release == 'true' && env.DONT_PACKAGE != 'true'
        run: |
          export package_name="BambooTracker-${{ steps.identify-build.outputs.build-tag }}-${BUILD_TARGET}"
          echo "package-name=${package_name}" >> $GITHUB_OUTPUT
          mv -v ${BT_INSTALLBASE} ${package_name}
          tar -cvf- \
            ${package_name} | gzip -9c > ${package_name}.tar.gz

      - name: Upload release package.
        if: steps.identify-build.outputs.release == 'true' && env.DONT_PACKAGE != 'true'
        uses: svenstaro/upload-release-action@v2
        with:
          repo_token: ${{ secrets.GITHUB_TOKEN }}
          file: ${{ steps.packaging.outputs.package-name }}.tar.gz
          asset_name: ${{ steps.packaging.outputs.package-name }}.tar.gz
          tag: ${{ github.ref }}
