# Publish to AUR after a release is created

name: Publish AUR Package

on:
  workflow_call:
    inputs:
      version:
        required: true
        type: string
  workflow_dispatch:
    inputs:
      version:
        description: 'Version to publish (e.g., 0.1.21)'
        required: true
        type: string

jobs:
  publish-aur-bin:
    runs-on: ubuntu-latest
    env:
      VERSION: ${{ inputs.version }}
    steps:
      - uses: actions/checkout@v6

      - name: Download linux tarballs from release
        id: checksum
        run: |
          mkdir -p dist-artifacts
          curl -fsSL "https://github.com/sinelaw/fresh/releases/download/v${VERSION}/fresh-editor-x86_64-unknown-linux-gnu.tar.xz" \
            -o dist-artifacts/fresh-editor-x86_64-unknown-linux-gnu.tar.xz
          curl -fsSL "https://github.com/sinelaw/fresh/releases/download/v${VERSION}/fresh-editor-aarch64-unknown-linux-gnu.tar.xz" \
            -o dist-artifacts/fresh-editor-aarch64-unknown-linux-gnu.tar.xz
          SHA256_X86_64=$(sha256sum dist-artifacts/fresh-editor-x86_64-unknown-linux-gnu.tar.xz | cut -d' ' -f1)
          SHA256_AARCH64=$(sha256sum dist-artifacts/fresh-editor-aarch64-unknown-linux-gnu.tar.xz | cut -d' ' -f1)
          echo "sha256_x86_64=$SHA256_X86_64" >> "$GITHUB_OUTPUT"
          echo "sha256_aarch64=$SHA256_AARCH64" >> "$GITHUB_OUTPUT"
          echo "Computed SHA256 (x86_64): $SHA256_X86_64"
          echo "Computed SHA256 (aarch64): $SHA256_AARCH64"

      - name: Generate PKGBUILD from template
        run: |
          mkdir -p aur-repo
          cp crates/fresh-editor/packaging/aur/fresh-editor-bin/PKGBUILD.template aur-repo/PKGBUILD
          sed -i "s/VERSION_PLACEHOLDER/${VERSION}/g" aur-repo/PKGBUILD
          sed -i "s/SHA256_X86_64_PLACEHOLDER/${{ steps.checksum.outputs.sha256_x86_64 }}/g" aur-repo/PKGBUILD
          sed -i "s/SHA256_AARCH64_PLACEHOLDER/${{ steps.checksum.outputs.sha256_aarch64 }}/g" aur-repo/PKGBUILD
          echo "Generated PKGBUILD:"
          cat aur-repo/PKGBUILD

      - name: Publish AUR package (fresh-editor-bin)
        uses: KSXGitHub/github-actions-deploy-aur@v4.1.3
        with:
          pkgname: fresh-editor-bin
          pkgbuild: ./aur-repo/PKGBUILD
          commit_username: ${{ secrets.AUR_USERNAME }}
          commit_email: ${{ secrets.AUR_EMAIL }}
          ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }}
          commit_message: "Update to ${{ inputs.version }}"
          ssh_keyscan_types: ed25519

  publish-aur-source:
    runs-on: ubuntu-latest
    env:
      VERSION: ${{ inputs.version }}
    steps:
      - uses: actions/checkout@v6

      - name: Download stable source tarball from release
        id: checksum
        run: |
          curl -sL "https://github.com/sinelaw/fresh/releases/download/v${VERSION}/fresh-editor-${VERSION}-source.tar.gz" -o source.tar.gz
          SHA256=$(sha256sum source.tar.gz | cut -d' ' -f1)
          echo "sha256=$SHA256" >> "$GITHUB_OUTPUT"
          echo "Computed SHA256: $SHA256"

      - name: Generate PKGBUILD from template
        run: |
          mkdir -p aur-repo
          cp crates/fresh-editor/packaging/aur/fresh-editor/PKGBUILD.template aur-repo/PKGBUILD
          sed -i "s/VERSION_PLACEHOLDER/${VERSION}/g" aur-repo/PKGBUILD
          sed -i "s/SHA256_PLACEHOLDER/${{ steps.checksum.outputs.sha256 }}/g" aur-repo/PKGBUILD
          echo "Generated PKGBUILD:"
          cat aur-repo/PKGBUILD

      - name: Publish AUR package (fresh-editor)
        uses: KSXGitHub/github-actions-deploy-aur@v4.1.3
        with:
          pkgname: fresh-editor
          pkgbuild: ./aur-repo/PKGBUILD
          commit_username: ${{ secrets.AUR_USERNAME }}
          commit_email: ${{ secrets.AUR_EMAIL }}
          ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }}
          commit_message: "Update to ${{ inputs.version }}"
          ssh_keyscan_types: ed25519
