name: M3U Lint and Sort

on:
  push:
    paths:
      - 'mystique.m3u'
      - 'european_radio.m3u'
      - 'world_radio.m3u'
  workflow_dispatch:

jobs:
  lint-and-sort:
    runs-on: ubuntu-22.04
    
    steps:
      - uses: actions/checkout@v4
      
      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Basic M3U cleanup
        run: node .forgejo/scripts/cleanup-m3u.js mystique.m3u
        
      - name: Sort M3U file
        run: node .forgejo/scripts/sort-m3u.js mystique.m3u

      - name: Split into country playlists
        run: node .forgejo/scripts/countries.js mystique.m3u

      - name: Split into cultural group playlists
        run: node .forgejo/scripts/cultural-groups.js mystique.m3u

      - name: Update README
        run: node .forgejo/scripts/readme-m3u.js mystique.m3u european_radio.m3u world_radio.m3u


      - name: Commit changes to files 
        run: |
          git config --global user.name 'Forgejo Actions Bot'
          git config --global user.email 'forgejo-actions[bot]@noreply.forgejo.org'
          git add --all
          git diff --quiet && git diff --staged --quiet || (git commit -m "Automated updates of M3U Files" && git push)

