name: Update AUR Package

on:
  workflow_run:
    workflows: ["Build Tauri Clients"]
    types:
      - completed
  workflow_dispatch:
    inputs:
      version:
        description: "Version tag (e.g. 0.6.6)"
        required: true

jobs:
  update-aur-package:
    if: ${{ github.event.workflow_run.conclusion == 'success' || github.event_name == 'workflow_dispatch' }}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set version
        run: |
          if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
            echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_ENV
          else
            # Extract version from the triggering release
            RELEASE_TAG=$(curl -s "https://api.github.com/repos/${{ github.repository }}/releases/latest" | jq -r .tag_name)
            echo "VERSION=$RELEASE_TAG" >> $GITHUB_ENV
          fi

      - name: Generate PKGBUILD
        run: |
          # Calculate checksums for both architectures
          x86_64_url="https://github.com/madeofpendletonwool/PinePods/releases/download/$VERSION/Pinepods_${VERSION}_amd64.deb"
          aarch64_url="https://github.com/madeofpendletonwool/PinePods/releases/download/$VERSION/Pinepods_${VERSION}_arm64.deb"

          echo "Downloading and calculating checksums..."
          curl -L "$x86_64_url" -o x86_64.deb
          curl -L "$aarch64_url" -o aarch64.deb

          x86_64_sum=$(sha256sum x86_64.deb | cut -d' ' -f1)
          aarch64_sum=$(sha256sum aarch64.deb | cut -d' ' -f1)

          cat > PKGBUILD << EOF
          pkgname=pinepods
          pkgver=$VERSION
          pkgrel=1
          pkgdesc="Pinepods is a complete podcast management system and allows you to play, download, and keep track of podcasts you enjoy. All self hosted and enjoyed on your own server!"
          arch=('x86_64' 'aarch64')
          url="https://github.com/madeofpendletonwool/PinePods"
          license=('gpl3')
          depends=('cairo' 'desktop-file-utils' 'gdk-pixbuf2' 'glib2' 'gtk3' 'hicolor-icon-theme' 'libsoup' 'pango' 'webkit2gtk')
          options=('!strip' '!emptydirs')
          source_x86_64=("https://github.com/madeofpendletonwool/PinePods/releases/download/\${pkgver}/Pinepods_\${pkgver}_amd64.deb")
          source_aarch64=("https://github.com/madeofpendletonwool/PinePods/releases/download/\${pkgver}/Pinepods_\${pkgver}_arm64.deb")
          sha256sums_x86_64=('$x86_64_sum')
          sha256sums_aarch64=('$aarch64_sum')

          package() {
            # Extract the .deb package
            cd "\$srcdir"
            tar xf data.tar.gz -C "\$pkgdir/"

            # Create symlink from /usr/bin/app to /usr/bin/pinepods
            ln -s /usr/bin/app "\$pkgdir/usr/bin/pinepods"

            # Ensure correct permissions
            chmod 755 "\$pkgdir/usr/bin/app"
            chmod 644 "\$pkgdir/usr/share/applications/Pinepods.desktop"
            find "\$pkgdir/usr/share/icons" -type f -exec chmod 644 {} +
            find "\$pkgdir" -type d -exec chmod 755 {} +
          }
          EOF

      - name: Test PKGBUILD
        uses: KSXGitHub/github-actions-deploy-aur@v3.0.1
        with:
          pkgname: pinepods
          pkgbuild: ./PKGBUILD
          test: true
          commit_username: ${{ secrets.GIT_USER }}
          commit_email: ${{ secrets.GIT_EMAIL }}
          ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }}
          commit_message: "Update to version ${{ env.VERSION }}"
          ssh_keyscan_types: rsa,ecdsa,ed25519

      - name: Publish AUR package
        if: success()
        uses: KSXGitHub/github-actions-deploy-aur@v3.0.1
        with:
          pkgname: pinepods
          pkgbuild: ./PKGBUILD
          commit_username: ${{ secrets.GIT_USER }}
          commit_email: ${{ secrets.GIT_EMAIL }}
          ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }}
          commit_message: "Update to version ${{ env.VERSION }}"
          ssh_keyscan_types: rsa,ecdsa,ed25519
