name: Announce Release
on:
  release:
    types: [published]
env:
  VER: ${{ github.ref_name }}
  HOMEBREW_REPO: https://kenshaw:${{ secrets.HOMEBREW_TOKEN }}@github.com/xo/homebrew-xo.git
  AUR_REPO: aur@aur.archlinux.org:usql.git

jobs:
  bump-aur-package:
    name: Bump AUR Package
    runs-on: ubuntu-24.04
    steps:
      - name: Add AUR SSH key
        uses: shimataro/ssh-key-action@v2
        with:
          key: ${{ secrets.AUR_SSH_KEY }}
          name: id_ed25519
          known_hosts: |
            aur.archlinux.org ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEuBKrPzbawxA/k2g6NcyV5jmqwJ2s+zpgZGZ7tpLIcN
            aur.archlinux.org ssh-rsa  
            aur.archlinux.org ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBLMiLrP8pVi5BFX2i3vepSUnpedeiewE5XptnUnau+ZoeUOPkpoCgZZuYfpaIQfhhJJI5qgnjJmr4hyJbe/zxow=
      - name: Bump AUR Package
        run: |
          export WORKDIR=$(mktemp -d /tmp/aur-usql.XXXXXX)
          export REPO_PATH=$WORKDIR/aur-usql
          wget -O $WORKDIR/archive.tar.gz https://github.com/xo/usql/archive/${VER}.tar.gz
          export SHA256SUM=$(sha256sum $WORKDIR/archive.tar.gz|awk '{print $1}')
          export CHANGELOG=$(
            curl \
              -s \
              -H 'Accept: application/vnd.github+json' \
              https://api.github.com/repos/xo/usql/releases/tags/$VER \
              |jq -r .body \
              |sed -e 's/\\r//g' -e 's/\[VirusTotal.*//'
          )
          git clone $AUR_REPO $REPO_PATH
          git -C $REPO_PATH config user.name 'Kenneth Shaw'
          git -C $REPO_PATH config user.email 'kenshaw@gmail.com'
          sed -i "s/pkgver=.*$/pkgver=${VER#v}/" $REPO_PATH/PKGBUILD
          sed -i "s/sha256sums=.*$/sha256sums=('$SHA256SUM')/" $REPO_PATH/PKGBUILD
          sed -i "s/pkgrel=.*$/pkgrel=1/" $REPO_PATH/PKGBUILD
          sed -i "s/pkgver =.*$/pkgver = ${VER#v}/" $REPO_PATH/.SRCINFO
          sed -i "s%source =.*$%source = usql-${VER#v}.tar.gz::https://github.com/xo/usql/archive/${VER}.tar.gz%" $REPO_PATH/.SRCINFO
          sed -i "s/sha256sums =.*$/sha256sums = $SHA256SUM/" $REPO_PATH/.SRCINFO
          sed -i "s/pkgrel =.*$/pkgrel = 1/" $REPO_PATH/.SRCINFO
          git -C $REPO_PATH add PKGBUILD .SRCINFO
          git -C $REPO_PATH commit -m "$(printf %b "Update usql version to ${VER}\n\n${CHANGELOG}")"
          git -C $REPO_PATH show -C
          git -C $REPO_PATH push origin master

  bump-homebrew-formula:
    name: Bump Homebrew Formula
    runs-on: ubuntu-24.04
    steps:
      - name: Bump Homebrew Formula
        run: |
          export WORKDIR=$(mktemp -d /tmp/homebrew-xo.XXXXXX)
          export REPO_PATH=$WORKDIR/homebrew-xo
          wget -O $WORKDIR/archive.tar.gz https://github.com/xo/usql/archive/${VER}.tar.gz
          export SHA256SUM=$(sha256sum $WORKDIR/archive.tar.gz|awk '{print $1}')
          export CHANGELOG=$(
            curl \
              -s \
              -H 'Accept: application/vnd.github+json' \
              https://api.github.com/repos/xo/usql/releases/tags/$VER \
              |jq -r .body \
              |sed -e 's/\\r//g' -e 's/\[VirusTotal.*//'
          )
          git clone $HOMEBREW_REPO $REPO_PATH
          git -C $REPO_PATH config user.name 'Kenneth Shaw'
          git -C $REPO_PATH config user.email 'ken@usql.app'
          sed -i "s%url \".*$%url \"https://github.com/xo/usql/archive/${VER}.tar.gz\"%" $REPO_PATH/Formula/usql.rb
          sed -i "s/sha256 \".*$/sha256 \"$SHA256SUM\"/" $REPO_PATH/Formula/usql.rb
          git -C $REPO_PATH add Formula/usql.rb
          git -C $REPO_PATH commit -m "$(printf %b "Update usql version to ${VER}\n\n${CHANGELOG}")"
          git -C $REPO_PATH show -C
          git -C $REPO_PATH push origin master

  announce-discord:
    name: Announce Discord
    runs-on: ubuntu-24.04
    steps:
      - name: Announce Discord
        run: |
          curl \
            -H 'Content-Type: application/json' \
            -d '{"username": "usql", "content": "> *usql ${{ github.ref_name }}* has been released!\n\nGet it here: https://github.com/xo/usql/releases/${{ github.ref_name }}"}' \
            ${{ secrets.DISCORD_WEBHOOK_URL }}
