name: Build

on:
  push:
    branches: [ master ]
    tags:
      - 'v[0-9]+.[0-9]+.[0-9]+'
  pull_request:
    branches: [ master ]
  workflow_dispatch:

jobs:
  flatpak:
    name: Build Flatpak
    runs-on: ubuntu-latest
    permissions:
      contents: read
    container:
      image: ghcr.io/flathub-infra/flatpak-github-actions:kde-6.10
      options: --privileged
    steps:
      - uses: actions/checkout@v6

      - name: Determine version
        id: version
        run: |
          if echo "${{ github.ref }}" | grep -qE '^refs/tags/v[0-9]+\.[0-9]+\.[0-9]+$'; then
            VER="${{ github.ref_name }}"
            VER="${VER#v}"
            MAJOR=$(echo $VER | cut -d. -f1)
            MINOR=$(echo $VER | cut -d. -f2)
            PATCH=$(echo $VER | cut -d. -f3)
            echo "version=$VER"         >> "$GITHUB_OUTPUT"
            echo "is_release=true"      >> "$GITHUB_OUTPUT"
          else
            SHORT="${{ github.sha }}"
            SHORT="${SHORT:0:7}"
            MAJOR=0; MINOR=0; PATCH=0
            echo "version=git-${SHORT}" >> "$GITHUB_OUTPUT"
            echo "is_release=false"     >> "$GITHUB_OUTPUT"
          fi
          echo "major=$MAJOR"           >> "$GITHUB_OUTPUT"
          echo "minor=$MINOR"           >> "$GITHUB_OUTPUT"
          echo "patch=$PATCH"           >> "$GITHUB_OUTPUT"

      - name: Inject version into Flatpak manifest
        run: |
          python3 -c "
          import json
          m = json.load(open('com.github.fontmatrix.Fontmatrix.json'))
          m['modules'][0]['config-opts'] = [
            '-DVERSION_MAJOR=${{ steps.version.outputs.major }}',
            '-DVERSION_MINOR=${{ steps.version.outputs.minor }}',
            '-DVERSION_PATCH=${{ steps.version.outputs.patch }}',
          ]
          json.dump(m, open('com.github.fontmatrix.Fontmatrix.json', 'w'), indent=4)
          "

      - uses: flatpak/flatpak-github-actions/flatpak-builder@v6
        with:
          bundle: fontmatrix-${{ steps.version.outputs.version }}.flatpak
          manifest-path: com.github.fontmatrix.Fontmatrix.json
          cache-key: flatpak-builder-${{ github.sha }}

      - name: Upload Flatpak artifact
        uses: actions/upload-artifact@v7
        with:
          name: fontmatrix-flatpak
          path: fontmatrix-${{ steps.version.outputs.version }}.flatpak

  windows:
    name: Build Windows
    runs-on: windows-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@v6

      - name: Determine version
        id: version
        shell: bash
        run: |
          if echo "${{ github.ref }}" | grep -qE '^refs/tags/v[0-9]+\.[0-9]+\.[0-9]+$'; then
            VER="${{ github.ref_name }}"
            VER="${VER#v}"
            MAJOR=$(echo $VER | cut -d. -f1)
            MINOR=$(echo $VER | cut -d. -f2)
            PATCH=$(echo $VER | cut -d. -f3)
            echo "version=$VER"         >> "$GITHUB_OUTPUT"
            echo "is_release=true"      >> "$GITHUB_OUTPUT"
          else
            SHORT="${{ github.sha }}"
            SHORT="${SHORT:0:7}"
            MAJOR=0; MINOR=0; PATCH=0
            echo "version=git-${SHORT}" >> "$GITHUB_OUTPUT"
            echo "is_release=false"     >> "$GITHUB_OUTPUT"
          fi
          echo "major=$MAJOR"           >> "$GITHUB_OUTPUT"
          echo "minor=$MINOR"           >> "$GITHUB_OUTPUT"
          echo "patch=$PATCH"           >> "$GITHUB_OUTPUT"

      - name: Install Qt
        uses: jurplel/install-qt-action@v4
        with:
          version: '6.10.*'
          arch: win64_msvc2022_64
          modules: 'qtwebengine qtpositioning qtwebchannel'

      - name: Install FreeType via vcpkg
        run: vcpkg install freetype:x64-windows

      - name: Configure CMake
        run: >
          cmake -B build -DCMAKE_BUILD_TYPE=Release
          -DVERSION_MAJOR=${{ steps.version.outputs.major }}
          -DVERSION_MINOR=${{ steps.version.outputs.minor }}
          -DVERSION_PATCH=${{ steps.version.outputs.patch }}
          "-DCMAKE_TOOLCHAIN_FILE=$env:VCPKG_INSTALLATION_ROOT/scripts/buildsystems/vcpkg.cmake"

      - name: Build
        run: cmake --build build --config Release --parallel

      - name: Deploy
        shell: pwsh
        run: cmake --install build --config Release --prefix (Join-Path $PWD "dist")

      - name: Create ZIP archive
        shell: pwsh
        run: Compress-Archive -Path dist\* -DestinationPath fontmatrix-windows-${{ steps.version.outputs.version }}.zip

      - name: Install WiX toolset
        run: |
          dotnet tool install --global wix --version 6.0.2
          wix extension add --global WixToolset.UI.wixext/6.0.2

      - name: Build MSI installer
        id: msi
        shell: pwsh
        run: |
          cd build
          cpack -G WIX -C Release

      - name: Upload WiX log on failure
        if: failure()
        uses: actions/upload-artifact@v7
        with:
          name: wix-log
          path: build/_CPack_Packages/win64/WIX/wix.log
          if-no-files-found: ignore

      - name: Upload Windows ZIP artifact
        uses: actions/upload-artifact@v7
        with:
          name: fontmatrix-windows
          path: fontmatrix-windows-${{ steps.version.outputs.version }}.zip

      - name: Upload Windows MSI artifact
        uses: actions/upload-artifact@v7
        with:
          name: fontmatrix-windows-msi
          path: build/fontmatrix-*.msi

  release:
    name: Publish to Release
    needs: [ flatpak, windows ]

    if: startsWith(github.ref, 'refs/tags/v') && github.ref_type == 'tag'
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0

      - name: Extract version from tag
        id: version
        run: |
          VERSION="${{ github.ref_name }}"
          echo "version=${VERSION#v}" >> "$GITHUB_OUTPUT"

      - name: Check if tag commit is on master branch
        id: check-branch
        run: |
          git fetch origin master
          if git merge-base --is-ancestor "${{ github.sha }}" origin/master; then
            echo "on_master=true" >> "$GITHUB_OUTPUT"
          else
            echo "on_master=false" >> "$GITHUB_OUTPUT"
            echo "::warning::Tag commit is not on the master branch — skipping release upload."
          fi

      - name: Download Flatpak artifact
        if: steps.check-branch.outputs.on_master == 'true'
        uses: actions/download-artifact@v7
        with:
          name: fontmatrix-flatpak
          path: release-artifacts/

      - name: Download Windows ZIP artifact
        if: steps.check-branch.outputs.on_master == 'true'
        uses: actions/download-artifact@v7
        with:
          name: fontmatrix-windows
          path: release-artifacts/

      - name: Download Windows MSI artifact
        if: steps.check-branch.outputs.on_master == 'true'
        uses: actions/download-artifact@v7
        with:
          name: fontmatrix-windows-msi
          path: release-artifacts/

      - name: Upload artifacts to GitHub Release
        if: steps.check-branch.outputs.on_master == 'true'
        uses: softprops/action-gh-release@v2
        with:
          files: |
            release-artifacts/fontmatrix-*.flatpak
            release-artifacts/fontmatrix-windows-*.zip
            release-artifacts/*.msi
