name: Create Release Commit
on:
  push:
    branches:
      - main
    tags-ignore:
      - v*.*.*
jobs:
  prepare:
    runs-on: ubuntu-latest
    if: contains(github.event.head_commit.message, 'Release-As:')
    env:
      USERNAME: servostar
      EMAIL: sven.vogel123@web.de
      GIT_AUTH_TOKEN: ${{ secrets.RELEASE_TOKEN }}
      COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
        with:
          ref: 'main'
          token:  ${{ secrets.RELEASE_TOKEN }}
      - name: Install dependencies for Nix setup action
        run: |
          apt update -y
          apt install sudo -y
      - name: Setup Nix
        uses: cachix/install-nix-action@v27
        with:
          nix_path: nixpkgs=channel:nixos-unstable
      - name: Setup Git
        run: |
          git config --global user.name "${USERNAME}"
          git config --global user.email "${EMAIL}"
      - name: Update Typst.toml
        run: |
          export VERSION=$(echo "${COMMIT_MESSAGE}" | grep -Po '(?<=Release-As: )v\d+\.\d+\.\d+')
          sed -i "/version/c\version = \"${VERSION#v}\"" typst.toml
          git add typst.toml
          git commit -m "chore: bump release version to $VERSION" -m "Generated-By: ${{ gitea.actor }}"
      - name: Generate Example Document
        run: nix-shell --run ./run-ci.sh
      - name: Generate Assets
        if: contains(github.event.head_commit.message, 'Generate-Assets')
        run: |
          ./run-bake-assets.sh
          git add assets/banner.png
          git add assets/page-preview.png
          git commit -m "chore: update assets" -m "Generated-By: ${{ gitea.actor }}"
      - name: Tag and Push Changes
        run: |
          git push origin main
          export VERSION=$(echo "${COMMIT_MESSAGE}" | grep -Po '(?<=Release-As: )v\d+\.\d+\.\d+')
          git tag -m "Release" "${VERSION}"
          git push origin "${VERSION}"