#!/bin/bash - 
#===============================================================================
#
#         FILE: move_shownotes
#
#        USAGE: ./move_shownotes original_show new_show
#
#  DESCRIPTION: After a show has been moved from one slot to another on the
#               HPR server this script ensures that the local versions of
#               processed show notes correspond to the change by moving
#               directories around and correcting file names and some
#               contents.
#
#               ** Under development **
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.2
#      CREATED: 2022-12-02 22:12:20
#     REVISION: 2022-12-06 23:03:12
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

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

VERSION='0.0.2'

STDOUT="/dev/fd/2"

#
# Load library functions
#
LIB="$HOME/bin/function_lib.sh"
[ -e "$LIB" ] || { echo "$SCRIPT: Unable to source functions"; exit 1; }
# shellcheck source=/home/cendjm/bin/function_lib.sh
source "$LIB"

#
# Colour codes
#
define_colours

#===  FUNCTION  ================================================================
#         NAME: _usage
#  DESCRIPTION: Reports usage; always exits the script after doing so
#   PARAMETERS: 1 - the integer to pass to the 'exit' command
#      RETURNS: Nothing
#===============================================================================
_usage () {
    local -i result=${1:-0}

    cat >$STDOUT <<-endusage
Usage: ./${SCRIPT} [-h] [-d] [-D] original_show new_show

Version: $VERSION

When a show has been processed and posted on the server, but needs to be moved
to another slot (after getting the host's permission) this script assists with
the processes involved in ensuring the local shownote caches reflect the
server state.

The need to move a show is rare, but often follows when a host has forgotten
the request to spread their shows out by at least two weeks and has sent them
in to clots too close in time to one another.

Options:
  -h                    Print this help
  -d                    Select dry run mode
  -D                    Turn on debug mode with lots of extra output

Arguments:
    original_show
    new_show

 Both arguments can use either an integer number or a format like 'hpr9999'
 and also compensate for the use of 'HPR' rather than 'hpr'.

Examples
  ./${SCRIPT} -h
  ./${SCRIPT} -d 3742 3756
  ./${SCRIPT} -D 3738 3746

endusage
    exit "$result"

}

#===  FUNCTION  ================================================================
#         NAME: _DEBUG
#  DESCRIPTION: Writes one or more message lines if in DEBUG mode
#   PARAMETERS: List of messages
#      RETURNS: Nothing
#===============================================================================
_DEBUG () {
    [ "$DEBUG" == 0 ] && return
    for msg in "$@"; do
        printf 'D> %s\n' "$msg"
    done
}

#===  FUNCTION  ================================================================
#         NAME: coloured
#  DESCRIPTION: Write a message with colour codes
#   PARAMETERS: 1 - colour name
#               2 - message
#      RETURNS: Nothing
#===============================================================================
coloured () {
    local colour="${1:-green}"
    local message="${2:-no message}"

    printf '%s%s%s\n' "${!colour}" "$message" "${reset}"
}

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

#
# Directories
#
BASEDIR="$HOME/HPR/Show_Submission"
RES="$BASEDIR/reservations"
SN="$BASEDIR/shownotes"

#
# Check arguments
#
if [[ $# -ne 2 ]]; then
    _usage 1
fi

#
# Check the arguments and standardise them
#
declare -a ARGS
re="^(hpr|HPR)?([0-9]{1,4})$"
for arg; do
    if [[ $arg =~ $re ]]; then
        printf -v n '%04d' "${BASH_REMATCH[2]}"
        ARGS+=("hpr$n")
    else
        coloured 'red' "Invalid show specification: $arg"
    fi
done

if [[ ${#ARGS[@]} -ne 2 ]]; then
    coloured 'red' "Unable to continue with invalid arguments"
    exit 1
fi

#
# Save the parsed show details
#
original_show="${ARGS[0]}"
new_show="${ARGS[1]}"
coloured 'blue' "Moving from $original_show to $new_show"

#-------------------------------------------------------------------------------
# Consistency checks
#-------------------------------------------------------------------------------
#
# We expect the original show to have been moved into the reservations/
# directory
#
if [[ ! -e "$RES/$original_show" ]]; then
    coloured 'red' "Did not find the moved original show as $RES/$original_show."

    #
    # Perhaps the original show hasn't been moved yet, otherwise there's an
    # error - the wrong show has been specified.
    #
    if [[ -e "$SN/$original_show" ]]; then
        coloured 'red' "Found the original show as $SN/$original_show."
        coloured 'yellow' "Double check that this show has been moved on the HPR server."
        coloured 'yellow' "If so, wait for 'scan_HPR' to move it locally."
        coloured 'yellow' "If not, the show numbers you used are wrong."
    else
        coloured 'red' "Did not find the original show as $SN/$original_show."
        coloured 'yellow' "It looks as if the show numbers you used are wrong."
    fi

    coloured 'red' "Unable to continue"
    exit
fi

#
# Is the destination free? It may have been filled with a dummy show by
# 'scrape_HPR' or there may have been a mistake in the arguments.
#
if [[ -e "$SN/$new_show" ]]; then
    coloured 'yellow' "Found a directory at $SN/$new_show"

    #
    # A dummy show directory will only contain 'shownotes.txt', 'hpr1234.out'
    # and 'hpr1234.html' to keep other scripts happy and a file called .dummy
    # to be detected by this script. So look for this signature, and if not
    # found halt with an error.
    #
    if [[ ! -e "$SN/$new_show/.dummy" ]]; then
        coloured 'red' "The directory seems to hold a live show!"
        coloured 'red' "Cannot continue; please check the situation"
        exit
    fi
fi

#
# Now we should have found the original show has been placed in the
# reservations/ directory and the new slot should have had a dummy show placed
# in it. We now need to move the reserved show into the destination slot after
# removing the dummy show. Once done we need to rename files and change the
# contents of some files.
#

exit

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