name: Update README

# Allow Gitea Actions to commit and push changes
permissions:
  contents: write

on:
  workflow_dispatch:
  schedule:
    - cron: '0 4 * * *'  # Every day at 4 AM UTC

jobs:
  update-readme:
    runs-on: ubuntu-latest
    container:
      image: catthehacker/ubuntu:act-latest

    env:
      # Adjusted for Gitea profile convention (usually owner/.profile or owner/.gitea)
      # Please verify if 'ivancarlos/.profile' exists or adjust to your profile repository.
      SOURCE_REPO: ivancarlos/.profile
      SOURCE_BRANCH: main

    steps:
      - name: Checkout current repository
        uses: actions/checkout@v4

      - name: Checkout source README template
        uses: actions/checkout@v4
        with:
          repository: ${{ env.SOURCE_REPO }}
          ref: ${{ env.SOURCE_BRANCH }}
          path: source_readme

      - name: Update README.md (buttons and footer)
        run: |
          set -e
          REPO_NAME="${GITHUB_REPOSITORY##*/}"

          # --- Extract buttons block from source ---
          # Checking if source file exists, if not, skip or fail.
          if [ ! -f source_readme/README.md ]; then
             echo "Source README not found in ${SOURCE_REPO}"
             exit 1
          fi

          BUTTONS=$(awk '/<!-- buttons -->/{flag=1;next}/<!-- endbuttons -->/{flag=0}flag' source_readme/README.md)
          BUTTONS_UPDATED=$(echo "$BUTTONS" | sed "s/\.github/${REPO_NAME}/g")

          # --- Extract footer block from source (everything from <!-- footer --> onward) ---
          FOOTER=$(awk '/<!-- footer -->/{flag=1}flag' source_readme/README.md)

          # --- Replace buttons section in README.md ---
          UPDATED=$(awk -v buttons="$BUTTONS_UPDATED" '
            BEGIN { skip=0 }
            /<!-- buttons -->/ {
              print
              print buttons
              skip=1
              next
            }
            /<!-- endbuttons -->/ && skip {
              print
              print buttons
              skip=0
              next
            }
            !skip { print }
          ' README.md)

          # --- Replace everything after <!-- footer --> with FOOTER ---
          echo "$UPDATED" | awk -v footer="$FOOTER" '
            /<!-- footer -->/ {
              print footer
              found=1
              exit
            }
            { print }
          ' > README.tmp && mv README.tmp README.md

      - name: Remove source_readme from git index
        run: rm -rf source_readme || true

      - name: Commit and push changes
        run: |
          git config --global --add safe.directory '*'
          git config user.name "Gitea Actions"
          git config user.email "actions@git.icc.gg"
          
          if [ -n "$(git status --porcelain README.md)" ]; then
            git add README.md
            git commit -m "Sync README from template [▶️]"
            git push origin HEAD:${{ github.ref_name }}
          else
            echo "No changes to README.md"
          fi
