name: update
on:
  workflow_dispatch:
  schedule:
    - cron: '0 0 * * *'
jobs:
  main:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: tibdex/github-app-token@v1.8.2
        if: ${{ !env.ACT }}
        id: create-app-token
        with:
          app_id: ${{ secrets.APP_ID }}
          private_key: ${{ secrets.APP_PRIVATE_KEY }}
      - uses: actions/checkout@v3
        if: ${{ !env.ACT }}
        with:
          token: ${{ steps.create-app-token.outputs.token }}
      - name: setup git
        run: |
          git config user.name "iptv-bot[bot]"
          git config user.email "84861620+iptv-bot[bot]@users.noreply.github.com"
      - uses: actions/setup-node@v3
        if: ${{ !env.ACT }}
        with:
          node-version: 18
          cache: 'npm'
      - name: install dependencies
        run: npm install
      - name: load api data
        run: npm run api:load
      - name: update internal playlists
        run: npm run playlist:update --silent >> $GITHUB_OUTPUT
        id: playlist-update
      - name: check internal playlists
        run: |
          npm run playlist:lint
          npm run playlist:validate
      - name: generate public playlists
        run: npm run playlist:generate
      - name: generate .api/streams.json
        run: npm run api:generate
      - name: update readme.md
        run: npm run readme:update
      - run: git status
      - name: commit changes to /streams
        run: |
          git add streams
          git status
          git commit --allow-empty -m "[Bot] Update /streams" -m "Committed by [iptv-bot](https://github.com/apps/iptv-bot) via [update](https://github.com/iptv-org/iptv/actions/runs/${{ github.run_id }}) workflow." -m "${{ steps.playlist-update.outputs.OUTPUT }}" --no-verify
      - name: commit changes to readme.md
        run: |
          git add README.md
          git status
          git commit --allow-empty -m "[Bot] Update README.md" -m "Committed by [iptv-bot](https://github.com/apps/iptv-bot) via [update](https://github.com/iptv-org/iptv/actions/runs/${{ github.run_id }}) workflow." --no-verify
      - name: push all changes to the repository
        if: ${{ !env.ACT && github.ref == 'refs/heads/master' }}
        run: git push
      - name: deploy public playlists to github pages
        uses: JamesIves/github-pages-deploy-action@4.1.1
        if: ${{ !env.ACT && github.ref == 'refs/heads/master' }}
        with:
          repository-name: iptv-org/iptv
          branch: gh-pages
          folder: .gh-pages
          token: ${{ steps.create-app-token.outputs.token }}
          git-config-name: iptv-bot[bot]
          git-config-email: 84861620+iptv-bot[bot]@users.noreply.github.com
          commit-message: '[Bot] Deploy to GitHub Pages'
          clean: true
      - name: move .api/streams.json to iptv-org/api
        uses: JamesIves/github-pages-deploy-action@4.1.1
        if: ${{ !env.ACT && github.ref == 'refs/heads/master' }}
        with:
          repository-name: iptv-org/api
          branch: gh-pages
          folder: .api
          token: ${{ steps.create-app-token.outputs.token }}
          git-config-name: iptv-bot[bot]
          git-config-email: 84861620+iptv-bot[bot]@users.noreply.github.com
          commit-message: '[Bot] Deploy to iptv-org/api'
          clean: false
