#!/bin/bash

MEDIA_DIR="/home/marcus/media"
BASE_URL="https://media.petterssonhome.se"
OUTPUT="/home/marcus/media/iptv.m3u"

printf "#EXTM3U\n" > "$OUTPUT"

find "$MEDIA_DIR" -type f \( -iname "*.mp4" -o -iname "*.mkv" -o -iname "*.avi" \) | sort | while read -r file; do
  rel="${file#$MEDIA_DIR}"

  # FULL URL encoding (Android IPTV safe)
  url=$(python3 - <<EOF
import urllib.parse
print(urllib.parse.quote("$rel"))
EOF
)

  name=$(basename "$file")

  printf "#EXTINF:-1,%s\n" "$name" >> "$OUTPUT"
  printf "%s%s\n" "$BASE_URL" "$url" >> "$OUTPUT"
done

echo "✔ Android-safe IPTV playlist generated"
