# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json

name: Publish release

on:
  workflow_call:
    inputs:
      target:
        description: "The git ref to checkout, build from and release"
        required: true
        type: string
      tag:
        description: "The tag of the release"
        required: true
        type: string
      prerelease:
        description: "Prerelease"
        required: false
        default: false
        type: boolean

env:
  CI_DIR: 2049ef39-42a2-46d2-b513-ee6d2e3a7b15
  RELEASES: |
    windows:client:Windows/Client
    windows:server:Windows/Server
    linux:client:Linux/Client
    linux:server:Linux/Server
    mac:client:Mac/Client/Barotrauma.app/Contents/MacOS
    mac:server:Mac/Server
  ARCHIVE_BASE_NAME: luacsforbarotrauma
  # XXX: these file names are subject to shell expansion.
  # Be careful when using special characters.
  ARCHIVE_FILES_SERVER: |
    DedicatedServer.deps.json
    DedicatedServer.dll
    DedicatedServer.pdb
    Publicized/DedicatedServer.dll
  ARCHIVE_FILES_CLIENT: |
    Barotrauma.deps.json
    Barotrauma.dll
    Barotrauma.pdb
    DedicatedServer.deps.json
    DedicatedServer.dll
    DedicatedServer.pdb
    Publicized/Barotrauma.dll
    Publicized/DedicatedServer.dll
  ARCHIVE_FILES_SHARED: |
    0Harmony.dll
    Sigil.dll
    MoonSharp.Interpreter.dll
    MoonSharp.VsCodeDebugger.dll
    MonoMod.Common.dll
    Mono.Cecil.dll
    Mono.Cecil.Mdb.dll
    Mono.Cecil.Pdb.dll
    Mono.Cecil.Rocks.dll
    Microsoft.CodeAnalysis.CSharp.Scripting.dll
    Microsoft.CodeAnalysis.CSharp.dll
    Microsoft.CodeAnalysis.dll
    Microsoft.CodeAnalysis.Scripting.dll
    System.Collections.Immutable.dll
    System.Reflection.Metadata.dll
    System.Runtime.CompilerServices.Unsafe.dll
    mscordaccore_amd64_amd64_*
    Lua

jobs:
  build:
    uses: ./.github/workflows/build.yml
    with:
      target: ${{ inputs.target }}

  publish-release:
    runs-on: ubuntu-latest
    needs: [build]
    steps:
      - name: Download build artifacts
        uses: actions/download-artifact@9bc31d5ccc31df68ecc42ccf4149144866c47d8a # v3.0.2
        with:
          name: build
          path: ${{ env.CI_DIR }}

      - name: Extract build artifacts
        run: |
          artifacts_dir="$(realpath -m "$CI_DIR/artifacts")"
          mkdir -p "$artifacts_dir"
          tar -xzf "$CI_DIR/build.tar.gz" -C "$artifacts_dir"
          rm "$CI_DIR/build.tar.gz"

      - name: Create archives
        run: |
          set -e
          shopt -s globstar nullglob
          shopt -u dotglob

          # This converts a newline-separated (LF) list into a Bash array
          # NOTE: this doesn't discard the trailing LF that GitHub actions
          # append (which results in an extra entry in the array).
          lines_to_array() {
            IFS=$'\n' readarray -td $'\n' "$1" <<< "${!1}"
          }

          lines_to_array ARCHIVE_FILES_SHARED
          lines_to_array ARCHIVE_FILES_CLIENT
          lines_to_array ARCHIVE_FILES_SERVER
          lines_to_array RELEASES

          artifacts_dir="$(realpath -m "$CI_DIR/artifacts")"
          mkdir -p "$artifacts_dir"

          archives_dir="$(realpath -m "$CI_DIR/archives")"
          mkdir -p "$archives_dir"

          for i in "${!RELEASES[@]}"; do
            [[ -z "${RELEASES[i]}" ]] && continue
            (
              IFS=':' read platform side publish_dir _rest <<< "${RELEASES[i]}"
              cd "${artifacts_dir}/${publish_dir}"

              echo "Creating build_${platform}_${side}.zip"
              zip --must-match -qr "${archives_dir}/${ARCHIVE_BASE_NAME}_build_${platform}_${side}.zip" *

              echo "Creating build_${platform}_${side}.tar.gz"
              tar -czf "${archives_dir}/${ARCHIVE_BASE_NAME}_build_${platform}_${side}.tar.gz" \
                --owner=0 --group=0 \
                *

              if [[ "$side" == "client" ]]; then
                files=(
                  ${ARCHIVE_FILES_SHARED[@]}
                  ${ARCHIVE_FILES_CLIENT[@]}
                )
              elif [[ "$side" == "server" ]]; then
                files=(
                  ${ARCHIVE_FILES_SHARED[@]}
                  ${ARCHIVE_FILES_SERVER[@]}
                )
              else
                echo "Invalid side: $side"
                exit 1
              fi

              echo "Creating patch_${platform}_${side}.zip"
              zip \
                --must-match \
                -qr \
                "${archives_dir}/${ARCHIVE_BASE_NAME}_patch_${platform}_${side}.zip" \
                "${files[@]}"

              echo "Creating patch_${platform}_${side}.tar.gz"
              tar \
                -zcf "${archives_dir}/${ARCHIVE_BASE_NAME}_patch_${platform}_${side}.tar.gz" \
                --owner=0 --group=0 \
                "${files[@]}"
            )
          done

      - name: Publish release
        uses: notpeelz/action-gh-create-release@a12edfc71daf5daa7922b931c28e2bf88d3b2ced # v5.0.0
        with:
          target: ${{ inputs.target }}
          tag: ${{ inputs.tag }}
          prerelease: ${{ inputs.prerelease }}
          strategy: replace
          title: "Automatic build"
          body: "Automatic build"
          files: |
            ${{ env.CI_DIR }}/archives/${{ env.ARCHIVE_BASE_NAME }}_{build,patch}_{windows,linux,mac}_{client,server}.zip
            ${{ env.CI_DIR }}/archives/${{ env.ARCHIVE_BASE_NAME }}_{build,patch}_linux_{client,server}.tar.gz
