name: Auto Update README with Repo Name

on:
  push:
    branches:
      - main
      - master

jobs:
  update-readme:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code (Gitea version)
        uses: https://gitea.com/gitea/act/src/branch/master/actions/checkout@v1
        with:
          fetch-depth: 0

      - name: Extract repository name
        run: echo "REPO_NAME=ActionDemo" >> $GITHUB_ENV

      - name: Create or update README
        run: |
          if [ ! -f README.md ]; then
            echo "# $REPO_NAME" > README.md
            echo -e "\nThis repository is automatically maintained by Gitea Actions." >> README.md
          else
            if ! grep -q "^# $REPO_NAME" README.md; then
              sed -i "1i# $REPO_NAME" README.md
            fi
          fi

      - name: Commit and push changes
        run: |
          git config --global user.name "iorebuild"
          git config --global user.email "liyepan@linuxacme.com"
          git add README.md
          git diff --quiet && git diff --staged --quiet || git commit -m "Auto-update: Add repository name to README"
          git push