#!/bin/bash -
#===============================================================================
#
#         FILE: replace_derived
#
#        USAGE: ./replace_derived show_number
#
#  DESCRIPTION: Given a show that has already been uploaded to the IA, upload
#               a locally derived set of files to replace those generated by
#               the IA themselves. The IA's process of deriving files does not
#               propagate the tags, which we really want to do.
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: This is to be run on the HPR VPS
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.5
#      CREATED: 2018-01-05 19:08:32
#     REVISION: 2019-01-27 15:55:58
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

VERSION="0.0.5"

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

STDOUT="/dev/fd/2"

#
# Select the appropriate working directory
#
case $(hostname) in
    i7-desktop)         BASEDIR="$HOME/HPR/InternetArchive";;
    hprvps|marvin)      BASEDIR="$HOME/IA";;
    *)                  echo "Wrong host!"; exit 1;;
esac

cd "$BASEDIR" || exit 1

#===  FUNCTION  ================================================================
#         NAME: yes_no
#  DESCRIPTION: Read a Yes or No response from STDIN (only these values are
#               accepted) and return a suitable numeric value.
#   PARAMETERS: 1 - Prompt string for the read
#               2 - Default value (optional)
#      RETURNS: 0 for a response of Y or YES, 1 otherwise
#===============================================================================
yes_no () {
    local prompt="${1:?Usage: ${FUNCNAME[0]} prompt [default]}"
    local default="${2^^}"
    local ans res

    if [[ $prompt =~ %s ]]; then
        if [[ -n $default ]]; then
            default=${default:0:1}
            # shellcheck disable=SC2059
            # {
            case "$default" in
                Y) printf -v prompt "$prompt" "[Y/n]" ;;
                N) printf -v prompt "$prompt" "[y/N]" ;;
                *) echo "Error: ${FUNCNAME[0]} @ line ${BASH_LINENO[0]}: Default must be 'Y' or 'N'"
                   exit 1
                   ;;
            esac
            # }
        else
            echo "Error: ${FUNCNAME[0]} @ line ${BASH_LINENO[0]}: Default required"
            exit 1
        fi
    fi

    #
    # Loop until a valid input is received
    #
    while true; do
        #
        # Read and handle CTRL-D (EOF)
        #
        read -r -e -p "$prompt" ans
        res="$?"
        if [[ $res -ne 0 ]]; then
            echo "Read aborted"
            return 1
        fi

        [ -z "$ans" ] && ans="$default"

        #
        # Look for valid replies and return appropriate values. Print an error
        # message otherwise and loop around for another go
        #
        if [[ ${ans^^} =~ ^Y(E|ES)?$ ]]; then
            return 0
        elif [[ ${ans^^} =~ ^NO?$ ]]; then
            return 1
        else
            echo "Invalid reply; please use 'Y' or 'N'"
        fi
    done
}

#===  FUNCTION  ================================================================
#         NAME: _usage
#  DESCRIPTION: Report usage
#   PARAMETERS: None
#      RETURNS: Nothing
#===============================================================================
_usage () {
    local exitcode=${1:-0}

    cat >$STDOUT <<-endusage
${SCRIPT} - version: ${VERSION}

Usage: ./${SCRIPT} show_number

For a show already on the archive it determines if the files are those derived
by the IA software and if not collects the WAV file (if necessary) and
generates all the derivatives (if necessary), then it uploads these
replacements. This is necessary because the IA's derivation process does not
copy the audio tags across to the derived files whereas we do.

endusage
    exit "$exitcode"
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#
# Make temporary files and set traps to delete them
#
# TMP1=$(mktemp) || { echo "$SCRIPT: creation of temporary file failed!"; exit 1; }
# trap 'cleanup_temp $TMP1' SIGHUP SIGINT SIGPIPE SIGTERM EXIT

UPLOADS="/var/IA/uploads"

#
# Don't run on the wrong system
#
HOST=${HOSTNAME:-${hostname}}
if [[ $HOST != 'hprvps' ]]; then
    echo "This should be run on the HPR VPS"
    exit 1
fi

#
# We need a show number argument
#
if [[ $# -ne 1 ]]; then
    _usage 1
fi

show_number=$1

#
# Tools we need: the Awk script 'parse_ia_audio.awk', and the script
# 'transcode'
#
PARSE_IA_AUDIO="$BASEDIR/parse_ia_audio.awk"
[ -e "$PARSE_IA_AUDIO" ] || { echo "$SCRIPT: Unable to find $PARSE_IA_AUDIO"; exit 1; }
TRANSCODE="$BASEDIR/transcode"
[ -e "$TRANSCODE" ] || { echo "$SCRIPT: Unable to find $TRANSCODE"; exit 1; }

#
# Build the file path and the identifier we're looking for
#
WAV="$UPLOADS/hpr${show_number}.wav"
IDENTIFIER="hpr${show_number}"

#
# Look for the WAV file in the upload area
#
if [[ -e $WAV ]]; then
    echo "The WAV file already exists"
else
    #
    # Not found. Check it's on the archive
    #
    if ia metadata --exists "$IDENTIFIER" > /dev/null 2>&1; then
        #
        # It's there. Report the status of original and derived
        #
        echo "Need to download ${WAV##*/}"
        ia list -va "$IDENTIFIER" | $PARSE_IA_AUDIO

        #
        # Download the WAV
        #
        if yes_no "OK to download? %s " N; then
            echo "Downloading"
            ia download "$IDENTIFIER" "${WAV##*/}" --stdout > "$WAV"
            RES=$?
            [ $RES -gt 0 ] && { echo "Download failed"; exit 1; }
        else
            echo "Download cancelled"
            exit 1
        fi
    else
        echo "This episode is not in the archive"
        exit 1
    fi
fi

#
# Did we already transcode this one?
#
tally=0
for fmt in flac mp3 ogg opus spx; do
    if [[ -e "$UPLOADS/hpr${show_number}.${fmt}" ]]; then
        ((tally++))
    fi
done

#
# We have a WAV file so transcode it unless we found transcoded files
#
if [[ $tally -gt 0 ]]; then
    echo "There are already $tally derived files for this show"
    echo "Not transcoding"
else
    if yes_no "OK to transcode? %s " N; then
        $TRANSCODE "$WAV"
    else
        echo "Transcode cancelled"
        exit 1
    fi
fi

if yes_no "OK to upload? %s " N; then
    ia upload "$IDENTIFIER" "${WAV%wav}"{flac,mp3,ogg,opus,spx}
else
    echo "Upload cancelled"
    exit 1
fi

if yes_no "OK to delete WAV and derived files? %s " N; then
    rm -f "${WAV%wav}"*
else
    echo "Deletion cancelled"
    exit 1
fi

exit

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