name: "Discord"

on:
  workflow_dispatch:
    inputs:
      skip_post:
        description: "Skip posting to Social Media"
        required: false
        type: boolean
  schedule:
    # - cron: "0 */1 * * *" # every 1 hour
    - cron: "*/30 * * * *" # every 30 minutes
    # - cron: "0 0 * * *"

permissions:
  contents: write

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          fetch-depth: 0
      - name: Check Cache
        run: |
          cat hack/.watch_cache || echo "No cache found"
      - name: Set up Go
        uses: actions/setup-go@v7
        with:
          go-version: "1.26"
      - name: Run ipsw watch WebKit/WebKit (Security Features)
        run: |
          go run ./cmd/ipsw/main.go watch WebKit/WebKit --pattern '(?i)Lockdown Mode|Memory Tagging|EnhancedSecurity|Enhanced Security|(?-i:MTE)' --days 1 \
          --cache hack/.watch_cache \
          --api ${{ secrets.GITHUB_TOKEN }} \
          --discord \
          --discord-id ${{ secrets.DISCORD_ID }} \
          --discord-token ${{ secrets.DISCORD_TOKEN }} \
          --discord-icon "https://raw.githubusercontent.com/blacktop/ipsw/master/www/static/img/webkit.png"
      - name: Run ipsw watch WebKit/WebKit tags
        if: ${{ !inputs.skip_post }}
        run: |
          go run ./cmd/ipsw/main.go watch WebKit/WebKit --tags \
          --cache hack/.watch_cache \
          --api ${{ secrets.GITHUB_TOKEN }} \
          --discord \
          --discord-id ${{ secrets.DISCORD_ID }} \
          --discord-token ${{ secrets.DISCORD_TOKEN }} \
          --discord-icon "https://raw.githubusercontent.com/blacktop/ipsw/master/www/static/img/webkit.png"
      - name: Run ipsw watch apple-oss-distributions/distribution-macOS
        if: ${{ !inputs.skip_post }}
        run: |
          go run ./cmd/ipsw/main.go watch apple-oss-distributions/distribution-macOS --tags --post \
          --cache hack/.watch_cache \
          --api ${{ secrets.GITHUB_TOKEN }} \
          --discord \
          --discord-id ${{ secrets.DISCORD_ID }} \
          --discord-token ${{ secrets.DISCORD_TOKEN }} \
          --discord-icon "https://avatars.githubusercontent.com/u/91919287"
        # --mastodon \
        # --mastodon-client-id ${{ secrets.MASTODON_CLIENT_ID }} \
        # --mastodon-client-secret ${{ secrets.MASTODON_CLIENT_SECRET }} \
        # --mastodon-access-token ${{ secrets.MASTODON_ACCESS_TOKEN }}
      - name: Run ipsw watch apple-oss-distributions/distribution-macOS (SKIP MASTODON)
        if: ${{ inputs.skip_post }}
        run: |
          go run ./cmd/ipsw/main.go watch apple-oss-distributions/distribution-macOS --tags --post \
          --cache hack/.watch_cache \
          --api ${{ secrets.GITHUB_TOKEN }} \
          --discord \
          --discord-id ${{ secrets.DISCORD_ID }} \
          --discord-token ${{ secrets.DISCORD_TOKEN }} \
          --discord-icon "https://avatars.githubusercontent.com/u/91919287"
        # --mastodon \
        # --mastodon-client-id ${{ secrets.MASTODON_CLIENT_ID }} \
        # --mastodon-client-secret ${{ secrets.MASTODON_CLIENT_SECRET }} \
        # --mastodon-access-token ${{ secrets.MASTODON_ACCESS_TOKEN }}
      - uses: stefanzweifel/git-auto-commit-action@4a55954c782fc1ea30b9056cd3e7a2b40ca8887d # v5
        with:
          commit_message: Update .watch_cache
          file_pattern: hack/.watch_cache
      # - name: Commit Cache Changes
      #   run:
      #     if [[ -n $(git status --porcelain) ]]; then
      #       git config --global user.email "github-actions[bot]@users.noreply.github.com"
      #       git config --global user.name "github-actions[bot]"
      #       git add hack/.watch_cache
      #       git commit -m "Update .watch_cache"
      #       git push
      #     fi
