#!/bin/bash

# Check if required arguments are provided
if [ -z "$1" ] || [ -z "$2" ]; then
    echo "Usage: $0 <channel-name-or-url> <output_dir> [base_url]"
    echo "Example: $0 restisclassified ./RiC"
    echo "Example: $0 restisclassified ./RiC https://pods.attack-kitte.com/RiC"
    echo "Example: $0 https://www.youtube.com/@restisclassified/videos ./RiC https://pods.attack-kitte.com/RiC"
    exit 1
fi

# Build URL if only channel name is provided
if [[ "$1" == http* ]]; then
    URL="$1"
else
    URL="https://www.youtube.com/@${1}/videos"
fi

# Set output directory and base URL
OUTPUT_DIR="$2"
mkdir -p "$OUTPUT_DIR"

BASE_URL="${3:-https://pods.attack-kitte.com/$(basename "$OUTPUT_DIR")}"

# Download audio from YouTube channel
/home/hcooper/bin/yt-dlp \
    -o "$OUTPUT_DIR/%(upload_date)s - %(title)s - %(uploader)s.%(ext)s" \
    --embed-thumbnail \
    --embed-metadata \
    --sponsorblock-remove sponsor \
    --download-archive "$OUTPUT_DIR/history" \
    --break-on-existing \
    --extract-audio \
    --js-runtimes deno:/home/hcooper/code/node_modules/deno \
    --paths "temp:temp/" \
    "$URL"

# Generate RSS feed
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
echo ""
echo "Generating RSS feed..."
python3 "$SCRIPT_DIR/generate_rss.py" "$OUTPUT_DIR" "$BASE_URL"
