name: Modify Last Modified Timestamp

on:
  push:
    branches:
      - main
    paths-ignore:
      - README.md

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  patch:
    name: modify
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v6
        with:
          ref: main
          fetch-depth: 0

      - name: Modify last-updated field
        run: |
          for f in $(git ls-files *.txt); do
            COMMIT_DATE=$(git log -1 --format=%aD $f)
            AUTHOR=$(git log -1 --pretty=format:%an $f)

            # Skip commits made by GitHub Actions bot
            if [[ "$AUTHOR" == "github-actions[bot]" ]]; then
              continue
            fi

            if sed --version >/dev/null 2>&1; then
              sed -i "0,/^! Last modified: /s|^! Last modified: .*|! Last modified: $COMMIT_DATE|" "$f"
            fi
          done

      - name: Commit changes (if any)
        run: |
          if [[ -n $(git diff) ]]; then
            git config user.name "github-actions[bot]"
            git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
            git add -A
            git commit -m "Update modified filter lists with last commit timestamp"
            git push -u origin main
          fi
