#!/bin/bash -
#===============================================================================
#
#         FILE: show_queue
#
#        USAGE: ./show_queue
#
#  DESCRIPTION: Show the pending queue, expanding each album's details from
#               the 'all_albums' file
#               / This is the version for the Magnatune XML catalog /
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.2
#      CREATED: 2017-11-14 16:31:46
#     REVISION: 2018-11-24 14:51:57
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

SCRIPT=${0##*/}
#DIR=${0%/*}

#
# Files and directories
#
BASEDIR="$HOME/MusicDownloads"
DATADIR="$BASEDIR/Magnatune_Data"
SCRIPTDIR="$BASEDIR/magnatune-downloader"

SUMMARY="$DATADIR/all_albums"
QUEUE="$SCRIPTDIR/pending"

#
# Sanity checks
#
[ -e "$QUEUE" ] || { echo "$QUEUE not found"; exit 1; }
[ -e "$SUMMARY" ] || { echo "$SUMMARY not found"; exit 1; }

#
# Check the queue contains data
#
if [[ ! -s $QUEUE ]]; then
    echo "$SCRIPT: there is nothing in the queue"
    exit
fi

RE='^http://magnatune.com/artists/albums/([A-Za-z0-9-]+)/?$'

#
# Read and report the queue elements
#
n=0
while read -r URL; do
    ((n++))

    if [[ $URL =~ $RE ]]; then
        SKU="${BASH_REMATCH[1]}"
    else
        echo "Problem parsing URL in queue (line $n): $URL"
        continue
    fi

    #
    # The SKU should be the entire thing, so we surround it with word
    # boundaries so we don't match other variants.
    #
    awk 'BEGIN{RS = "\n\n"; ORS = "\n----\n"} /Code: +\<'"$SKU"'\>/{print}' "$SUMMARY"
done < "$QUEUE"

exit

# vim: syntax=sh:ts=8:sw=4:ai:et:tw=78:fo=tcrqn21

