name: AUR Release on Release
on:
  release:
    types: [published]
  workflow_dispatch:
    inputs: {}

jobs:
  aur-release:
    runs-on: ubuntu-latest

    if: '!github.event.release.prerelease'

    steps:
      - uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v6

      - run: PACKAGE_VERSION=$(cat ./package.json | grep version | head -1 | awk -F '"' '{print $4}') && echo "package_version=$PACKAGE_VERSION" >> $GITHUB_ENV
      - run: sed "s/PACKAGE_VERSION/${package_version}/g" build/linux/PKGBUILD_template > build/linux/PKGBUILD

      - name: Publish AUR package (attempt 1)
        id: publish_aur_1
        continue-on-error: true
        uses: KSXGitHub/github-actions-deploy-aur@da03e160361ce01bf087e790b6ffd196d7dccff7 # v4.1.3
        with:
          pkgname: superproductivity-bin
          pkgbuild: build/linux/PKGBUILD
          updpkgsums: true
          commit_username: ${{ secrets.AUR_USERNAME }}
          commit_email: ${{ secrets.AUR_EMAIL }}
          ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }}
          commit_message: Update AUR package
          ssh_keyscan_types: rsa,ecdsa,ed25519

      - name: Wait before AUR publish retry
        if: steps.publish_aur_1.outcome == 'failure'
        run: sleep 30

      - name: Publish AUR package (attempt 2)
        id: publish_aur_2
        if: steps.publish_aur_1.outcome == 'failure'
        continue-on-error: true
        uses: KSXGitHub/github-actions-deploy-aur@da03e160361ce01bf087e790b6ffd196d7dccff7 # v4.1.3
        with:
          pkgname: superproductivity-bin
          pkgbuild: build/linux/PKGBUILD
          updpkgsums: true
          commit_username: ${{ secrets.AUR_USERNAME }}
          commit_email: ${{ secrets.AUR_EMAIL }}
          ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }}
          commit_message: Update AUR package
          ssh_keyscan_types: rsa,ecdsa,ed25519

      - name: Wait before final AUR publish retry
        if: steps.publish_aur_1.outcome == 'failure' && steps.publish_aur_2.outcome == 'failure'
        run: sleep 60

      - name: Publish AUR package (attempt 3)
        if: steps.publish_aur_1.outcome == 'failure' && steps.publish_aur_2.outcome == 'failure'
        uses: KSXGitHub/github-actions-deploy-aur@da03e160361ce01bf087e790b6ffd196d7dccff7 # v4.1.3
        with:
          pkgname: superproductivity-bin
          pkgbuild: build/linux/PKGBUILD
          updpkgsums: true
          commit_username: ${{ secrets.AUR_USERNAME }}
          commit_email: ${{ secrets.AUR_EMAIL }}
          ssh_private_key: ${{ secrets.AUR_SSH_PRIVATE_KEY }}
          commit_message: Update AUR package
          ssh_keyscan_types: rsa,ecdsa,ed25519
