name: version

on:
  release:
    types: [published]

jobs:
  update-version:
    runs-on: [ubuntu-24.04]
    steps:
      - name: Checkout repository
        run: |
          set -euo pipefail
          rm -rf .git || true
          rm -rf ./* ./.??* || true
          git clone --depth 1 https://git.knoxmakers.org/KnoxMakers/knoxmakers-inkscape.git .
          git checkout main

      - name: Configure git identity
        run: |
          git config user.name "haxbot"
          git config user.email "haxbot@knoxmakers.sh"

      - name: Write version file
        run: |
          echo "${{ gitea.event.release.name }}" > version

      - name: Commit and push version file
        env:
          HAXBOT_TOKEN: ${{ secrets.HAXBOT_TOKEN }}
        run: |
          set -euo pipefail

          git add version

          if git diff --cached --quiet; then
            echo "No changes to version file"
            exit 0
          fi

          git commit -m "version: ${{ gitea.event.release.name }}"

          REPO_URL="https://x-access-token:${HAXBOT_TOKEN}@git.knoxmakers.org/KnoxMakers/knoxmakers-inkscape.git"
          git remote set-url origin "$REPO_URL"
          git push origin HEAD:main
