name: Wordlist Updater - Remote wordlists updater

on:
  schedule:
  - cron: 0 * * * *
  
  workflow_dispatch:

jobs:
  update-files:
    if: vars.RUN_UPDATERS == 1
    permissions:
      contents: write
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Update lists
        run: ./.bin/wordlist-updaters/updater.py
          
      - name: Ensure UTF-8 encoding
        run: |
          wget https://raw.githubusercontent.com/ItsIgnacioPortal/utf8fixer/f42bfe64db0bc095e9e93a10ac6bba746f09670c/utf8fixer.py
          chmod +x utf8fixer.py
          python ./utf8fixer.py Discovery/Web-Content/trickest-robots-disallowed-wordlists/top-10000.txt utf8
          rm utf8fixer.py

      - name: Commit files if changed
        run: |
          git add -N .

          if [ -z "$(git ls-files --modified)" ]; then
              echo "[+] No files were changed"
          else
              echo "[+] Files were changed! Pushing changed..."
              git add --renormalize -A && git add -A
              chmod +x ./.bin/brute-force-renormalize.sh
              ./.bin/brute-force-renormalize.sh ./Discovery/Web-Content/trickest-robots-disallowed-wordlists/top-10000.txt
              git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/$GITHUB_REPOSITORY"
              git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
              git config --local user.name "github-actions[bot]"
              git commit -m "[Github Action] Automated trickest wordlists update."
              git push
          fi

        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
