name: Update Factorio Mod List

on:
  workflow_dispatch:
  push:
    branches: [ main ]
    paths:
      - 'Factorio/**'

jobs:
  update-modlist:
    runs-on: ubuntu-latest
    steps:
      - name: Clone repository manually
        run: |
          git clone https://git.garpnet.it/GamerArena/FastDL.git
          git checkout main

      - name: Generate mod list
        run: |
          cd Factorio
          echo "[" > mods.json
          for f in *.zip; do
            [ -f "$f" ] || continue
            echo "  \"${f}\"," >> mods.json
          done
          # remove trailing comma and close JSON array
          sed -i '$ s/,$//' mods.json
          echo "]" >> mods.json

      - name: Commit and push updated mod list
        run: |
          git config user.name "Gitea Bot"
          git config user.email "actions@local"
          git add -f Factorio/mods.json
          git commit -m "Auto-update Factorio mod list" || exit 0
          git push