name: Build Linux+Windows and Publish Release
on:
  workflow_dispatch:
    inputs:
      enable_linux_gcc:
        description: Enable Linux GCC build
        type: boolean
        default: true
      enable_windows_msvc:
        description: Enable Windows MSVC build
        type: boolean
        default: true
      enable_windows_gcc:
        description: Enable Windows GCC (MSYS2) build
        type: boolean
        default: false
      build_type:
        description: Build type (Release/Debug)
        type: string
        default: Release
      prerelease:
        description: Mark GitHub Release as prerelease
        type: boolean
        default: true
      version:
        description: Release version/tag (empty = short commit id)
        type: string
        default: ""
permissions:
  contents: write
jobs:
  build_linux:
    if: ${{ inputs.enable_linux_gcc }}
    uses: ./.github/workflows/linux-gcc.yml
    with:
      build_type: ${{ inputs.build_type || 'Release' }}
      publish_release: false
      prerelease: ${{ inputs.prerelease }}
      version: ${{ inputs.version }}
  build_windows:
    if: ${{ inputs.enable_windows_msvc }}
    uses: ./.github/workflows/windows-msvc.yml
    with:
      build_type: ${{ inputs.build_type || 'Release' }}
      publish_release: false
      prerelease: ${{ inputs.prerelease }}
      version: ${{ inputs.version }}
  build_windows_gcc:
    if: ${{ inputs.enable_windows_gcc }}
    uses: ./.github/workflows/windows-gcc.yml
    with:
      build_type: ${{ inputs.build_type || 'Release' }}
      publish_release: false
      prerelease: ${{ inputs.prerelease }}
      version: ${{ inputs.version }}
  publish:
    if: ${{ always() }}
    needs:
      - build_linux
      - build_windows
      - build_windows_gcc
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          fetch-depth: 1
      - name: Prepare release metadata
        id: meta
        shell: bash
        run: |
          short_sha="${GITHUB_SHA::7}"
          version="${{ inputs.version }}"
          if [ -z "$version" ]; then
            version="$short_sha"
          fi
          echo "short_sha=$short_sha" >> "$GITHUB_OUTPUT"
          echo "version=$version" >> "$GITHUB_OUTPUT"
          msg="$(git log -1 --pretty=%B)"
          {
            echo "message<<EOF"
            echo "$msg"
            echo "EOF"
          } >> "$GITHUB_OUTPUT"
      - name: Download build artifacts
        uses: actions/download-artifact@v7
      - name: Package artifacts
        id: package
        shell: bash
        run: |
          version="${{ steps.meta.outputs.version }}"
          build_type="${{ inputs.build_type || 'Release' }}"

          files=()

          if [ "${{ inputs.enable_linux_gcc }}" = "true" ]; then
            linux_dir="$(ls -d Linux-GCC-* | head -n 1)"
            test -n "$linux_dir"
            tar -czf "PrismLauncher-Linux-GCC-${version}-${build_type}.tar.gz" -C "$linux_dir" install
            tar -czf "PrismLauncher-Linux-GCC-Portable-${version}-${build_type}.tar.gz" -C "$linux_dir" install-portable
            files+=("PrismLauncher-Linux-GCC-${version}-${build_type}.tar.gz")
            files+=("PrismLauncher-Linux-GCC-Portable-${version}-${build_type}.tar.gz")
          fi

          if [ "${{ inputs.enable_windows_msvc }}" = "true" ]; then
            windows_dir="$(ls -d Windows-MSVC-* | head -n 1)"
            test -n "$windows_dir"
            (cd "$windows_dir" && zip -r -9 "../PrismLauncher-Windows-MSVC-${version}-${build_type}.zip" install)
            (cd "$windows_dir" && zip -r -9 "../PrismLauncher-Windows-MSVC-Portable-${version}-${build_type}.zip" install-portable)
            files+=("PrismLauncher-Windows-MSVC-${version}-${build_type}.zip")
            files+=("PrismLauncher-Windows-MSVC-Portable-${version}-${build_type}.zip")
          fi

          if [ "${{ inputs.enable_windows_gcc }}" = "true" ]; then
            windows_gcc_dir="$(ls -d Windows-GCC-* | head -n 1)"
            test -n "$windows_gcc_dir"
            (cd "$windows_gcc_dir" && zip -r -9 "../PrismLauncher-Windows-GCC-${version}-${build_type}.zip" install)
            (cd "$windows_gcc_dir" && zip -r -9 "../PrismLauncher-Windows-GCC-Portable-${version}-${build_type}.zip" install-portable)
            files+=("PrismLauncher-Windows-GCC-${version}-${build_type}.zip")
            files+=("PrismLauncher-Windows-GCC-Portable-${version}-${build_type}.zip")
          fi

          if [ "${#files[@]}" -eq 0 ]; then
            echo "No targets enabled; nothing to publish."
            exit 1
          fi

          {
            echo "files<<EOF"
            printf "%s\n" "${files[@]}"
            echo "EOF"
          } >> "$GITHUB_OUTPUT"
      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.meta.outputs.version }}
          target_commitish: ${{ github.sha }}
          name: Prism Launcher ${{ steps.meta.outputs.version }}
          body: ${{ steps.meta.outputs.message }}
          prerelease: ${{ inputs.prerelease }}
          files: ${{ steps.package.outputs.files }}
