name: Sync ** to adamlui/ai-web-extensions/perplexity-omnibox/

on:
  push:
    branches: [main]
    paths: ["**", "!.*"]

jobs:
  build:
    if: (github.repository == 'adamlui/perplexity-omnibox') && !contains(github.event.head_commit.message, '[auto-sync')
    runs-on: ubuntu-24.04
    permissions:
      contents: read
    env:
      TZ: PST8PDT

    steps:

      - name: Checkout adamlui/perplexity-omnibox
        uses: actions/checkout@v6.0.2
        with:
          path: adamlui/perplexity-omnibox

      - name: Checkout adamlui/ai-web-extensions
        uses: actions/checkout@v6.0.2
        with:
          token: ${{ secrets.REPO_SYNC_PAT }}
          repository: adamlui/ai-web-extensions
          path: adamlui/ai-web-extensions

      - name: Sync ** to adamlui/ai-web-extensions/perplexity-omnibox/
        run: |
          rsync -avhr --delete --exclude '.*' \
            ${{ github.workspace }}/adamlui/perplexity-omnibox/ \
            ${{ github.workspace }}/adamlui/ai-web-extensions/perplexity-omnibox/

      - name: Escape backticks in commit msg
        env:
          COMMIT_MSG: ${{ github.event.head_commit.message }}
        run: |
          DELIM="EOF_$(uuidgen)"
          echo "ESCAPED_MSG<<$DELIM" >> "$GITHUB_ENV"
          echo "$COMMIT_MSG" | sed 's/`/\`/g' >> "$GITHUB_ENV"
          echo "$DELIM" >> "$GITHUB_ENV"

      - name: Config committer
        env:
          GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
          GPG_PRIVATE_ID: ${{ secrets.GPG_PRIVATE_ID }}
        run: |
          gpg --batch --import <(echo "$GPG_PRIVATE_KEY")
          git config --global commit.gpgsign true
          git config --global user.name "kudo-sync-bot"
          git config --global user.email "auto-sync@kudoai.com"
          git config --global user.signingkey "$GPG_PRIVATE_ID"

      - name: Push changes to adamlui/ai-web-extensions
        run: |
          cd ${{ github.workspace }}/adamlui/ai-web-extensions
          git add . && git commit -n -m "$ESCAPED_MSG ↞ [auto-sync from https://github.com/adamlui/perplexity-omnibox]" || true
          git pull --rebase 
          git push
