name: Build

on:
  push:
    branches:
      - "*"
  pull_request:
    branches:
      - "*"
    types: [opened, synchronize, reopened]

concurrency:
  group: ${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    name: Build
    runs-on: windows-2022
    strategy:
      fail-fast: false
      matrix:
        configuration:
          - Debug
          - Release
    steps:
      - name: Check out files
        uses: actions/checkout@v4
        with:
          submodules: true
          fetch-depth: 0
          # NOTE - If LFS ever starts getting used during builds, switch this to true!
          lfs: false

      - name: Add msbuild to PATH
        uses: microsoft/setup-msbuild@v1.3.1

      - name: Generate project files
        run: tools/premake5 vs2022

      - name: Set up problem matching
        uses: ammaraskar/msvc-problem-matcher@master

      - name: Build ${{matrix.configuration}}
        run: msbuild /m /v:minimal /p:Configuration=${{matrix.configuration}} /p:Platform=x64 build/t7x.sln

      - name: Upload ${{matrix.configuration}} symbols
        if: matrix.configuration == 'Release' && github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop')
        uses: actions/upload-artifact@v3.1.3
        with:
          name: ${{matrix.configuration}} Symbols
          path: |
            build/bin/x64/${{matrix.configuration}}/t7x.pdb

      - name: Upload ${{matrix.configuration}} binary
        if: matrix.configuration == 'Release' && github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop')
        uses: actions/upload-artifact@v3.1.3
        with:
          name: ${{matrix.configuration}} Binary
          path: |
            build/bin/x64/${{matrix.configuration}}/t7x.exe

      - name: Upload ${{matrix.configuration}} data artifacts
        if: matrix.configuration == 'Release' && github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop')
        uses: actions/upload-artifact@v3.1.3
        with:
          name: ${{matrix.configuration}} data artifacts
          path: |
            data/*

  deploy:
    name: Deploy artifacts
    needs: build
    runs-on: ubuntu-latest
    if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop')
    steps:
      - name: Setup environment
        run: echo "BOIII_MASTER_PATH=${{ secrets.BOIII_MASTER_SSH_PATH }}" >> $GITHUB_ENV

      - name: Download Release binaries
        uses: actions/download-artifact@v3.0.2
        with:
          name: Release Binary

      - name: Download Release data artifacts
        uses: actions/download-artifact@v3.0.2
        with:
          name: Release data artifacts
          path: data

      - name: Install SSH key
        uses: shimataro/ssh-key-action@v2.6.1
        with:
          key: ${{ secrets.BOIII_MASTER_SSH_PRIVATE_KEY }}
          known_hosts: "just-a-placeholder-so-we-dont-get-errors"

      - name: Add known hosts
        run: ssh-keyscan -H ${{ secrets.BOIII_MASTER_SSH_ADDRESS }} >> ~/.ssh/known_hosts

      - name: Remove old data files
        run: ssh ${{ secrets.BOIII_MASTER_SSH_USER }}@${{ secrets.BOIII_MASTER_SSH_ADDRESS }} rm -rf ${{ env.BOIII_MASTER_PATH }}/t7x/data/*

      - name: Upload T7x binary
        run: rsync -avz t7x.exe ${{ secrets.BOIII_MASTER_SSH_USER }}@${{ secrets.BOIII_MASTER_SSH_ADDRESS }}:${{ env.BOIII_MASTER_PATH }}/t7x/

      - name: Upload data files
        run: rsync -avz ./data/ ${{ secrets.BOIII_MASTER_SSH_USER }}@${{ secrets.BOIII_MASTER_SSH_ADDRESS }}:${{ env.BOIII_MASTER_PATH }}/t7x/data/

      - name: Publish changes
        run: ssh ${{ secrets.BOIII_MASTER_SSH_USER }}@${{ secrets.BOIII_MASTER_SSH_ADDRESS }} ${{ secrets.BOIII_MASTER_SSH_CHANGE_PUBLISH_COMMAND }}

      - name: Prepare cleanup
        run: echo "" > t7x.exe

      - name: Cleanup
        uses: actions/upload-artifact@v3.1.3
        with:
          name: Release Binary
          retention-days: 1
          path: |
            t7x.exe
