name: Sync PKGBUILD version

on:
  pull_request_target:
    types:
      - opened
      - synchronize
      - reopened

jobs:
  sync:
    if: github.event.pull_request.head.repo.full_name == github.repository && startsWith(github.head_ref, 'release-plz-')
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout release PR branch
        uses: actions/checkout@v5
        with:
          repository: ${{ github.event.pull_request.head.repo.full_name }}
          ref: ${{ github.event.pull_request.head.ref }}
      - name: Configure git user
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
      - name: Sync PKGBUILD version
        run: |
          set -euo pipefail
          version="$(python3 - <<'PY'
          import sys
          
          try:
              import tomllib  # Python 3.11+
          except ModuleNotFoundError:  # pragma: no cover - fallback for older interpreters
              import tomli as tomllib
          
          with open("Cargo.toml", "rb") as f:
              data = tomllib.load(f)
          
          print(data["package"]["version"])
          PY
          )"
          pkgbuild_file="pkgbuild/ins/PKGBUILD"
          file_version="$(awk -F '=' '/^pkgver=/ {print $2; exit}' "$pkgbuild_file")"
          pkgrel="$(awk -F '=' '/^pkgrel=/ {print $2; exit}' "$pkgbuild_file")"
          if [[ "$file_version" != "$version" ]]; then
            sed -i "s/^pkgver=.*/pkgver=$version/" "$pkgbuild_file"
            sed -i "s/^pkgrel=.*/pkgrel=1/" "$pkgbuild_file"
          fi
          if ! git diff --quiet "$pkgbuild_file"; then
            srcinfo="$(
              docker run --rm -v "$PWD/pkgbuild/ins:/pkgbuild" archlinux:latest bash -lc '
                set -euo pipefail
                pacman -Sy --noconfirm pacman-contrib >/dev/null
                useradd -m builder
                install -d -m 755 /home/builder/work
                cp -a /pkgbuild/. /home/builder/work/
                chown -R builder:builder /home/builder/work
                su builder -c "cd /home/builder/work && makepkg --printsrcinfo"
              '
            )"
            printf '%s\n' "$srcinfo" > pkgbuild/ins/.SRCINFO
            git add "$pkgbuild_file" pkgbuild/ins/.SRCINFO
            git commit -m "chore: sync PKGBUILD version"
            git push
          fi
