# .gitea/workflows/typst-build.yml
name: Build Typst PDFs (Docker)

on:
  push:
    branches: [ "**" ]
  pull_request:
    branches: [ "**" ]

jobs:
  build-typst:
    runs-on: ubuntu-latest

    # Run the whole job inside a Docker container that has Typst installed
    steps:
      - uses: typst-community/setup-typst@v4
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: false
          lfs: false

      - name: Debug Ls
        run: ls -la "$PWD" && echo "$PWD && echo ${{ github.workspace }}"

      - name: Make build directory
        run: mkdir -p build

      - name: Compile Analysis1
        continue-on-error: true
        run: typst compile --root src src/cheatsheets/Analysis1.typ "build/sem1-Analysis_1.pdf"

      - name: Compile Schaltungstheorie
        continue-on-error: true
        run: typst compile --root src src/cheatsheets/Schaltungstheorie.typ "build/sem1-Schaltungstheorie.pdf"

      - name: Compile LinAlg
        continue-on-error: true
        run: typst compile --root src src/cheatsheets/LinearAlgebra.typ "build/sem1-Lineare-algebra.pdf"

      - name: Compile Digtaltechnik
        continue-on-error: true
        run: typst compile --root src src/cheatsheets/Digitaltechnik.typ "build/sem1-Digitaltechnik.pdf"

      - name: Compile CT
        continue-on-error: true
        run: typst compile --root src src/cheatsheets/CT.typ "build/sem1-Computertechnik.pdf"


      - name: Create Gitea Release
        continue-on-error: true
        uses: akkuman/gitea-release-action@v1
        with:
          name: "Formelsammlungen PDFs"
          tag_name: "latest"
          files: |
            build/*.pdf
            inventory.json
          
      - name: Update and push latest tag
        env:
          GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}
        run: |
          set -euo pipefail

          git config user.name  "gitea-actions"
          git config user.email "actions@local"

          # Ensure origin uses token auth
          git remote set-url origin "https://oauth2:${GITEA_TOKEN}@gitea.mintcalc.com/alexander/TUM-Formelsammlungen.git"

          # Move (or create) the tag locally
          git tag -f latest

          # Force-push the tag to overwrite remote 'latest'
          git push origin refs/tags/latest --force
          
      - name: Trigger 
        continue-on-error: true
        run: curl -u trigger:${{ secrets.TRIGGER_PASSWORD }} -X POST https://trigger.typst4ei.de/trigger/all