name: Build raveos packages and publish to raveos-core-repo (SAFE)

on:
  push:
    branches: ["main"]
  workflow_dispatch:

concurrency:
  group: raveos-theme-module-build-publish
  cancel-in-progress: true

jobs:
  build:
    runs-on: iso-builder

    steps:
      - name: Checkout PKGBUILDs repo
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Install build tools
        run: |
          set -euo pipefail
          sudo pacman -S --needed --noconfirm \
            base-devel git pacman-contrib cmake qt6-tools go

      - name: Clone core repo
        run: |
          set -euo pipefail
          rm -rf /tmp/raveos-core-repo
          git clone --depth 1 git@git.rp1.hu:RaveOS/raveos-core-repo.git /tmp/raveos-core-repo
          mkdir -p /tmp/builtpkgs

      - name: Import GPG signing key
        run: |
          set -euo pipefail
          export GNUPGHOME=/tmp/raveos-gnupg
          rm -rf "$GNUPGHOME"
          mkdir -m 700 "$GNUPGHOME"
          echo "${{ secrets.GPG_PRIVATE_KEY }}" | base64 -d | gpg --batch --import

      - name: Build raveos-calamares-theme
        run: |
          set -euo pipefail
          CORE="/tmp/raveos-core-repo/x86_64"
          cd raveos-calamares-theme
          PKGVER=$(grep '^pkgver=' PKGBUILD | cut -d= -f2)
          PKGREL=$(grep '^pkgrel=' PKGBUILD | cut -d= -f2)
          if ls "$CORE/raveos-calamares-theme-${PKGVER}-${PKGREL}-"*.pkg.tar.zst 2>/dev/null | grep -q .; then
            echo "raveos-calamares-theme ${PKGVER}-${PKGREL} unchanged -> skip"
            exit 0
          fi
          rm -f *.pkg.tar.zst || true
          makepkg -s --noconfirm -f
          cp -v raveos-calamares-theme-*.pkg.tar.zst /tmp/builtpkgs/

      - name: Build raveos-calamares-module
        run: |
          set -euo pipefail
          CORE="/tmp/raveos-core-repo/x86_64"
          cd raveos-calamares-module
          PKGVER=$(grep '^pkgver=' PKGBUILD | cut -d= -f2)
          PKGREL=$(grep '^pkgrel=' PKGBUILD | cut -d= -f2)
          if ls "$CORE/raveos-calamares-module-${PKGVER}-${PKGREL}-"*.pkg.tar.zst 2>/dev/null | grep -q .; then
            echo "raveos-calamares-module ${PKGVER}-${PKGREL} unchanged -> skip"
            exit 0
          fi
          rm -f *.pkg.tar.zst || true
          makepkg -s --noconfirm -f
          cp -v raveos-calamares-module-*.pkg.tar.zst /tmp/builtpkgs/

      - name: Build raveos-calamares
        run: |
          set -euo pipefail
          CORE="/tmp/raveos-core-repo/x86_64"
          cd raveos-calamares
          PKGVER=$(grep '^pkgver=' PKGBUILD | cut -d= -f2)
          PKGREL=$(grep '^pkgrel=' PKGBUILD | cut -d= -f2)
          if ls "$CORE/calamares-${PKGVER}-${PKGREL}-"*.pkg.tar.zst 2>/dev/null | grep -q .; then
            echo "raveos-calamares ${PKGVER}-${PKGREL} unchanged -> skip"
            exit 0
          fi
          rm -f *.pkg.tar.zst || true
          makepkg -s --noconfirm -f
          PKG=$(ls -t *.pkg.tar.zst | grep -v -- '-debug-' | head -n 1)
          cp -v "$PKG" /tmp/builtpkgs/

      - name: Build raveos-keyring
        run: |
          set -euo pipefail
          CORE="/tmp/raveos-core-repo/x86_64"
          cd raveos-keyring
          PKGVER=$(grep '^pkgver=' PKGBUILD | cut -d= -f2)
          PKGREL=$(grep '^pkgrel=' PKGBUILD | cut -d= -f2)
          if ls "$CORE/raveos-keyring-${PKGVER}-${PKGREL}-"*.pkg.tar.zst 2>/dev/null | grep -q .; then
            echo "raveos-keyring ${PKGVER}-${PKGREL} unchanged -> skip"
            exit 0
          fi
          rm -rf build || true
          makepkg -s --noconfirm -f
          cp -v build/raveos-keyring-*.pkg.tar.zst /tmp/builtpkgs/

      - name: Build raveos-sddm-theme
        run: |
          set -euo pipefail
          CORE="/tmp/raveos-core-repo/x86_64"
          cd raveos-sddm-theme
          PKGVER=$(grep '^pkgver=' PKGBUILD | cut -d= -f2)
          PKGREL=$(grep '^pkgrel=' PKGBUILD | cut -d= -f2)
          if ls "$CORE/raveos-sddm-theme-${PKGVER}-${PKGREL}-"*.pkg.tar.zst 2>/dev/null | grep -q .; then
            echo "raveos-sddm-theme ${PKGVER}-${PKGREL} unchanged -> skip"
            exit 0
          fi
          rm -rf build || true
          makepkg -s --noconfirm -f
          cp -v build/raveos-sddm-theme-*.pkg.tar.zst /tmp/builtpkgs/
          # Azonnal telepitjuk is, mert meg nincs publikalva egy repoban sem
          # (a "Publish" lepes csak a legvegen fut) - a tobbi DE-tema csomag
          # makepkg -s hivasa igy fogja tudni feloldani a fuggoseget.
          sudo pacman -U --noconfirm build/raveos-sddm-theme-*.pkg.tar.zst

      - name: Build raveos-plasma-theme
        run: |
          set -euo pipefail
          CORE="/tmp/raveos-core-repo/x86_64"
          cd raveos-plasma-theme
          PKGVER=$(grep '^pkgver=' PKGBUILD | cut -d= -f2)
          PKGREL=$(grep '^pkgrel=' PKGBUILD | cut -d= -f2)
          if ls "$CORE/raveos-plasma-theme-${PKGVER}-${PKGREL}-"*.pkg.tar.zst 2>/dev/null | grep -q .; then
            echo "raveos-plasma-theme ${PKGVER}-${PKGREL} unchanged -> skip"
            exit 0
          fi
          rm -rf build || true
          makepkg -s --noconfirm -f
          cp -v build/raveos-plasma-theme-*.pkg.tar.zst /tmp/builtpkgs/

      - name: Build raveos-gnome-theme
        run: |
          set -euo pipefail
          CORE="/tmp/raveos-core-repo/x86_64"
          cd raveos-gnome-theme
          PKGVER=$(grep '^pkgver=' PKGBUILD | cut -d= -f2)
          PKGREL=$(grep '^pkgrel=' PKGBUILD | cut -d= -f2)
          if ls "$CORE/raveos-gnome-theme-${PKGVER}-${PKGREL}-"*.pkg.tar.zst 2>/dev/null | grep -q .; then
            echo "raveos-gnome-theme ${PKGVER}-${PKGREL} unchanged -> skip"
            exit 0
          fi
          rm -rf build || true
          makepkg -s --noconfirm -f
          cp -v build/raveos-gnome-theme-*.pkg.tar.zst /tmp/builtpkgs/

      - name: Build raveos-hyprland-theme
        run: |
          set -euo pipefail
          CORE="/tmp/raveos-core-repo/x86_64"
          cd raveos-hyprland-theme
          PKGVER=$(grep '^pkgver=' PKGBUILD | cut -d= -f2)
          PKGREL=$(grep '^pkgrel=' PKGBUILD | cut -d= -f2)
          if ls "$CORE/raveos-hyprland-theme-${PKGVER}-${PKGREL}-"*.pkg.tar.zst 2>/dev/null | grep -q .; then
            echo "raveos-hyprland-theme ${PKGVER}-${PKGREL} unchanged -> skip"
            exit 0
          fi
          rm -rf build || true
          makepkg -s --noconfirm -f
          cp -v build/raveos-hyprland-theme-*.pkg.tar.zst /tmp/builtpkgs/
          rm -f /tmp/builtpkgs/raveos-hyprland-theme-debug-*.pkg.tar.zst || true

      - name: Build raveos-cosmic-theme
        run: |
          set -euo pipefail
          CORE="/tmp/raveos-core-repo/x86_64"
          cd raveos-cosmic-theme
          PKGVER=$(grep '^pkgver=' PKGBUILD | cut -d= -f2)
          PKGREL=$(grep '^pkgrel=' PKGBUILD | cut -d= -f2)
          if ls "$CORE/raveos-cosmic-theme-${PKGVER}-${PKGREL}-"*.pkg.tar.zst 2>/dev/null | grep -q .; then
            echo "raveos-cosmic-theme ${PKGVER}-${PKGREL} unchanged -> skip"
            exit 0
          fi
          rm -rf build || true
          makepkg -s --noconfirm -f
          cp -v build/raveos-cosmic-theme-*.pkg.tar.zst /tmp/builtpkgs/

      - name: Build raveos-tools
        run: |
          set -euo pipefail
          CORE="/tmp/raveos-core-repo/x86_64"
          cd raveos-tools
          PKGVER=$(grep '^pkgver=' PKGBUILD | cut -d= -f2)
          PKGREL=$(grep '^pkgrel=' PKGBUILD | cut -d= -f2)
          if ls "$CORE/raveos-tools-${PKGVER}-${PKGREL}-"*.pkg.tar.zst 2>/dev/null | grep -q .; then
            echo "raveos-tools ${PKGVER}-${PKGREL} unchanged -> skip"
            exit 0
          fi
          rm -rf build || true
          makepkg -s --noconfirm -f
          cp -v build/raveos-tools-*.pkg.tar.zst /tmp/builtpkgs/

      - name: Build raveos-app-installer
        run: |
          set -euo pipefail
          CORE="/tmp/raveos-core-repo/x86_64"
          cd raveos-app-installer
          PKGVER=$(grep '^pkgver=' PKGBUILD | cut -d= -f2)
          PKGREL=$(grep '^pkgrel=' PKGBUILD | cut -d= -f2)
          if ls "$CORE/raveos-app-installer-${PKGVER}-${PKGREL}-"*.pkg.tar.zst 2>/dev/null | grep -q .; then
            echo "raveos-app-installer ${PKGVER}-${PKGREL} unchanged -> skip"
            exit 0
          fi
          rm -rf build || true
          makepkg -s --noconfirm -f
          cp -v build/raveos-app-installer-*.pkg.tar.zst /tmp/builtpkgs/

      - name: Build raveos-welcome
        run: |
          set -euo pipefail
          CORE="/tmp/raveos-core-repo/x86_64"
          cd raveos-welcome
          PKGVER=$(grep '^pkgver=' PKGBUILD | cut -d= -f2)
          PKGREL=$(grep '^pkgrel=' PKGBUILD | cut -d= -f2)
          if ls "$CORE/raveos-welcome-${PKGVER}-${PKGREL}-"*.pkg.tar.zst 2>/dev/null | grep -q .; then
            echo "raveos-welcome ${PKGVER}-${PKGREL} unchanged -> skip"
            exit 0
          fi
          rm -rf build || true
          makepkg -s --noconfirm -f
          cp -v build/raveos-welcome-*.pkg.tar.zst /tmp/builtpkgs/

      - name: Sign built packages
        run: |
          set -euo pipefail
          export GNUPGHOME=/tmp/raveos-gnupg
          BUILT=$(ls /tmp/builtpkgs/*.pkg.tar.zst 2>/dev/null || true)
          if [ -z "$BUILT" ]; then
            echo "Nincs csomag alairashoz -> kihagyva"
            exit 0
          fi
          for pkg in $BUILT; do
            gpg --batch --yes --pinentry-mode loopback \
                --passphrase "${{ secrets.GPG_PRIVATE_KEY_PASSPHRASE }}" \
                --local-user 25C7668F1235F5664F037671BDB2A9CE6EBFF113 \
                --detach-sign "$pkg"
            echo "Alairva: $(basename "$pkg")"
          done

      - name: Publish packages to raveos-core-repo (only if changed)
        run: |
          set -euo pipefail

          CORE="/tmp/raveos-core-repo/x86_64"
          mkdir -p "$CORE"

          BUILT=$(ls /tmp/builtpkgs/*.pkg.tar.zst 2>/dev/null || true)
          if [ -z "$BUILT" ]; then
            echo "No packages built -> nothing to publish"
            exit 0
          fi

          cd /tmp/raveos-core-repo
          git config user.name "Forgejo Actions"
          git config user.email "actions@forgejo.local"

          for pkg in $BUILT; do
            PKGFILE=$(basename "$pkg")
            PKGNAME=$(echo "$PKGFILE" | sed 's/-[0-9][^-]*-[^-]*-[^-]*\.pkg\.tar\.zst$//')
            if [[ ! "$PKGNAME" =~ ^[a-zA-Z0-9_+-]+$ ]]; then
              echo "HIBA: Ervenytelen csomagnev: $PKGNAME -> skip"
              continue
            fi
            rm -f "$CORE/${PKGNAME}-"*.pkg.tar.zst* 2>/dev/null || true
            cp -v "$pkg" "$CORE/"
            [ -f "$pkg.sig" ] && cp -v "$pkg.sig" "$CORE/"
          done

          git add -A
          git commit -m "Publish packages ${GITHUB_SHA:0:7}"
          git push origin main

          TAG="vrepo-$(date +%Y%m%d-%H%M%S)"
          git tag -a "$TAG" -m "Trigger repo-add (${GITHUB_SHA:0:7})"
          git push origin "$TAG"

          echo "::notice title=Published packages::Tag $TAG"

      - name: Cleanup (SAFE)
        if: always()
        run: |
          gpgconf --homedir /tmp/raveos-gnupg --kill gpg-agent || true
          sudo rm -rf /tmp/raveos-gnupg || true
          sudo rm -rf /tmp/builtpkgs || true
          sudo rm -rf /tmp/raveos-core-repo || true
          sudo rm -rf /var/cache/pacman/pkg/* || true
          df -h
