name: Add time - Deploy to cdn-page branch

on:
  push:
    branches:
      - main
    paths:
      - 'easylistdutch/**'
      - 'Personal List (uBo).txt'
      - 'tools/dutch-filter-list.template'
  workflow_dispatch:

# https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#example-only-cancel-in-progress-jobs-or-runs-for-the-current-workflow
concurrency: 
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
  
permissions: 
  contents: read

jobs:
  publish:
    permissions: 
      contents: write # for Git to git push
    name: Publish lists
    runs-on: ubuntu-latest
    steps:
      - name: Repo check
        run: |
          if [[ "$GITHUB_REPOSITORY_OWNER" != "JohnyP36" ]]; then
            echo "This GitHub Action is meant to deliver filter lists for EasyDutch."
            echo "You shouldn't need to run this GitHub Action in your fork."
            echo "If you do, please customize the cron expression above, and the URLs below."
            exit 1
          fi
          exit 0
      - name: Sleep for 10 minutes
        run: sleep 600s
        shell: bash
      - name: Clone easylistdutch
        uses: actions/checkout@v4
        with:
            ref: cdn-page
      - name: Generate version string
        run: |
          TAGNAME=$(date -u "+%Y.%-m.%-d.")$(date -u "+%H*60+%M" | bc)
          echo "TAGNAME=$TAGNAME" >> $GITHUB_ENV
          echo "Version string is $TAGNAME"
      - name: Copy shell script from JohnyP36/Personal-List
        run: |
          TMPDIR="$(mktemp -d)"
          git clone --depth=1 --single-branch --branch=main https://github.com/JohnyP36/Personal-List.git "$TMPDIR"
          cp -v "$TMPDIR"/tools/make-diffpatch.sh .
          cp -v "$TMPDIR"/tools/update-diffpatches.sh .
      # - name: Change permissions
      #  run: git update-index --chmod=+x ./tools/make-dutchfilterlist.sh
      - name: Copy filter lists to gh-pages
        run: |
          TMPFILE="$(mktemp -d)"
          git clone --depth=1 --branch=main https://github.com/JohnyP36/Personal-List.git "$TMPFILE"
          pushd "$TMPFILE" > /dev/null
          # ./tools/make-easydutch.sh
          echo "*** Dutch Filter List: Assembling 'Personal List (uBo).txt'"
          node ./tools/make-list.mjs in=./tools/dutch-filter-list.template out=./Dutch-Filter-List.txt
          popd > /dev/null
          cp "$TMPFILE"/Dutch-Filter-List.txt Dutch-Filter-List.txt
          cp "$TMPFILE"/easylistdutch/Anti-AdBlock.txt easylistdutch/Anti-AdBlock.txt
          cp "$TMPFILE"/easylistdutch/Block*general*.txt easylistdutch/Block_general.txt
          cp "$TMPFILE"/easylistdutch/Block*1p*server*.txt easylistdutch/Block_1p-server.txt
          cp "$TMPFILE"/easylistdutch/Block*1p*specific*.txt easylistdutch/Block_1p-specific.txt
          cp "$TMPFILE"/easylistdutch/Block*1p*whitelist*.txt easylistdutch/Block_1p-whitelist.txt
          cp "$TMPFILE"/easylistdutch/Block*3p*server*.txt easylistdutch/Block_3p-server.txt
          cp "$TMPFILE"/easylistdutch/Block*3p*specific*.txt easylistdutch/Block_3p-specific.txt
          cp "$TMPFILE"/easylistdutch/Block*3p*whitelist*.txt easylistdutch/Block_3p-whitelist.txt
          cp "$TMPFILE"/easylistdutch/Hide*general*.txt easylistdutch/Hide_general.txt
          cp "$TMPFILE"/easylistdutch/Hide*specific*.txt easylistdutch/Hide_specific.txt
          cp "$TMPFILE"/easylistdutch/Hide*whitelist*.txt easylistdutch/Hide_whitelist.txt
      - name: Patch last-updated field
        run: |
          DATE=$(date -Ru)
          for f in $(git diff --name-only); do
            STAT=$(git diff --numstat $f | sed -r '/^1\s+1\s+/d')
            if [[ -n $STAT ]]; then
              sed -ir "0,/^! Last modified: /s/^\(! Last modified: \)%timestamp%/\\1$DATE/" $f
            else
              git checkout -q $f
            fi
          done
      - name: Create new patch
        run: |
          chmod 755 ./make-diffpatch.sh
          PATCHES_DIR="patches"
          ./make-diffpatch.sh "${{ env.TAGNAME }}" "$PATCHES_DIR"
      - name: Update existing patch
        run: |
          chmod 755 ./update-diffpatches.sh
          PATCHES_DIR="patches"
          FILTER_FILES=$(git ls-files --exclude-standard -- Dutch-Filter-List.txt)
          ./update-diffpatches.sh "$GITHUB_REPOSITORY" "$PATCHES_DIR" "$FILTER_FILES"
      - name: Commit changes (if any)
        run: |
         # if [[ -n $(git diff) ]]; then
         #   git config user.name "GitHub-Actions"
         #   git config user.email "<>"
         #   git add -u easylistdutch/ 
         #   git add -u Dutch-Filter-List.txt
         #   git commit -m "Update modified filter lists"
         #   git tag ${{ env.TAGNAME }}
         #   git push origin cdn-page
         # fi
          if [[ -z $(git diff --name-only --cached) ]]; then exit 0; fi
          git config user.name "GitHub-Actions"
          git config user.email "<>"
          git commit -m "Update modified filter lists to ${{ env.TAGNAME }}"
          git push origin cdn-page
          git tag ${{ env.TAGNAME }}
          git push origin ${{ env.TAGNAME }}
