on:
  # Run on pushes to tags, the "master" branch, and PR's
  push:
    tags:
      - '**'
    branches:
      - master
  pull_request:

name: CI

jobs:
  # Job key
  ci:
    # Label displayed in UI
    name: CI
    runs-on: ${{ matrix.os }}
    strategy:
      # Don't cancel the macOS build if the Linux build fails, etc.
      fail-fast: false
      matrix:
        os: [ubuntu-22.04, macos-13, windows-2022]
        tb-build-type: [release]
        tb-arch: [default-arch]
        exclude:
          - os: windows-2022
            tb-arch: default-arch
        include:
          - os: macos-13
            tb-build-type: asan
          - os: windows-2022
            tb-arch: x64
          - os: windows-2022
            tb-arch: Win32
    env:
      # Record pull request head commit SHA
      TB_PULL_REQUEST_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
      TB_OS_IMAGE: ${{ matrix.os }}
      # Tells vcpkg where binary packages are stored.
      VCPKG_DEFAULT_BINARY_CACHE: ${{ github.workspace }}/vcpkg/bincache
      # Let's use GitHub Action cache as storage for the vcpkg Binary Caching feature.
      VCPKG_BINARY_SOURCES: 'clear;x-gha,readwrite'

    steps:
      # See: https://github.com/actions/checkout
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          submodules: 'recursive'

      - name: "Create directory '${{ env.VCPKG_DEFAULT_BINARY_CACHE }}'"
        run: mkdir -p $VCPKG_DEFAULT_BINARY_CACHE
        shell: bash

      # Set env vars needed for vcpkg to leverage the GitHub Actions cache as a storage
      # for Binary Caching.
      - name: Set vcpkg environment variables
        uses: actions/github-script@v7
        with:
          script: |
            core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
            core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');

      # Dependencies
      - name: Install common Linux dependencies
        if: ${{ startsWith(matrix.os, 'ubuntu-') }}
        run: |
          sudo apt update
          sudo apt install build-essential libxi-dev libxrandr-dev libxxf86vm-dev freeglut3-dev mesa-common-dev libgl1-mesa-dev libglu1-mesa-dev libglm-dev pandoc cmake p7zip-full ninja-build xvfb libglew-dev libfreeimage-dev libfreetype6-dev libtinyxml2-dev libassimp-dev

      - name: Install Qt5 on Ubuntu 22.04
        if: ${{ matrix.os == 'ubuntu-22.04' }}
        run: |
          sudo apt update
          sudo apt install qtbase5-dev libqt5svg5-dev

      - name: Install Pandoc (Windows)
        if: ${{ matrix.os == 'windows-2022' }}
        run: |
          # Chocolatey was taking 3 minutes to install pandoc; just install it directly
          Invoke-WebRequest 'https://github.com/jgm/pandoc/releases/download/2.11.3.1/pandoc-2.11.3.1-windows-x86_64.zip' -OutFile 'pandoc.zip'
          if ("668A62A8990DEB2753753DF0C8D3F1BE567026FE" -ne (Get-FileHash -Path 'pandoc.zip' -Algorithm SHA1).Hash) { exit }
          7z x pandoc.zip -o'.'
          $tb_pandoc_path = "$(pwd)\pandoc-2.11.3.1"
          # Add this to the system path
          echo "Pandoc path: $tb_pandoc_path"
          # See: https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#environment-files
          echo "$tb_pandoc_path" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

      - name: Install Qt on Windows (x64)
        if: ${{ matrix.os == 'windows-2022' && matrix.tb-arch == 'x64' }}
        uses: jurplel/install-qt-action@v3
        with:
          version: '5.15.2'
          arch: 'win64_msvc2019_64'

      - name: Install Qt on Windows (Win32)
        if: ${{ matrix.os == 'windows-2022' && matrix.tb-arch == 'Win32' }}
        uses: jurplel/install-qt-action@v3
        with:
          version: '5.15.2'
          arch: 'win32_msvc2019'

      - name: Install macOS dependencies
        if: ${{ matrix.os == 'macos-13' }}
        run: |
          brew install cmake p7zip pandoc qt5 ninja autoconf automake kadwanev/brew/retry

      # Run build script
      - name: Linux build
        if: ${{ startsWith(matrix.os, 'ubuntu-') }}
        run: ./CI-linux.sh

      - name: macOS build
        if: ${{ matrix.os == 'macos-13' }}
        run: |
          if [ '${{ matrix.tb-build-type }}' = 'asan' ]; then
            export TB_DEBUG_BUILD=true
          fi
          ./CI-macos.sh

      - name: Windows build
        if: ${{ matrix.os == 'windows-2022' }}
        # NOTE: docs at https://github.com/jurplel/install-qt-action
        # mention that it sets a Qt5_DIR environment variable.
        # Our script expects it in QT5_INSTALL_DIR
        # NOTE: cmd.exe /c = run a command and terminate
        run: |
          $Env:QT5_INSTALL_DIR = $env:Qt5_DIR
          cmd.exe /c CI-windows.bat
        env:
          TB_ARCH: ${{ matrix.tb-arch }}

      # Upload artifacts.
      # These need to be separate, otherwise all of the artifacts are bundled into
      # one .zip file.

      # Windows 2022 x32
      - name: Upload win32 artifact
        uses: actions/upload-artifact@v4
        if: ${{ matrix.os == 'windows-2022' && matrix.tb-arch == 'Win32' }}
        with:
          name: windows-2022-x32
          path: |
            cmakebuild/*.7z
            cmakebuild/*.7z.md5

      # Windows 2022 x64
      - name: Upload win64 artifact
        uses: actions/upload-artifact@v4
        if: ${{ matrix.os == 'windows-2022' && matrix.tb-arch == 'x64' }}
        with:
          name: windows-2022-x64
          path: |
            cmakebuild/*.7z
            cmakebuild/*.7z.md5

      # Ubuntu 22.04
      - name: Upload Linux artifacts
        uses: actions/upload-artifact@v4
        if: ${{ matrix.os == 'ubuntu-22.04' }}
        with:
          name: ubuntu-22.04
          path: |
            build/*.deb
            build/*.md5

      # macOS
      - name: Upload macOS artifact
        uses: actions/upload-artifact@v4
        if: ${{ matrix.os == 'macos-13' && matrix.tb-build-type != 'asan' }}
        with:
          name: macos-13
          path: |
            build/*.dmg
            build/*.md5

      # Create release

      # Official action upload-release-asset doesn't support uploading files
      # based on a glob, so use https://github.com/softprops/action-gh-release
      - name: Release
        uses: softprops/action-gh-release@v1
        if: ${{ startsWith(github.ref, 'refs/tags/') && matrix.tb-build-type != 'asan' }}
        with:
          draft: true
          files: |
            build/*.deb
            build/*.dmg
            build/*.md5
            cmakebuild/*.7z
            cmakebuild/*.7z.md5
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload compiled manual
        uses: peaceiris/actions-gh-pages@v3
        if: ${{ startsWith(github.ref, 'refs/tags/') && matrix.os == 'ubuntu-22.04' }}
        with:
          # I've created a SSH key pair following https://github.com/peaceiris/actions-gh-pages#%EF%B8%8F-create-ssh-deploy-key
          # The TrenchBroom/TrenchBroom repo has a repository secret ACTIONS_DEPLOY_KEY set to the SSH private key
          # The TrenchBroom/manual repo has a deploy key set to the SSH public key
          deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }}
          external_repository: TrenchBroom/manual # Repo to deploy to
          publish_branch: gh-pages # Branch to deploy to
          publish_dir: ./build/app/gen-manual # Source directory tree
          destination_dir: latest # Deploy to this directory in target repo
