#!/bin/bash -
#===============================================================================
#
#         FILE: generate_asset_list
#
#        USAGE: ./generate_asset_list episode [episode [episode ...]]
#
#  DESCRIPTION: Produces SQL for uploading assets for a show without them.
#               This is very rough and ready and can only be run on 'borg'
#               with access to the 'uploads' and 'done' directories. It's
#               based on Ken Fallon's suggested way of doing this, but has
#               been adjusted somewhat.
#
#               2023-08-17: Modified to accept multiple episodes per run.
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.4
#      CREATED: 2023-06-26 21:50:14
#     REVISION: 2023-08-18 00:00:13
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

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

# VERSION="0.0.4"

#
# Needs an argument
#
if [[ $# -eq 0 ]]; then
    echo "Usage: $SCRIPT episode [episode [episode ...]]"
    exit 1
fi

#
# 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

#
# Configure depending whether local or on borg
#
case $HOSTNAME in
    borg)
        BASEDIR="$HOME/IA"
        UPLOADS="/data/IA/uploads"
        DONE="/data/IA/done"
        ;;
    i7-desktop)
        BASEDIR="$HOME/HPR/IA"
        UPLOADS="$BASEDIR/uploads"
        DONE="$BASEDIR/done"
        ;;
    *)
        echo "Wrong host!"
        exit 1
        ;;
esac

#
# It's easiest to work here
#
cd "$BASEDIR" || { echo "Failed to cd to $BASEDIR"; exit 1; }

#===  FUNCTION  ================================================================
#         NAME: cleanup_temp
#  DESCRIPTION: Cleanup temporary files in case of a keyboard interrupt
#               (SIGINT) or a termination signal (SIGTERM) and at script
#               exit
#   PARAMETERS: * - names of temporary files to delete
#      RETURNS: Nothing
#===============================================================================
function cleanup_temp {
    for tmp; do
        [ -e "$tmp" ] && rm --force "$tmp"
    done
    exit 0
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#
# Count number of arguments processed
#
argcount=0

#
# Loop through arguments
#
for arg; do
    failed=0

    #
    # Accept a bare number or 'hpr<number>'. Parse out the pieces we'll need later
    #
    if [[ $arg =~ (hpr)?([0-9]{1,4}) ]]; then
        printf -v show 'hpr%04d' "${BASH_REMATCH[2]}"
        printf -v episode_id '%d' "${BASH_REMATCH[2]}"
    else
        echo "Argument '$arg' must be '<number>' or hpr<number>'"
        echo "Skipping this argument"
        continue
    fi

    #
    # Argument is OK, so prepare for the inner loop
    #
    echo 'INSERT INTO assets (episode_id,filename,extension,size,sha1sum,mime_type,file_type) VALUES' >> "$TMP1"

    #
    # Loop through the audio types. Keep in this order since the 'wav'
    # extension needs to be the last one.
    #
    for extension in mp3 ogg spx flac opus wav; do

        f1="$UPLOADS/${show}.${extension}"
        f2="$DONE/${show}.${extension}"

        #
        # Find the file in either of the two directories.
        # If any file type in the list is missing abort.
        # TODO: This will leave a partial output which isn't good!
        #
        if [[ -f "$f1" ]]; then
            filename="$f1"
        elif [[ -f "$f2" ]]; then
            filename="$f2"
        else
            echo "Can't find $f1 or $f2"
            echo "Aborting the processing of $show"
            failed=1
            break
        fi

        #
        # Collect file statistics and attributes
        #
        size="$( stat --printf='%s' "${filename}" )"
        sha1sum="$( sha1sum "${filename}" | cut -f1 -d' ' )"
        mime_type=$( file --dereference --brief --mime "${filename}" )
        file_type=$( file --dereference --brief "${filename}" )

        #
        # Generate the next list of values to insert
        #
        printf "(%s,'%s','%s',%s,'%s','%s','%s')" \
            "${episode_id}" "${show}.${extension}" "${extension}" "${size}" "${sha1sum}" "${mime_type}" "${file_type}" >> "$TMP1"

        #
        # End with a comma or a semicolon
        #
        if [[ $extension == 'wav' ]]; then
            echo ";" >> "$TMP1"
        else
            echo "," >> "$TMP1"
        fi

    done

    #
    # Add the `UPDATE` too, unless we failed to find a file and aborted.
    #
    if [[ $failed -eq 0 ]]; then
        printf 'UPDATE eps SET valid = 1 WHERE id = %s;\n' "${episode_id}" >> "$TMP1"
        ((argcount++))
    fi

done

if [[ $argcount -gt 0 ]]; then
    cat "$TMP1"
fi

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