#!/usr/bin/env bash

function usage {
    cat <<EOF
yt-album [-v] [--artist|-a <Artist>] <youtube playlist URL>

Give the script a playlist URL, and it will download all songs in the playlist
in m4a format and properly set the metadata for them.

EXAMPLE

To download the Blue Album from Weezer:

  yt-album -a Weezer 'https://www.youtube.com/watch?v=b45rzwIBS_Y&list=PL81_CtYCym2_indonS249kcDhb04JVSy6'
EOF
  exit 0
}

ARTIST=
while true; do
    case $1 in
        help | -h | --help)
            usage
            ;;
        -v | --verbose)
            set -x
            shift
            ;;
        -a | --artist)
            ARTIST=$2
            shift 2
            ;;
        *)
            break
            ;;
    esac
done

if [[ -z "$1" ]]; then
    echo "Error: YouTube URL is required" >&2
    usage
fi

URL_HASH=$(echo "$1" | md5sum | cut -d' ' -f1)
DOWNLOAD_ARCHIVE="/tmp/yt_album_downloaded_${URL_HASH}.txt"

yt-dlp \
  --download-archive "$DOWNLOAD_ARCHIVE" \
  --extract-audio \
  --audio-format m4a \
  --embed-metadata \
  --parse-metadata "playlist_index:%(track_number)s" \
  --parse-metadata "playlist_title:%(album)s" \
  --embed-thumbnail \
  ${ARTIST:+--ppa "ffmpeg:-metadata artist='$ARTIST'"} \
  -o '%(playlist_index)s. %(title)s.%(ext)s' \
  "$1"
