name: Dev Version Bump

on:
  workflow_run:
    workflows: ["Push Smoke Build"]
    types: [completed]
    branches: [dev]

env:
  CI_GIT_EMAIL: 'git-ci@hiabuto.net'
  CI_GIT_NAME:  'Git-CI'
  GITEA_REPO: ${{ github.repository }}

jobs:
  bump:
    runs-on: linux-amd64
    if: "${{ gitea.event.workflow_run.conclusion == 'success' && gitea.event.workflow_run.head_branch == 'dev' }}"
    steps:
      - name: Checkout repository
        shell: bash
        env:
          CI_MATCHA_GITEA_TOKEN: ${{ secrets.CI_MATCHA_GITEA_TOKEN }}
        run: |
          set -euo pipefail
          git init .
          git remote add origin "https://git.hiabuto.net/${{ github.repository }}.git"
          if [ -n "${CI_MATCHA_GITEA_TOKEN:-}" ]; then
            basic=$(python3 -c 'import base64, os; print(base64.b64encode(("matcha:" + os.environ["CI_MATCHA_GITEA_TOKEN"]).encode()).decode())')
            git config --global http.https://git.hiabuto.net/.extraheader "AUTHORIZATION: basic ${basic}"
          else
            echo "CI_MATCHA_GITEA_TOKEN not set; trying unauthenticated checkout."
          fi
          git fetch --depth=50 origin "${{ gitea.event.workflow_run.head_branch }}"
          git checkout --force "${{ gitea.event.workflow_run.head_sha }}"

      - name: Load central CI templates
        shell: bash
        env:
          CI_MATCHA_GITEA_TOKEN: ${{ secrets.CI_MATCHA_GITEA_TOKEN }}
        run: |
          set -euo pipefail
          if [ -z "${CI_MATCHA_GITEA_TOKEN:-}" ]; then
            echo "Missing secrets.CI_MATCHA_GITEA_TOKEN" >&2
            exit 1
          fi
          basic=$(python3 -c 'import base64, os; print(base64.b64encode(("matcha:" + os.environ["CI_MATCHA_GITEA_TOKEN"]).encode()).decode())')
          git config --global http.https://git.hiabuto.net/.extraheader "AUTHORIZATION: basic ${basic}"
          git submodule sync --recursive
          git submodule update --init --remote ci-templates
          git -C ci-templates rev-parse --short HEAD

      - id: ver
        name: Compute version
        uses: ./ci-templates/.gitea/actions/actions-versioning
        with:
          config_file: ci-config.yaml
          ref_name: ${{ gitea.event.workflow_run.head_branch }}

      - name: Commit + push VERSION (dev)
        shell: bash
        env:
          CI_MATCHA_GITEA_TOKEN: ${{ secrets.CI_MATCHA_GITEA_TOKEN }}
        run: |
          set -euo pipefail
          if [ -z "${CI_MATCHA_GITEA_TOKEN:-}" ]; then
            echo "Missing secrets.CI_MATCHA_GITEA_TOKEN" >&2
            exit 1
          fi

          subj=$(git log -1 --pretty=format:%s)
          if [[ "$subj" == *"[nobuild]"* ]]; then
            echo "[nobuild] present; skipping dev bump."
            exit 0
          fi

          if [[ "$subj" == ci:\ bump\ dev\ version* ]]; then
            echo "Already a dev bump commit; nothing to do."
            exit 0
          fi

          version="${{ steps.ver.outputs.program_version }}"
          version_file=$(python3 - <<'PY'
          import re
          p='VERSION'
          for line in open('ci-config.yaml', encoding='utf-8'):
              if re.match(r'^\s*file\s*:', line):
                  p=line.split(':',1)[1].strip().strip('"\'')
                  break
          print(p)
          PY
          )

          git config user.email "${CI_GIT_EMAIL}"
          git config user.name  "${CI_GIT_NAME}"

          echo "$version" > "$version_file"
          git add "$version_file"
          if git diff --cached --quiet; then
            echo "VERSION already up-to-date; nothing to commit."; exit 0
          fi

          git commit -m "ci: bump dev version to ${version} [skip ci]"
          git config --local --unset-all http.https://git.hiabuto.net/.extraheader || true
          git config --global --unset-all http.https://git.hiabuto.net/.extraheader || true
          git remote set-url origin "https://matcha:${CI_MATCHA_GITEA_TOKEN}@git.hiabuto.net/${GITEA_REPO}.git"
          git push origin HEAD:dev
