#!/usr/bin/env nix-shell
#! nix-shell -i bash -p bash yt-dlp

minutes=5
time_alive=60
sleep_time=$((minutes * 60))
loops=$((time_alive / (sleep_time / time_alive)))

re="[[:space:]]+"
echo "$1"
if [[ $1 =~ $re ]]; then
    read -ra arr <<<"$1"
    url="https://picarto.tv/${arr[0]}"
else
    url="https://chaturbate.com/$1"
fi

save_dir=/srv/pool/glue/stream-dl
if [ ! -d "$save_dir" ]; then
    mkdir -p "$save_dir"
fi
cd $save_dir || exit

for i in $(seq 1 1 "$loops"); do
    waiting_time=$(((i * sleep_time) / time_alive))
    yt-dlp --hls-use-mpegts --prefer-ffmpeg -o '%(title)s.%(ext)s' "$url"
    echo "sleeping for $sleep_time seconds… been waiting for $waiting_time minutes"
    sleep $sleep_time
done
