name: Build Test package

on:
  push:
    branches:
      - 'test/*'

env:
  QT_VERSION: 5.12.12

jobs:
  build_linux:
    strategy:
      matrix:
        configuration: [FastDebug, Release]
    name: Linux
    runs-on: ubuntu-latest
    container: ghcr.io/scp-fs2open/linux_build:sha-71099c9
    steps:
      - uses: actions/checkout@v7
        name: Checkout
        with:
          submodules: true
      - name: Set workspace as safe
        # This appears to be broken in current actions, so do it manually
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      - name: Set up test version
        shell: bash
        run: |
          [[ "${{ github.ref }}" =~ ^refs\/heads\/test\/(.*)$ ]]
          # Override the revision string so that the builds are named correctly
          echo "set(FSO_VERSION_REVISION_STR ${BASH_REMATCH[1]})" > "version_override.cmake"
      - name: Configure CMake
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          COMPILER: gcc-9
          ENABLE_QTFRED: OFF
        run: $GITHUB_WORKSPACE/ci/linux/configure_cmake.sh
      - name: Compile
        working-directory: ./build
        env:
          CONFIGURATION: ${{ matrix.configuration }}
        run: |
          LD_LIBRARY_PATH=$Qt5_DIR/lib:$LD_LIBRARY_PATH ninja -k 20 all
      - name: Run Tests
        working-directory: ./build
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          XDG_RUNTIME_DIR: /root
        run: $GITHUB_WORKSPACE/ci/linux/run_tests.sh
      - name: Generate AppImage
        working-directory: ./build
        env:
          CONFIGURATION: ${{ matrix.configuration }}
        run: $GITHUB_WORKSPACE/ci/linux/generate_appimage.sh $GITHUB_WORKSPACE/build/install
      - name: Upload build result
        uses: actions/upload-artifact@v7
        with:
          name: linux-${{ matrix.configuration }}
          path: ${{ github.workspace }}/build/install/*.AppImage
  linux_zip:
    name: Build Linux distribution zip
    needs: build_linux
    runs-on: ubuntu-latest
    container: ghcr.io/scp-fs2open/sftp_upload:sha-844131c
    steps:
      - uses: actions/checkout@v7
        name: Checkout
        with:
          submodules: true
          fetch-depth: '0'
          ref: '${{ github.ref }}'
      - name: Set workspace as safe
        # This appears to be broken in current actions, so do it manually
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      - name: Download Release builds
        uses: actions/download-artifact@v8
        with:
          name: linux-Release
          path: builds
      - name: Download FastDebug builds
        uses: actions/download-artifact@v8
        with:
          name: linux-FastDebug
          path: builds
      - name: Create Distribution package
        working-directory: ./builds
        run: $GITHUB_WORKSPACE/ci/linux/create_dist_pack.sh Linux
      - name: Upload result package
        working-directory: ./builds
        env:
          INDIEGAMES_USER: ${{ secrets.INDIEGAMES_USER }}
          INDIEGAMES_SSHPASS: ${{ secrets.INDIEGAMES_PASSWORD }}
          DATACORDER_USER: ${{ secrets.DATACORDER_USER }}
          DATACORDER_SSHPASS: ${{ secrets.DATACORDER_PASSWORD }}
        run: $GITHUB_WORKSPACE/ci/linux/upload_dist_package.sh test

  build_windows:
    strategy:
      matrix:
        configuration: [FastDebug, Release]
        arch: [Win32, x64]
        simd: [SSE2]
    name: Windows
    runs-on: windows-2022
    steps:
      - uses: actions/checkout@v7
        name: Checkout
        with:
          submodules: true
      - name: Set workspace as safe
        # This appears to be broken in current actions, so do it manually
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      - name: Set up test version
        shell: bash
        run: |
          [[ "${{ github.ref }}" =~ ^refs\/heads\/test\/(.*)$ ]]
          # Override the revision string so that the builds are named correctly
          echo "set(FSO_VERSION_REVISION_STR ${BASH_REMATCH[1]})" > "version_override.cmake"
      # - name: Cache Qt
        # id: cache-qt-win
        # uses: actions/cache@v1
        # with:
          # path: ${{ github.workspace }}/../Qt
          # key: ${{ runner.os }}-${{ matrix.arch }}-QtCache-${{ env.QT_VERSION }}
      # - name: Install Qt (32 bit)
        # uses: jurplel/install-qt-action@v2
        # if: ${{ matrix.arch == 'Win32' }}
        # with:
          # version: ${{ env.QT_VERSION }}
          # dir: ${{ github.workspace }}/..
          # arch: win32_msvc2017
          # cached: ${{ steps.cache-qt-win.outputs.cache-hit }}
          # aqtversion: ==0.8
      # - name: Install Qt (64 bit)
        # uses: jurplel/install-qt-action@v2
        # if: ${{ matrix.arch == 'x64' }}
        # with:
          # version: ${{ env.QT_VERSION }}
          # dir: ${{ github.workspace }}/..
          # arch: win64_msvc2017_64
          # cached: ${{ steps.cache-qt-win.outputs.cache-hit }}
          # aqtversion: ==0.8
      - name: Prepare Vulkan SDK
        uses: humbletim/setup-vulkan-sdk@v1.2.1
        with:
          vulkan-query-version: 1.4.304.1
          vulkan-components: Vulkan-Headers, Vulkan-Loader
          vulkan-use-cache: true
      - name: Configure CMake
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          ARCHITECTURE: ${{ matrix.arch }}
          SIMD: ${{ matrix.simd }}
        shell: bash
        run: |
          mkdir build
          cd build

          if [ "$ARCHITECTURE" = "Win32" ]; then
              cmake -DCMAKE_INSTALL_PREFIX="$(pwd)/install" -DFSO_USE_SPEECH="ON" \
                -DFSO_USE_VOICEREC="ON" -DFORCED_SIMD_INSTRUCTIONS="$SIMD" \
                -DFSO_BUILD_QTFRED=OFF -DFSO_BUILD_TESTS=ON \
                -DFSO_INSTALL_DEBUG_FILES="ON" -DFSO_BUILD_WITH_VULKAN="OFF" -A "$ARCHITECTURE" \
                -G "Visual Studio 17 2022" -T "v143" -DCMAKE_BUILD_TYPE=$CONFIGURATION ..
          else
              cmake -DCMAKE_INSTALL_PREFIX="$(pwd)/install" -DFSO_USE_SPEECH="ON" \
                -DFSO_USE_VOICEREC="ON" -DFORCED_SIMD_INSTRUCTIONS="$SIMD" \
                -DFSO_BUILD_QTFRED=OFF -DFSO_BUILD_TESTS=ON \
                -DFSO_INSTALL_DEBUG_FILES="ON" -A "$ARCHITECTURE" \
                -G "Visual Studio 17 2022" -T "v143" -DCMAKE_BUILD_TYPE=$CONFIGURATION ..
          fi
      - name: Compile
        working-directory: ./build
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          COMPILER: ${{ matrix.compiler }}
        shell: bash
        run: |
          cmake --build . --config "$CONFIGURATION" --target INSTALL -- /verbosity:minimal
          ls -alR "$(pwd)/install"
      - name: Run Tests
        working-directory: ./build
        env:
          CONFIGURATION: ${{ matrix.configuration }}
        shell: bash
        run: ./bin/$CONFIGURATION/unittests --gtest_shuffle
      - name: Upload build result
        uses: actions/upload-artifact@v7
        with:
          name: windows-${{ matrix.configuration }}-${{ matrix.arch }}-${{ matrix.simd }}
          path: ${{ github.workspace }}/build/install/*
  windows_zip:
    name: Build Windows distribution zip
    needs: build_windows
    runs-on: ubuntu-latest
    container: ghcr.io/scp-fs2open/sftp_upload:sha-844131c
    strategy:
      matrix:
        arch: [Win32, x64]
        simd: [SSE2]
    steps:
      - uses: actions/checkout@v7
        name: Checkout
        with:
          submodules: true
          fetch-depth: '0'
          ref: '${{ github.ref }}'
      - name: Set workspace as safe
        # This appears to be broken in current actions, so do it manually
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      - name: Download Release builds
        uses: actions/download-artifact@v8
        with:
          name: windows-Release-${{ matrix.arch }}-${{ matrix.simd }}
          path: builds
      - name: Download FastDebug builds
        uses: actions/download-artifact@v8
        with:
          name: windows-FastDebug-${{ matrix.arch }}-${{ matrix.simd }}
          path: builds
      - name: Create Distribution package
        working-directory: ./builds
        shell: bash
        env:
          ARCH: ${{ matrix.arch }}
          SIMD: ${{ matrix.simd }}
        run: $GITHUB_WORKSPACE/ci/linux/create_dist_pack.sh Windows
      - name: Upload result package
        working-directory: ./builds
        shell: bash
        env:
          INDIEGAMES_USER: ${{ secrets.INDIEGAMES_USER }}
          INDIEGAMES_SSHPASS: ${{ secrets.INDIEGAMES_PASSWORD }}
          DATACORDER_USER: ${{ secrets.DATACORDER_USER }}
          DATACORDER_SSHPASS: ${{ secrets.DATACORDER_PASSWORD }}
        run: $GITHUB_WORKSPACE/ci/linux/upload_dist_package.sh test

  build_mac:
    strategy:
      matrix:
        configuration: [FastDebug, Release]
        compiler: [clang]
        arch: [x86_64, arm64]
    name: Mac
    runs-on: macos-latest
    steps:
      # - name: Cache Qt
        # id: cache-qt-mac
        # uses: actions/cache@v1
        # with:
          # path: ${{ github.workspace }}/../Qt
          # key: ${{ runner.os }}-QtCache-${{ env.QT_VERSION }}
      # - name: Install Qt
        # uses: jurplel/install-qt-action@v2
        # with:
          # version: ${{ env.QT_VERSION }}
          # dir: ${{ github.workspace }}/..
          # cached: ${{ steps.cache-qt-mac.outputs.cache-hit }}
          # setup-python: 'false'
          # aqtversion: ==1.1.3
          # py7zrversion: '==0.19.*'
      - uses: actions/checkout@v7
        name: Checkout
        with:
          submodules: true
          fetch-depth: 0
      - name: Set workspace as safe
        # This appears to be broken in current actions, so do it manually
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      - name: Prepare Vulkan SDK
        uses: humbletim/setup-vulkan-sdk@v1.2.1
        with:
          vulkan-query-version: 1.4.304.1
          vulkan-components: Vulkan-Headers, Vulkan-Loader
          vulkan-use-cache: true
      - name: Set up test version
        shell: bash
        run: |
          [[ "${{ github.ref }}" =~ ^refs\/heads\/test\/(.*)$ ]]
          # Override the revision string so that the builds are named correctly
          echo "set(FSO_VERSION_REVISION_STR ${BASH_REMATCH[1]})" > "version_override.cmake"
      - name: Configure CMake
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          COMPILER: ${{ matrix.compiler }}
          ARCHITECTURE: ${{ matrix.arch }}
          ENABLE_QTFRED: OFF
        run: $GITHUB_WORKSPACE/ci/linux/configure_cmake.sh
      - name: Compile
        working-directory: ./build
        run: LD_LIBRARY_PATH=$Qt5_DIR/lib:$LD_LIBRARY_PATH ninja all
      - name: Run Tests
        working-directory: ./build
        env:
          CONFIGURATION: ${{ matrix.configuration }}
          ARCH: ${{ matrix.arch }}
          XDG_RUNTIME_DIR: /root
        run: $GITHUB_WORKSPACE/ci/linux/run_tests.sh
      - name: Sign builds
        working-directory: ./build/bin
        run: |
          # make sure OpenAL framework is signed
          find . -name OpenAL -type f | while read xx; do codesign --force -s - $xx; done
          find . -name OpenAL.framework -type d | while read xx; do codesign --force -s - $xx; done
          # make sure dylibs are signed
          find . -name \*.dylib -type f | while read xx; do codesign --force -s - $xx; done
          # sign binaries
          find . -perm +111 -type f | grep MacOS | while read xx; do codesign --force -s - $xx; done
          # sign apps
          find . -name \*.app -maxdepth 1 | while read xx; do codesign --force -s - $xx; done
      - name: Package build result
        working-directory: ./build/bin
        # Use GNU tar here (part of runner image) to avoid weird corruption bug with bsdtar
        # Ref: https://github.com/actions/runner-images/issues/2619
        run: gtar -cvzf macos-build-${{ matrix.configuration }}-${{ matrix.arch }}.tgz *.app
      - name: Upload build result
        uses: actions/upload-artifact@v7
        with:
          name: mac-${{ matrix.configuration }}-${{ matrix.arch }}
          path: ${{ github.workspace }}/build/bin/macos-build-${{ matrix.configuration }}-${{ matrix.arch }}.tgz
  mac_zip:
    name: Build Mac distribution zip
    needs: build_mac
    runs-on: ubuntu-latest
    container: ghcr.io/scp-fs2open/sftp_upload:sha-844131c
    strategy:
      matrix:
        arch: [x86_64, arm64]
    steps:
      - uses: actions/checkout@v7
        name: Checkout
        with:
          submodules: true
          fetch-depth: '0'
          ref: '${{ github.ref }}'
      - name: Set workspace as safe
        # This appears to be broken in current actions, so do it manually
        run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
      - name: Download Release builds
        uses: actions/download-artifact@v8
        with:
          name: mac-Release-${{ matrix.arch }}
          path: builds
      - name: Download FastDebug builds
        uses: actions/download-artifact@v8
        with:
          name: mac-FastDebug-${{ matrix.arch }}
          path: builds
      - name: Unpack builds
        working-directory: ./builds
        run: |
          tar -xvzf macos-build-Release-${{ matrix.arch }}.tgz
          tar -xvzf macos-build-FastDebug-${{ matrix.arch }}.tgz
      - name: Create Distribution package
        working-directory: ./builds
        env:
          ARCH: ${{ matrix.arch }}
        run: $GITHUB_WORKSPACE/ci/linux/create_dist_pack.sh Mac
      - name: Upload result package
        working-directory: ./builds
        env:
          INDIEGAMES_USER: ${{ secrets.INDIEGAMES_USER }}
          INDIEGAMES_SSHPASS: ${{ secrets.INDIEGAMES_PASSWORD }}
          DATACORDER_USER: ${{ secrets.DATACORDER_USER }}
          DATACORDER_SSHPASS: ${{ secrets.DATACORDER_PASSWORD }}
        run: $GITHUB_WORKSPACE/ci/linux/upload_dist_package.sh test
