name: Generate TrollFools/Cyan files

on:
  workflow_dispatch:
    inputs:
      create_cyan:
        description: "Generate .cyan file"
        type: boolean
        required: true
        default: true

      create_tf:
        description: "Generate .zip for TrollFools"
        type: boolean
        required: true
        default: true

      enable_youpip:
        description: "Integrate YouPiP"
        type: boolean
        required: true
        default: false

      enable_ytuhd:
        description: "Integrate YTUHD"
        type: boolean
        required: true
        default: false

      enable_yq:
        description: "Integrate YouQuality"
        type: boolean
        required: true
        default: false

      enable_ryd:
        description: "Integrate Return YouTube Dislikes"
        type: boolean
        required: true
        default: false

      enable_ytabc:
        description: "Integrate YTABConfig"
        type: boolean
        required: true
        default: false

      enable_demc:
        description: "Integrate DontEatMyContent"
        type: boolean
        required: true
        default: false

      tweak_version:
        description: "The version of the tweak to use. Enter the version manually from dayanch96/YTLite/releases or leave default"
        default: "5.2b4"
        required: true
        type: string

      display_name:
        description: "App Name (Optional)"
        default: "YouTube"
        required: true
        type: string

      bundle_id:
        description: "BundleID (Optional)"
        default: "com.google.ios.youtube"
        required: true
        type: string

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build-tweaks:
    uses: ./.github/workflows/_build_tweaks.yml
    with:
      enable_youpip: ${{ inputs.enable_youpip }}
      enable_ytuhd: ${{ inputs.enable_ytuhd }}
      enable_yq: ${{ inputs.enable_yq }}
      enable_ryd: ${{ inputs.enable_ryd }}
      enable_ytabc: ${{ inputs.enable_ytabc }}
      enable_demc: ${{ inputs.enable_demc }}
      tweak_version: ${{ inputs.tweak_version }}

  package:
    name: Generate Cyan/TrollFools
    runs-on: macos-latest
    needs: build-tweaks
    permissions:
      contents: write

    steps:
      - name: Download built debs
        uses: actions/download-artifact@v7
        with:
          name: built-debs
          path: ${{ github.workspace }}

      - name: Check Inputs
        if: ${{ !inputs.create_cyan && !inputs.create_tf }}
        run: |
          echo "No output format selected."
          exit 0

      - name: Install Dependencies
        run: brew install dpkg

      - name: Install cyan
        if: ${{ github.event.inputs.create_cyan }}
        run: pipx install --force https://github.com/asdfzxcvbn/pyzule-rw/archive/main.zip

      - name: Generate .cyan file
        if: ${{ github.event.inputs.create_cyan }}
        run: |
          tweaks="ytplus.deb OpenYoutubeSafariExtension.appex"
          for f in *.deb; do
            [ -f "$f" ] && tweaks="$tweaks $f"
          done
          cgen -uwef $tweaks -n "${{ github.event.inputs.display_name }}" -b ${{ github.event.inputs.bundle_id }} -o cyan_YouTubePlus_${{ github.event.inputs.tweak_version }}.cyan

      - name: Generate .zip file for TrollFools
        if: ${{ github.event.inputs.create_tf }}
        run: |
          mkdir -p ytp_tf
          for f in *.deb; do
            if [ -f "$f" ]; then
              mkdir -p tmp_deb
              dpkg-deb -x "$f" tmp_deb
              find tmp_deb -type f -name "*.dylib" -exec cp -R {} ytp_tf/ \;
              find tmp_deb -type d -name "*.bundle" -exec cp -R {} ytp_tf/ \;
              rm -rf tmp_deb
            fi
          done
          cd ytp_tf
          zip -r "../TrollFools_YouTubePlus_${{ github.event.inputs.tweak_version }}.zip" .
          cd ..
          rm -rf ytp_tf

      - name: Set output file list
        id: filelist
        run: |
          files=""
          [ "${{ github.event.inputs.create_cyan }}" = "true" ] && files="${files}cyan_YouTubePlus_${{ github.event.inputs.tweak_version }}.cyan"$'\n'
          [ "${{ github.event.inputs.create_tf }}" = "true" ]   && files="${files}TrollFools_YouTubePlus_${{ github.event.inputs.tweak_version }}.zip"$'\n'
          echo -e "files<<EOF" >> $GITHUB_OUTPUT
          echo -e "$files" >> $GITHUB_OUTPUT
          echo "EOF" >> $GITHUB_OUTPUT

      - name: Upload to GitHub Releases
        uses: softprops/action-gh-release@v2.5.0
        with:
          tag_name: ytp-${{ github.run_number }}
          name: YouTubePlus v${{ github.event.inputs.tweak_version }}_cyan_trollfools (${{ github.run_number }})
          files: ${{ steps.filelist.outputs.files }}
          draft: true

      - name: Output Release URL
        run: |
          echo "::notice::Release available at: https://github.com/${{ github.repository }}/releases"