name: Sync _locales/ between <chrome|firefox>/extension/, then ** to adamlui/ai-web-extensions/chatgpt-infinity/, then greasemonkey/ to adamlui/userscripts/chatgpt/chatgpt-infinity/

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

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

    steps:

      - name: Checkout adamlui/chatgpt-infinity
        uses: actions/checkout@v6.0.2
        with:
          token: ${{ secrets.REPO_SYNC_PAT }}
          path: adamlui/chatgpt-infinity
          fetch-depth: 2

      - 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: Checkout adamlui/userscripts
        uses: actions/checkout@v6.0.2
        with:
          token: ${{ secrets.REPO_SYNC_PAT }}
          repository: adamlui/userscripts
          path: adamlui/userscripts

      - name: Sync _locales/ between <chrome|firefox>/extension/
        run: |
          cd ${{ github.workspace }}/adamlui/chatgpt-infinity
          ff_dir="firefox/extension/_locales"
          chromium_dir="chromium/extension/_locales"
          
          # Loop thru all lang dirs in firefox
          for locale in $(find "$ff_dir" -mindepth 1 -maxdepth 1 -type d -exec basename {} \;) ; do
            ff_file="$ff_dir/$locale/messages.json"
            chromium_file="$chromium_dir/$locale/messages.json"
            if [[ -f "$ff_file" && -f "$chromium_file" ]] ; then

              # Get the latest commit timestamps for both files
              ff_timestamp=$(git log -1 --format="%ct" -- "$ff_file" 2>/dev/null || echo 0)
              chromium_timestamp=$(git log -1 --format="%ct" -- "$chromium_file" 2>/dev/null || echo 0)
      
              # Sync the most recently updated messages.json to the other dir
              if [[ $ff_timestamp -ne $chromium_timestamp ]] ; then
                if (( $ff_timestamp > $chromium_timestamp )) ; then
                     cp -f "$ff_file" "$chromium_file" ; sync_src="${ff_dir%%/*}"
                else cp -f "$chromium_file" "$ff_file" ; sync_src="${chromium_dir%%/*}" ; fi
              fi

            fi
          done

          # Expose sync src for commit msg in self-push step
          echo "SYNC_SRC=$sync_src" >> $GITHUB_ENV

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

      - name: Sync greasemonkey/ to adamlui/userscripts/chatgpt/chatgpt-infinity/
        run: |
          rsync -avhc --delete \
            ${{ github.workspace }}/adamlui/chatgpt-infinity/greasemonkey/ \
            ${{ github.workspace }}/adamlui/userscripts/chatgpt/chatgpt-infinity/

      - 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/chatgpt-infinity
        if: env.SYNC_SRC
        run: |
          cd ${{ github.workspace }}/adamlui/chatgpt-infinity
          git add . && git commit -n -m "$ESCAPED_MSG ↞ [auto-sync from $SYNC_SRC/]" || true
          git pull --rebase 
          git push

      - 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/chatgpt-infinity]" || true
          git pull --rebase 
          git push

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