#!/bin/sh

set -eu

# Load helper functions
source "$(dirname "$(rreadlink "$0")")/../lib/utils.sh"

usage() {
    >&2 echo "Usage: $0 [-d folder_dest] <video url>"
    exit 1
}

dest="$HOME/mus/download/"
url=""

while [ $# -gt 0 ]; do
    arg="$1"
    case $arg in
    -h|--help)
        usage
        ;;
    -d)
        dest="$2"
        shift 2
        ;;
    *)
        url="$1"
        shift
        ;;
    esac
done

if [ -z "$url" ]; then
    usage
fi

[ -d "$dest" ] || mkdir -p "$dest"

notify-send -t 3000 "mp3dl" "Downloading $url..."

youtube-dl -x --audio-format mp3 --no-playlist --add-metadata -o "$dest/%(title)s.%(ext)s" "$url" 2>&1 \
    | grep 'ERROR:' \
    | notify_err