#!/bin/bash

if [ -z "$1" ] || [ $1 == "-h" ]; then
    echo -e "Usage: yt <url>\n\nurl can be any link to a video, playlist, or account on most video sites like youtube, odysee, rumble, etc."
    exit 1
fi

url="$1"

echo -e "What Quality?\n[1] Best\n[2] Medium\n[3] Low (default)\n[4] Audio\n"
echo -n "Enter Your Selection: "
read quality
quality=${quality:-3}

if [ "$quality" == "1" ]; then
	quality_flag="-f bv+ba/b"
elif [ "$quality" == "2" ]; then
	quality_flag="-f bv[height<=720]+ba/b[height<=720]"
elif [ "$quality" == "3" ]; then
	quality_flag="-f bv[height<=480]+ba/b[height<=480]"
elif [ "$quality" == "4" ]; then
	quality_flag="-x --audio-format mp3"
else
	echo "Your quality selection is bad"
	exit 1
fi

echo -ne "\nDo you want an archive? [Y/n] "
read archive
archive=$(tr '[:upper:]' '[:lower:]' <<< "$archive")
archive=${archive:-y}

if [ "$archive" == "y" ]; then
	archive_flag="--download-archive archive.txt --no-overwrites --add-metadata"
	echo -ne "\nDo you want to create a directory for this? [Y/n] "
	read directory
	directory=${directory:-y}
else
	archive_flag=""
	directory="n"
fi

directory=$(tr '[:upper:]' '[:lower:]' <<< "$directory")

if [ "$directory" == "y" ]; then
	echo -ne "\nDirectory name: "
	read directory_name
	directory_name=${directory_name:-y}
	mkdir -p "$directory_name"
	cd "$directory_name"
fi

yt-dlp ${quality_flag} ${archive_flag} ${url}
