name: Sync website

on:
  push:
    branches: [main]
    paths:
      - docs/**

concurrency:
  group: sync-website
  cancel-in-progress: true

jobs:
  sync-website:
    if: github.repository == 'mangowm/mango'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          token: ${{ github.token }}

      - name: Fetch tags for versioned docs
        run: git fetch origin --tags --depth=1

      - name: Checkout website
        uses: actions/checkout@v4
        with:
          repository: mangowm/mangowm.github.io
          path: website
          token: ${{ secrets.WEBSITE_SYNC_TOKEN }}
          fetch-depth: 1

      - name: Sync docs
        run: |
          TARGET=website/apps/web/content/docs
          rm -rf "$TARGET"
          mkdir -p "$TARGET"

          # Copy current docs as "(git)"
          cp -r docs "$TARGET/(git)"

          # Generate versioned docs from last 2 semver tags
          tags=$(git tag --sort=-v:refname | grep '^v\?[0-9]\+\.[0-9]\+\.[0-9]\+$' | head -2)
          for tag in $tags; do
            git worktree add /tmp/docs-"$tag" "$tag" || continue
            if [ -d "/tmp/docs-$tag/docs" ]; then
              name="v${tag#v}"
              cp -r "/tmp/docs-$tag/docs" "$TARGET/$name"
              jq --arg title "$name" --arg desc "$name release" \
                '.title = $title | .description = $desc | .root = true' \
                "$TARGET/$name/meta.json" > "$TARGET/$name/meta.json.tmp" \
                && mv "$TARGET/$name/meta.json.tmp" "$TARGET/$name/meta.json"
            fi
            git worktree remove /tmp/docs-"$tag"
          done

          # Generate root version index
          {
            echo "(git)"
            for tag in $tags; do
              echo "v${tag#v}"
            done
          } | jq -Rn '[inputs | select(length > 0)] | {pages: .}' > "$TARGET/meta.json"

      - name: Commit and push
        working-directory: website
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git add apps/web/content/docs
          git diff --staged --quiet || git commit \
            -m "docs: content update from mangowm/mango" \
            -m "${{ github.server_url }}/${{ github.repository }}/commit/${{ github.sha }}"
          git push
