name: ArchLinux AUR Release

on:
  release:
    types: [published]

  workflow_dispatch:

env:
  GO_VERSION: "1.20"
  PKGBUILD:   ".github/workflows/aur/PKGBUILD"
  SRCINFO:    ".github/workflows/aur/SRCINFO"

jobs:
  build:

    runs-on: ubuntu-latest
 
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Setup Go ${{ ENV.GO_VERSION }}
        uses: actions/setup-go@v4
        with:
          go-version: ${{ ENV.GO_VERSION }}

      - name: Install Murex
        run: |
          go build -v github.com/lmorg/murex

      - name: Download Murex tarball
        run: |
          export MUREX_VERSION=$(./murex -c "version --no-app-name")
          wget -O - https://github.com/lmorg/murex/archive/refs/tags/v${MUREX_VERSION}.tar.gz > murex.tar.gz

      - name: Update PKGBUILD and SRCINFO
        run: |
          export MUREX_VERSION=$(./murex -c "version --no-app-name")
          export PKG_RELEASE="${{ github.run_number }}"
          sha256sum murex.tar.gz | awk '{ print $1 }' > chksum
          export CHECKSUM=$(cat chksum)

          sed -i "s/xxxMUREX_VERSIONxxx/$MUREX_VERSION/" ${{ ENV.PKGBUILD }}
          sed -i "s/xxxPKG_RELEASExxx/$PKG_RELEASE/"     ${{ ENV.PKGBUILD }}
          sed -i "s/xxxCHECKSUMxxx/$CHECKSUM/"           ${{ ENV.PKGBUILD }}
          printf "# ${{ ENV.PKGBUILD }}:\n\n"
          cat ${{ ENV.PKGBUILD }}

          sed -i "s/xxxMUREX_VERSIONxxx/$MUREX_VERSION/" ${{ ENV.SRCINFO }}
          sed -i "s/xxxPKG_RELEASExxx/$PKG_RELEASE/"     ${{ ENV.SRCINFO }}
          sed -i "s/xxxCHECKSUMxxx/$CHECKSUM/"           ${{ ENV.SRCINFO }}
          printf "\n\n# ${{ ENV.SRCINFO }}:\n\n"
          cat ${{ ENV.SRCINFO }}

      - name: Clone AUR repo (murex)
        run: |
          install -m 600 -D /dev/null ~/.ssh/id_rsa
          echo "${{ secrets.AUR_SSH_PRIVATE }}" > ~/.ssh/id_rsa
          ssh-keyscan aur.archlinux.org >> ~/.ssh/known_hosts
          git clone ssh://aur@aur.archlinux.org/murex.git aur-murex

      - name: Update repo
        run: |
          cp -v ${{ ENV.PKGBUILD }} aur-murex/PKGBUILD
          cp -v ${{ ENV.SRCINFO }}  aur-murex/.SRCINFO
          cd aur-murex
          git config --global user.email "mail@laurencemorgan.co.uk"
          git config --global user.name "lmorg"
          git commit -am "$(../murex -c "version --no-app-name"), ${{ github.run_number }}"
          git push
          

