#!/bin/bash

BASE_DIR="/var/www/html/emisiuni/hls"
LIST="$BASE_DIR/emisiuni.txt"

mkdir -p "$BASE_DIR"

########################################
# VERIFICARE LISTĂ
########################################

if [ ! -f "$LIST" ]; then
    echo "Nu exista video_list.txt"
    exit 1
fi

########################################
# LOOP TV
########################################

while true; do
    echo "=== PORNESC PLAYLIST ==="

    # shuffle lista pentru random fără repetare
    shuf "$LIST" > /tmp/shuffled_list.txt

    while read URL; do
        echo "Rulez: $URL"

        STREAM=$(yt-dlp -f "best[height<=720]/best" -g "$URL" 2>/dev/null)

        if [ -z "$STREAM" ]; then
            echo "SKIP"
            continue
        fi

        ffmpeg -re -i "$STREAM" \
            -c copy \
            -f hls \
            -hls_time 10 \
            -hls_list_size 5 \
            -hls_flags delete_segments+append_list \
            "$BASE_DIR/stream.m3u8"

        sleep 1

    done < /tmp/shuffled_list.txt

    echo "=== REIAU LISTA (RESHUFFLE) ==="

done
