name: Sync README.md between node.js/ ⟷ node.js/docs/, then node.js/ to adamlui/js-utils/ai-personas

on:
  push:
    branches: [main]
    paths: ["node.js/**"]

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

    steps:

      - name: Checkout KudoAI/ai-personas
        uses: actions/checkout@v6.0.2
        with:
          token: ${{ secrets.REPO_SYNC_PAT }}
          path: KudoAI/ai-personas
          fetch-depth: 2

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

      - name: Sync README.md between node.js/ ⟷ node.js/docs/
        run: |
          cd ${{ github.workspace }}/KudoAI/ai-personas
          root_readme="./node.js/README.md"
          root_readme_content=$(git show HEAD:"$root_readme")
          root_readme_modified=$(git log -1 --format="%ct" -- "$root_readme")
          docs_readme="./node.js/docs/README.md"
          docs_readme_content=$(git show HEAD:"$docs_readme")
          docs_readme_modified=$(git log -1 --format="%ct" -- "$docs_readme")
          if [[ "$root_readme_content" != "$docs_readme_content" ]] ; then
            echo "readme_updated=true" >> $GITHUB_ENV
            if (( $root_readme_modified > $docs_readme_modified )) ; then
              cp -f "$root_readme" "$docs_readme"
              echo "Copied $root_readme to $docs_readme"
            elif (( $docs_readme_modified > $root_readme_modified )) ; then
              cp -f "$docs_readme" "$root_readme"
              echo "Copied $docs_readme to $root_readme"
            fi
          fi

      - name: Sync node.js/ to adamlui/js-utils/ai-personas
        run: |
          rsync -avhc --delete \
            ${{ github.workspace }}/KudoAI/ai-personas/node.js/ \
            ${{ github.workspace }}/adamlui/js-utils/ai-personas/

      - 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 KudoAI/ai-personas
        if: env.readme_updated
        run: |
          cd ${{ github.workspace }}/KudoAI/ai-personas
          git add . && git commit -n -m "$ESCAPED_MSG ↞ [auto-sync from https://github.com/KudoAI/ai-personas]" || true
          git pull --rebase
          git push

      - name: Push changes to adamlui/js-utils
        run: |
          cd ${{ github.workspace }}/adamlui/js-utils
          git add . && git commit -n -m \
            "$ESCAPED_MSG ↞ [auto-sync from https://github.com/KudoAI/ai-personas/tree/main/node.js]" || true
          git pull --rebase
          git push
