#!/bin/bash -
#===============================================================================
#
#         FILE: delete_uploaded
#
#        USAGE: ./delete_uploaded [-h] [-v] [-d {0|1}]
#
#  DESCRIPTION: Deletes HPR audio and other show-related files on the VPS
#               after their shows have been uploaded to the Internet Archive
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: Was 'delete_uploaded_new' while in development. Now replaces
#               the original 'delete_uploaded'.
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.1.4
#      CREATED: 2017-08-12 12:32:13
#     REVISION: 2022-03-30 10:57:23
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

VERSION="0.1.4"

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

STDOUT="/dev/fd/2"

#
# 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 the VPS
#
case $HOSTNAME in
    hprvps|marvin|borg) UPLOADS="/var/IA/uploads" ;;
    i7-desktop)         UPLOADS="$HOME/HPR/IA/uploads" ;;
    *)                  echo "Wrong host!"; exit 1 ;;
esac

#===  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 in "$@"; do
        [ -e "$tmp" ] && rm --force "$tmp"
    done
    exit 0
}

#===  FUNCTION  ================================================================
#         NAME: is_empty
#  DESCRIPTION: Check whether a directory is empty
#   PARAMETERS: $1      Directory to test
#      RETURNS: True if empty, otherwise false
#===============================================================================
is_empty() {
    test -z "$(find "$1" -mindepth 1 -printf X -quit)"
}

#===  FUNCTION  ================================================================
#         NAME: _usage
#  DESCRIPTION: Report usage
#   PARAMETERS: 1       [optional] exit value
#      RETURNS: Nothing
#===============================================================================
_usage () {
    local -i res="${1:-0}"

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

Usage: ./${SCRIPT} [-h] [-v] [-d {0|1}]

Deletes HPR audio and other show-related files on the VPS after their shows
have been uploaded to the Internet Archive.

Options:
  -h                    Print this help
  -v                    Run in verbose mode where more information is reported
  -d 0|1                Dry run: -d 1 (the default) runs the script in dry-run
                        mode where nothing is deleted but the actions that
                        will be taken are reported; -d 0 turns off dry-run
                        mode and the actions will be carried out.

endusage
    exit "$res"
}

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

#
# Process options
#
while getopts :d:hv opt
do
    case "${opt}" in
        d) DRYRUN=$OPTARG;;
        h) _usage 0;;
        v) VERBOSE=1;;
        *) _usage 1;;
    esac
done
shift $((OPTIND - 1))

DRYRUN=${DRYRUN:-1}
if [[ $DRYRUN -ne 0 && $DRYRUN -ne 1 ]]; then
    echo "** Use '-d 0' or '-d 1'"
    _usage 1
fi
[[ $DRYRUN -eq 1 ]] && echo "Dry run mode"

VERBOSE=${VERBOSE:-0}

#
# Should have no arguments
#
if [[ $# != 0 ]]; then
    echo "** ${SCRIPT} takes no arguments"
    _usage 1
fi

#
# Declarations
#
#re="^hpr[0-9]{4}"
declare -a dirs
lastitem=

while read -r path; do
    #
    # Extract the path relative to $UPLOADS and the IA item name from the
    # returned path
    #
    relpath="${path#"$UPLOADS"/}"
    item="${relpath:0:7}"

    [ $VERBOSE -eq 1 ] && echo "Found $path"

    #
    # Record all directories from the 'find'. Note that this means the
    # directory must begin with "^hpr[0-9]{4}", which may give a problem if
    # there's a directory that doesn't conform
    #
    if [[ -d $path ]]; then
        dirs+=("$path")
    fi

    #
    # Detect that the item prefix has changed. If it has we're processing
    # a new IA identifier, so work on this one
    #
    if [[ $item != "$lastitem" ]]; then
        lastitem=$item

        [ $VERBOSE -eq 1 ] && echo "Checking IA for $lastitem"

        if ia list "$lastitem" > "$TMP1"; then
            #
            # Scan the returned list to see if any files we have are online.
            # Delete when there's a match.
            #
            while read -r file; do
                if [[ -e "$UPLOADS/$file" ]]; then
                    #
                    # A file on the IA exists in the upload area. Delete the
                    # local one if we're not in dry-run mode, otherwise just
                    # report the deletion we would do.
                    #
                    if [[ $DRYRUN -eq 0 ]]; then
                        rm -f "$UPLOADS/$file"
                        echo "Deleted $UPLOADS/$file"
                    else
                        echo "Would delete $UPLOADS/$file"
                    fi
                fi
            done < "$TMP1"
        else
            #
            # End the outer 'while' loop because we hit an item not on the IA.
            # We rely on the list being sorted for this to be sensible
            #
            [ $VERBOSE -eq 1 ] && echo "Item not found on IA: $lastitem"
            break
        fi
    else
        #
        # Ignore all but the first file belonging to an IA identifier
        #
        [ $VERBOSE -eq 1 ] && echo "Skipped $path"
        continue
    fi

done < <(find "$UPLOADS" -regextype posix-extended -regex '.*hpr[0-9]{4}.*' | sort)

#
# Clean up any empty directories
#
for dir in "${dirs[@]}"; do
    if is_empty "$dir"; then
        if [[ $DRYRUN -eq 0 ]]; then
            rmdir "$dir"
            echo "Deleted $dir"
        else
            echo "Would delete $dir"
        fi
    fi
done

exit

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