name: Windows 10 (64-bit, Qt6)

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: windows-10-64bit
  MAKE: mingw32-make
  BT_INSTALLBASE: ${{ github.workspace }}/target/
  MINGW_VERSION: 11.2.0
  MINGW_BITNESS: 64
  MINGW_CHOCOBASE: /c/ProgramData/chocolatey/lib/mingw/tools/install/mingw64
  MINGW_INSTALLBASE: ${{ github.workspace }}/mingw64/
  QT_VERSION: 6.4.0
  QT_HOSTOS: windows_x86
  QT_TOOLCHAIN: win64_mingw
  QT_MODULES: qtbase qttools qttranslations qt5compat qtdeclarative
  QT_INSTALLBASE: ${{ github.workspace }}/Qt/

jobs:
  build:
    runs-on: windows-latest

    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

      ## Windows-specific steps

      # Unable to cache system-installed MinGW. Uploaded cache is supposedly an empty tarball
      # when trying to fetch it.
      # > Received 30 of 30 (100.0%), 0.0 MBs/sec
      # > Cache Size: ~0 MB (30 B)
      # We instead copy & reown the MinGW installation to the GitHub workspace,
      # ugly but it works. :/
      - name: Fetching MinGW cache.
        id: mingw-cache
        uses: actions/cache@v4
        with:
          path: ${{ env.MINGW_INSTALLBASE }}
          key: ${{ env.MINGW_VERSION }} ${{ env.MINGW_BITNESS }}

      - name: Installing MinGW.
        run: |
          ./scripts/fetch_mingw.sh ${MINGW_CHOCOBASE} ${MINGW_VERSION} ${MINGW_BITNESS} ${MINGW_INSTALLBASE}

      # "Prepending to PATH" doesn't work properly and inserts the path *close*
      # to the start of PATH, but *after* the system's MinGW path.
      # Our MinGW PATH is not searched early enough to work for this build
      # so we either manually reexport PATH for ever step or fiddle with the PATH even more. :/
      # - name: Adding MinGW to PATH.
      #   run: |
      #     echo ${MINGW_CHOCOBASE}/bin >> ${GITHUB_PATH}

      # 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}

      ## End Windows-specific steps

      - name: Configuring.
        run: |
          export PATH="${MINGW_INSTALLBASE}/bin:${PATH}"
          lupdate Project.pro
          mkdir build
          pushd build
          cmake .. -G "MinGW Makefiles" -DCMAKE_INSTALL_PREFIX=${BT_INSTALLBASE} -DCMAKE_BUILD_TYPE=Release -DRTAUDIO_API_DS=ON
          popd

      - name: Building.
        run: |
          export PATH="${MINGW_INSTALLBASE}/bin:${PATH}"
          pushd build
          ${MAKE} -j2
          popd

      - name: Installing.
        run: |
          export PATH="${MINGW_INSTALLBASE}/bin:${PATH}"
          pushd build
          ${MAKE} -j2 install
          popd

      - name: Test packaging.
        if: env.DONT_PACKAGE != 'true'
        run: |
          export PATH="${MINGW_INSTALLBASE}/bin:${PATH}"
          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}
          7z a -tzip ${package_name}{.zip,}

      - 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 }}.zip
          asset_name: ${{ steps.packaging.outputs.package-name }}.zip
          tag: ${{ github.ref }}
