#!/bin/bash
#
# Copyright (C) 2023-2026 Eugene 'Vindex' Stulin
# Distributed under the Boost Software License 1.0.

set -eu -o pipefail

# The function prints help information.
Print_Help() {
cat <<EndOfHelp
This script downloads video from video platforms using yt-dlp.
Main page of yt-dlp:
https://github.com/yt-dlp/yt-dlp

This wrapper sets a special name format for downloaded files:
name stores upload date and title.

yt uses default quality.
yt-360 tries to download the video with a frame height of 360 pixels.
yt-480 tries to download the video with a frame height of 480 pixels.
yt-720 tries to download the video with a frame height of 720 pixels.
yt-1080 tries to download the video with a frame height of 1080 pixels.
yt-1440 tries to download the video with a frame height of 1440 pixels.
yt-2160 tries to download the video with a frame height of 2160 pixels.
yt-a downloads audiotrack only.

Usage:
    ${0##*/} [-D] [-o <output-file>] <URL> [yt-dlp options...]

Flag '-D' enables upload date to the output file name.
Options ('-D' and '-o') must precede the URL.

Additional arguments will be passed to the yt-dlp call.

Help and version:
    ${0##*/} --help|-h
    ${0##*/} --version|-v
EndOfHelp
}


# The function prints the script version.
Print_Version() {
    local VERSION_SCRIPT="$(dirname -- "${BASH_SOURCE[0]}")/bbsi-version"
    if [[ ! -x "$VERSION_SCRIPT" ]]; then
        echo "Unknown version."
    else
        "$VERSION_SCRIPT"
    fi
}


# The function prints information about wrong usage to stderr.
Incorrect_Usage() {
    echo "Incorrect usage. See: ${0##*/} --help" >&2
}


# parse command line arguments
if [[ $# -eq 1 ]]; then
    if [[ "$1" == "-h" || "$1" == "--help" ]]; then
        Print_Help
        exit 0
    elif [[ "$1" == "-v" || "$1" == "--version" ]]; then
        Print_Version
        exit 0
    fi
fi
DATE_ENABLED=FALSE
CUSTOM_PATH=""
while true; do
    case "$1" in
        '-D')
            DATE_ENABLED=TRUE
            shift
            continue
            ;;
        '-o')
            CUSTOM_PATH="$2"
            shift 2
            continue
            ;;
        '--')
            shift
            break
            ;;
        *)
            break
            ;;
    esac
done

if [[ $# -eq 0 ]]; then
    Incorrect_Usage
    exit 1
fi


Interrupt_Execution() {
    set +x
    echo "The script has been interrupted." 2>&1
    exit 1
}
trap Interrupt_Execution ABRT INT QUIT TERM


UTIL=yt-dlp
BBSI_CACHE_DIR=~/.cache/bbsi
PYTHON_ENV="${BBSI_CACHE_DIR}/python_env"
mkdir -p "${PYTHON_ENV}"
virtualenv "${PYTHON_ENV}"
source "${PYTHON_ENV}/bin/activate"
readonly SIGS="EXIT HUP INT TERM ERR"
pip3 install --upgrade "$UTIL"

if [[ -z "$CUSTOM_PATH" ]]; then
    FMT="%(title)s.%(ext)s"
else
    FMT="$CUSTOM_PATH"
fi
if [[ "$DATE_ENABLED" == TRUE ]]; then
    FMT="%(upload_date)s - $FMT"
fi
URL="$1"
shift

SCRIPT_NAME="${0##*/}"
YT="$(dirname "$0")/yt"
if [[ "$SCRIPT_NAME" == yt-a ]]; then
    "$UTIL" -x "$URL" -o "$FMT" -f bestaudio
elif [[ "$SCRIPT_NAME" == yt ]]; then
    "$UTIL" "$URL" "$@" -o "$FMT" --merge-output-format mp4 --embed-subs
else
    R=$(echo "$SCRIPT_NAME" | awk -F'-' '{print $2}')  # from 'yt-720' to '720'
    "$YT" "$URL" "$@" -S "res:$R" --merge-output-format mp4 --embed-subs
fi
