name: Tagged Release Build

on:
  push:
    tags:
      - "*"
  workflow_dispatch:

permissions:
  contents: write

env:
  FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true

jobs:
  build-windows-zip:
    name: Build Windows ZIP
    runs-on: windows-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4.1.7

      - name: Set up Java 17
        uses: actions/setup-java@v4.1.0
        with:
          distribution: temurin
          java-version: "17"

      - name: Install WiX Toolset
        shell: pwsh
        run: choco install wixtoolset --no-progress -y

      - name: Build JAR and copy runtime dependencies
        shell: pwsh
        run: |
          .\mvnw.cmd -B -DskipTests package dependency:copy-dependencies -DincludeScope=runtime -DoutputDirectory=target/dist-libs
          $jar = Get-ChildItem -Path target -Filter 'praktiKST-*.jar' | Sort-Object LastWriteTime -Descending | Select-Object -First 1
          if (-not $jar) {
            throw "No project JAR produced"
          }
          Copy-Item $jar.FullName target/dist-libs/app.jar

      - name: Build app-image with jpackage
        shell: pwsh
        run: |
          New-Item -ItemType Directory -Force -Path dist | Out-Null
          jpackage --type app-image --name praktiKST --input target/dist-libs --main-jar app.jar --main-class kst4contest.view.Kst4ContestApplication --module-path target/dist-libs --add-modules javafx.controls,javafx.graphics,javafx.fxml,javafx.web,javafx.media,java.sql --dest dist

      - name: Create Windows ZIP
        shell: pwsh
        run: |
          if (-not (Test-Path dist/praktiKST)) {
            throw "No Windows app-image produced by jpackage"
          }
          Compress-Archive -Path dist/praktiKST -DestinationPath dist/praktiKST-${{ github.ref_name }}-windows-x64.zip -Force

      - name: Upload Windows artifact
        uses: actions/upload-artifact@v4.3.4
        with:
          name: windows-zip
          path: dist/praktiKST-${{ github.ref_name }}-windows-x64.zip

  build-linux-appimage:
    name: Build Linux AppImage
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4.1.7

      - name: Set up Java 17
        uses: actions/setup-java@v4.1.0
        with:
          distribution: temurin
          java-version: "17"

      - name: Ensure mvnw is executable
        run: chmod +x mvnw

      - name: Build JAR and copy runtime dependencies
        run: |
          ./mvnw -B -DskipTests package dependency:copy-dependencies -DincludeScope=runtime -DoutputDirectory=target/dist-libs
          cp "$(ls -t target/praktiKST-*.jar | head -n 1)" target/dist-libs/app.jar

      - name: Build app-image with jpackage
        run: |
          mkdir -p dist
          jpackage \
            --type app-image \
            --name praktiKST \
            --input target/dist-libs \
            --main-jar app.jar \
            --main-class kst4contest.view.Kst4ContestApplication \
            --module-path target/dist-libs \
            --add-modules javafx.controls,javafx.graphics,javafx.fxml,javafx.web,javafx.media,java.sql \
            --dest dist

      - name: Create AppDir metadata
        run: |
          rm -rf target/praktiKST.AppDir
          cp -a dist/praktiKST target/praktiKST.AppDir

          cat > target/praktiKST.AppDir/AppRun << 'EOF'
          #!/bin/sh
          HERE="$(dirname "$(readlink -f "$0")")"
          exec "$HERE/bin/praktiKST" "$@"
          EOF
          chmod +x target/praktiKST.AppDir/AppRun

          cat > target/praktiKST.AppDir/praktiKST.desktop << 'EOF'
          [Desktop Entry]
          Type=Application
          Name=praktiKST
          Exec=praktiKST
          Icon=praktiKST
          Categories=Network;HamRadio;
          Terminal=false
          EOF

          if [ -f target/praktiKST.AppDir/lib/praktiKST.png ]; then
            cp target/praktiKST.AppDir/lib/praktiKST.png target/praktiKST.AppDir/praktiKST.png
          fi

      - name: Build AppImage
        run: |
          wget -q -O target/appimagetool.AppImage https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-x86_64.AppImage
          chmod +x target/appimagetool.AppImage
          APPIMAGE_EXTRACT_AND_RUN=1 ARCH=x86_64 target/appimagetool.AppImage target/praktiKST.AppDir dist/praktiKST-${{ github.ref_name }}-linux-x86_64.AppImage

      - name: Upload Linux artifact
        uses: actions/upload-artifact@v4.3.4
        with:
          name: linux-appimage
          path: dist/praktiKST-${{ github.ref_name }}-linux-x86_64.AppImage

  build-docs-pdf:
    name: Build Documentation PDF
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4.1.7

      - name: Install pandoc and LaTeX toolchain
        run: |
          sudo apt-get update -qq
          sudo apt-get install -y --no-install-recommends \
            pandoc \
            texlive-xetex \
            texlive-fonts-recommended \
            texlive-latex-extra \
            texlive-plain-generic

      - name: Build English PDF manual
        run: |
          mkdir -p dist
          pandoc \
            --from=markdown-yaml_metadata_block \
            --template=.github/latex-manual/manual-template.tex \
            --pdf-engine=xelatex \
            --lua-filter=.github/latex-manual/strip-wiki-links.lua \
            --resource-path=.:github_docs \
            --listings \
            --toc \
            --toc-depth=3 \
            -V title="User Manual" \
            -V polyglossia-lang=english \
            -V version="${{ github.ref_name }}" \
            -o dist/KST4Contest-${{ github.ref_name }}-manual-en.pdf \
            github_docs/en-Home.md \
            github_docs/en-Installation.md \
            github_docs/en-Configuration.md \
            github_docs/en-Features.md \
            github_docs/en-User-Interface.md \
            github_docs/en-Macros-and-Variables.md \
            github_docs/en-Log-Sync.md \
            github_docs/en-AirScout-Integration.md \
            github_docs/en-DX-Cluster-Server.md \
            github_docs/en-Changelog.md

      - name: Build German PDF manual
        run: |
          pandoc \
            --from=markdown-yaml_metadata_block \
            --template=.github/latex-manual/manual-template.tex \
            --pdf-engine=xelatex \
            --lua-filter=.github/latex-manual/strip-wiki-links.lua \
            --resource-path=.:github_docs \
            --listings \
            --toc \
            --toc-depth=3 \
            -V title="Benutzerhandbuch" \
            -V polyglossia-lang=german \
            -V version="${{ github.ref_name }}" \
            -o dist/KST4Contest-${{ github.ref_name }}-manual-de.pdf \
            github_docs/de-Home.md \
            github_docs/de-Installation.md \
            github_docs/de-Konfiguration.md \
            github_docs/de-Funktionen.md \
            github_docs/de-Benutzeroberflaeche.md \
            github_docs/de-Makros-und-Variablen.md \
            github_docs/de-Log-Synchronisation.md \
            github_docs/de-AirScout-Integration.md \
            github_docs/de-DX-Cluster-Server.md \
            github_docs/de-Changelog.md

      - name: Upload PDF artifacts
        uses: actions/upload-artifact@v4.3.4
        with:
          name: docs-pdf
          path: dist/KST4Contest-${{ github.ref_name }}-manual-*.pdf

  release-tag:
    name: Publish Tagged Release
    runs-on: ubuntu-latest
    needs:
      - build-windows-zip
      - build-linux-appimage
      - build-docs-pdf

    steps:
      - name: Download Windows artifact
        uses: actions/download-artifact@v4.1.3
        with:
          name: windows-zip
          path: release-assets/windows

      - name: Download Linux artifact
        uses: actions/download-artifact@v4.1.3
        with:
          name: linux-appimage
          path: release-assets/linux

      - name: Download PDF manuals
        uses: actions/download-artifact@v4.1.3
        with:
          name: docs-pdf
          path: release-assets/docs

      - name: Create tagged release
        uses: ncipollo/release-action@v1
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          tag: ${{ github.ref_name }}
          name: ${{ startsWith(github.ref_name, 'beta-') && format('Beta {0}', github.ref_name) || format('Release {0}', github.ref_name) }}
          prerelease: ${{ startsWith(github.ref_name, 'beta-') }}
          allowUpdates: false
          replacesArtifacts: false
          makeLatest: ${{ !startsWith(github.ref_name, 'beta-') }}
          generateReleaseNotes: true
          artifacts: >-
            release-assets/windows/praktiKST-${{ github.ref_name }}-windows-x64.zip,
            release-assets/linux/praktiKST-${{ github.ref_name }}-linux-x86_64.AppImage,
            release-assets/docs/KST4Contest-${{ github.ref_name }}-manual-en.pdf,
            release-assets/docs/KST4Contest-${{ github.ref_name }}-manual-de.pdf
