name: build language

on:
  workflow_call:
    inputs:
      language:
        description: 'any of Chinese German English French Polish Italian Dutch Russian'
        required: true
        default: 'English'
        type: string
      assemble:
        description: 'assemble full package'
        required: true
        default: true
        type: boolean
      continue-on-error:
        description: 'allows a language to fail, used when building all languages'
        required: false
        default: false
        type: boolean

jobs:
  compile:
    runs-on: windows-latest # required for msbuild
    continue-on-error: ${{ inputs.continue-on-error }}
    strategy:
      fail-fast: false
      matrix:
        application: [ja2, ja2mapeditor, ja2ub, ja2ubmapeditor]

    steps:
      - name: Checkout source
        uses: actions/checkout@v4

      - name: Download versions.env
        uses: actions/download-artifact@v4
        with:
          name: versions.env
          path: artifacts
      - name: Restore versions.env
        shell: bash
        run: cat artifacts/versions.env >> $GITHUB_ENV

      # TODO: Move this into CMake, just taking the GitHub Actions parameters as -D variables
      - name: Update GameVersion.cpp
        shell: bash
        run: |
          set -eux
          INPUTS_LANGUAGE=${{ inputs.language }}
          GAME_VERSION=$(echo "$GAME_VERSION" | tr -cd '[:print:]' | tr -d '"\\')
          GAME_BUILD="${INPUTS_LANGUAGE:0:2} $GAME_BUILD_INFORMATION"
          GAME_BUILD=$(echo "$GAME_BUILD" | tr -cd '[:print:]' | tr -d '"\\')
          sed -i "s|@Version@|${GAME_VERSION:0:15}|" Ja2/GameVersion.cpp
          sed -i "s|@Build@|${GAME_BUILD:0:255}|" Ja2/GameVersion.cpp
          cat Ja2/GameVersion.cpp

      - name: Turn on link-time optimization?
        if: fromJSON(inputs.assemble)
        shell: bash
        run: |
          set -eux

          echo "
          LTO=ON
          " >> $GITHUB_ENV

      - name: Prepare build properties
        shell: bash
        run: |
          set -eux
          
          touch CMakePresets.json

          JA2Language=$(echo '${{ inputs.language }}' | tr '[:lower:]' '[:upper:]')
          JA2Application=$(echo '${{ matrix.application }}' | tr '[:lower:]' '[:upper:]')
          
          echo "
          JA2Language=$JA2Language
          JA2Application=$JA2Application
          " >> $GITHUB_ENV

      - uses: microsoft/setup-msbuild@v2
        with:
          msbuild-architecture: x86
      - uses: ilammy/msvc-dev-cmd@v1
        with:
          arch: x86
      - name: Prepare build
        run: |
          cmake -S . -B build -GNinja -DCMAKE_BUILD_TYPE=Release -DLanguages="$Env:JA2Language" -DApplications="$Env:JA2Application" -DLTO_OPTION="$Env:LTO"
      - name: Build
        run: |
          cmake --build build/ -- -v
      - name: List build artifacts
        shell: bash
        run: |
          find build/

      - name: Upload
        uses: actions/upload-artifact@v4
        with:
          name: ${{ inputs.language }}_${{ matrix.application }}
          path: build/*.exe

  assemble:
    needs: [ compile ]
    if: inputs.assemble
    runs-on: windows-latest # required for case-insensitive filesystem handling
    continue-on-error: ${{ inputs.continue-on-error }}

    steps:
      - name: Download versions.env
        uses: actions/download-artifact@v4
        with:
          name: versions.env
          path: artifacts
      - name: Restore versions.env
        shell: bash
        run: cat artifacts/versions.env >> $GITHUB_ENV

      - name: Checkout gamedir
        uses: actions/checkout@v4
        with:
          repository: ${{ env.GAMEDIR_REPOSITORY }}
          ref: ${{ env.GAMEDIR_COMMIT_SHA }}
          path: gamedir

      - name: Checkout gamedir-languages
        if: inputs.language != 'English'
        uses: actions/checkout@v4
        with:
          repository: ${{ env.GAMEDIR_LANGUAGES_REPOSITORY }}
          ref: ${{ env.GAMEDIR_LANGUAGES_COMMIT_SHA }}
          path: gamedir-languages

      - name: Copy gamedir-languages files to gamedir
        if: inputs.language != 'English'
        shell: bash
        run: |
          set -eux
          cp -a gamedir-languages/${{ inputs.language }}_Version/* gamedir/

      - name: Download ja2
        uses: actions/download-artifact@v4
        with:
          name: ${{ inputs.language }}_ja2
          path: artifacts/ja2

      - name: Download ja2mapeditor
        uses: actions/download-artifact@v4
        with:
          name: ${{ inputs.language }}_ja2mapeditor
          path: artifacts/ja2mapeditor

      - name: Download ja2ub
        uses: actions/download-artifact@v4
        with:
          name: ${{ inputs.language }}_ja2ub
          path: artifacts/ja2ub

      - name: Download ja2ubmapeditor
        uses: actions/download-artifact@v4
        with:
          name: ${{ inputs.language }}_ja2ubmapeditor
          path: artifacts/ja2ubmapeditor

      - name: Copy application files to gamedir
        shell: bash
        run: |
          set -eux
          for APP in ja2 ja2mapeditor ja2ub ja2ubmapeditor
          do
            mv artifacts/${APP}/*.exe gamedir/${APP}.exe
          done

      - name: Create version information file
        shell: bash
        run: |
          set -eux
          
          # "-" separates words, "_" combines words, see double-click behavior
          DIST_PREFIX='JA2_113'
          DIST_NAME="${DIST_PREFIX}-${GAME_VERSION}-G${GAMEDIR_COMMIT_SHA:0:4}L${GAMEDIR_LANGUAGES_COMMIT_SHA:0:4}-${{ inputs.language }}"
          echo "DIST_NAME=$DIST_NAME" >> $GITHUB_ENV
          
          echo "If you encounter problems during gameplay, please provide the following version information:
          Distribution Name: $DIST_NAME
          Game Version: $GAME_VERSION
          Language: ${{ inputs.language }}
          Build Information: $GAME_BUILD_INFORMATION
          Source Repository: $GITHUB_REPOSITORY
          Source Commit SHA: $GITHUB_SHA
          Source Commit Date: $SOURCE_COMMIT_DATETIME
          Gamedir Repository: $GAMEDIR_REPOSITORY
          Gamedir Commit SHA: $GAMEDIR_COMMIT_SHA
          Gamedir Commit Date: $GAMEDIR_COMMIT_DATETIME
          Gamedir Languages Repository: $GAMEDIR_LANGUAGES_REPOSITORY
          Gamedir Languages Commit SHA: $GAMEDIR_LANGUAGES_COMMIT_SHA
          Gamedir Languages Commit Date: $GAMEDIR_LANGUAGES_COMMIT_DATETIME
          " > "gamedir/${DIST_PREFIX}-Version.txt"
          cat "gamedir/${DIST_PREFIX}-Version.txt"

      - name: Create release archive
        shell: bash
        run: |
          set -eux
          mkdir dist/
          cd gamedir/
          7z a -bb -xr'!.*' "../dist/${DIST_NAME}.7z" .

      - name: Upload
        uses: actions/upload-artifact@v4
        with:
          name: ${{ inputs.language }}_release
          path: dist/
          compression-level: 0
