#!/bin/bash -
#===============================================================================
#
#         FILE: collect_show_data
#
#        USAGE: ./collect_show_data fromshow [toshow]
#
#  DESCRIPTION: Capture metadata for a range of shows for adding to the ia.db
#               database. Do it by show number rather than by date (see
#               'collect_metadata')
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.5
#      CREATED: 2018-01-27 14:13:19
#     REVISION: 2023-01-18 12:30:17
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

VERSION="0.0.5"

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

showmax=10

#
# We need a 'from' and 'to' argument as show numbers
#
if [[ $# -ne 1 && $# -ne 2 ]]; then
    echo -e "Usage: $SCRIPT fromshow [toshow]\\n(Version $VERSION)"
    exit 1
fi

#
# Validate the arguments
#
for arg; do
    if [[ ! $arg =~ ^[0-9]{1,4}$ ]]; then
        echo "Invalid show number: $arg"
        echo "Use a plain number"
        exit 1
    fi
done

#
# Save the arguments
#
from="$1"
if [[ $# -eq 1 ]]; then
    to="$1"
else
    to="$2"
fi

#
# Check the arguments are in the right order
#
if [[ $from -gt $to ]]; then
    echo "First argument must be less than or equal to second"
    exit 1
fi

#
# Make an array with the range in it. Ensure numbers have leading zeroes
#
mapfile ids < <(eval "printf '%04d\n' {$from..$to}")

#
# Check the request wasn't too big
#
if [[ ${#ids[@]} -gt $showmax ]]; then
    echo "Too many shows requested; limit $showmax"
    exit 1
fi

#
# Output file - with leading zeroes in numbers
#
printf -v metadata 'ia_metadata_hpr%04d-hpr%04d.json' "$from" "$to"

echo "From $from, to $to -> $metadata"

#
# Make the request
#
ia metadata "${ids[@]/#/hpr}" > "$metadata" 2> /dev/null
RES=$?

if [[ $RES -ne 0 ]]; then
    echo "Data collection has failed"
    if [[ ! -s $metadata ]]; then
        rm -f "$metadata"
    fi
else
    echo "Metadata is in $metadata"
fi

exit

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

