#!/bin/bash -
#===============================================================================
#
#         FILE: make_tsu_blank
#
#        USAGE: ./make_tsu_blank [-h] [-D] start count
#
#  DESCRIPTION: Make a template for generating a tag and summary update email.
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: Now obsolete but retained for reference purposes
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.8
#      CREATED: 2016-05-28 16:21:22
#     REVISION: 2021-06-23 13:03:31
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

SCRIPT=${0##*/}

VERSION="0.0.8"

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

#
# We need the SSH tunnel (The script to test this and to open it, open_tunnel,
# are in ~/bin. This needs to be set up if running this stuff somewhere else)
#
if ! tunnel_is_open; then
    echo "$SCRIPT: ${red}The SSH tunnel must be open to do this${reset}"
    exit 1
fi

#===  FUNCTION  ================================================================
#         NAME: _usage
#  DESCRIPTION: Report usage
#   PARAMETERS: None
#      RETURNS: Nothing
#===============================================================================
_usage () {
    cat >$STDOUT <<-endusage
Usage: ./${SCRIPT} [-h] [-d] [-D] start count

Version: $VERSION

Generates a file of tag and summary updates for shows in the given range which
can be edited and submitted to tags@hackerpublicradio.org in order to update
the relevant shows.

Options:
  -h                    Print this help
  -D                    Select debug mode (works the same; more output)

Arguments:
    start               starting show number
    count               number of shows (shouldn't exceed 20)

Examples
  ./${SCRIPT} -h
  ./${SCRIPT} -D 700 10

endusage
    exit
}

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

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

#
# Option defaults
#
DEBUG=0

#
# Process options
#
while getopts :hdD opt
do
    case "${opt}" in
        h) _usage;;
        D) DEBUG=1;;
        ?) echo "$SCRIPT: Invalid option; aborting"; exit 1;;
    esac
done
shift $((OPTIND - 1))

#
# Directories and files
#
BASEDIR="$HOME/HPR/Database"
TSU="$BASEDIR/tsu"

PREFIX="tag_summary_updates_"
GENERATOR="$BASEDIR/query2tt2"
LIVECFG="$BASEDIR/.hpr_livedb.cfg"
TEMPLATE="$BASEDIR/query2tt2_taglist.tpl"

#
# Sanity checks
#
[ -d "$BASEDIR" ] || { echo "Unable to find directory $BASEDIR"; exit 1; }
[ -d "$TSU" ] || { echo "Unable to find directory $TSU"; exit 1; }

for item in $GENERATOR $LIVECFG $TEMPLATE; do
    [ -e "$item" ] || {
        echo "Unable to find component: $item"
        exit 1
    }
done

#
# Maximum number of shows to scan. This is advisory since we might want to
# scan 40 and only get 3 which need work!
#
LIMIT=20

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

#
# Validate arguments and make the END variable
#
START="$1"
COUNT="$2"

RE='^[0-9]+$'
if ! [[ $START =~ $RE ]]; then
    echo "${red}Invalid starting value: $1${reset}"
    exit 1
fi
if ! [[ $COUNT =~ $RE ]]; then
    echo "${red}Invalid count value: $2${reset}"
    exit 1
fi

#
# Deal with leading zeroes if any by forcing such numbers to base 10
#
START=$((10#$START))
COUNT=$((10#$COUNT))
((END = START + COUNT - 1))

_DEBUG "Start: $START" "Count: $COUNT" "End: $END"

#
# Argument sanity checks
#
if [[ $COUNT -gt $LIMIT ]]; then
    echo "${yellow}Range: $START..$END ($COUNT)${reset}"
    echo "${yellow}You are asking for a count greater than 20.${reset}"
    echo "${red}Beware! This could be unmanageable!${reset}"
    if ! yes_no 'Are you sure you want this? %s ' 'N'; then
        echo "${red}Request ignored. Please try again.${reset}"
        exit
    fi
fi

#
# Generate the output file path
#
printf -v OUTFILE "%s/%s%04d-%04d.txt" "$TSU" "$PREFIX"  "$START" "$END"

_DEBUG "Output: $OUTFILE"

#
# Does the output file exist? If so, can we detect any work having been done
# to it?
#
overwrite=0
if [[ -e $OUTFILE ]]; then
    if [[ -s $OUTFILE ]]; then
        echo "${yellow}${OUTFILE/$HOME/\~} already exists.${reset}"
        if grep -E -q "^(summary|tags): ?\w+" "$OUTFILE"; then
            echo -n "${yellow}** Work has been done on this file"
            missing=$(grep -E -c "^(summary|tags): *$" "$OUTFILE")
            if ((missing)); then
                echo " (there are still tags/summaries to be added).${reset}"
            else
                echo ".${reset}"
            fi
        else
            echo "${yellow}This file has not had tags or summaries added.${reset}"
        fi

        if ! yes_no 'Are you sure you want to replace it? %s ' 'N'; then
            echo "${red}File not overwritten${reset}"
            exit
        else
            overwrite=1
        fi
    else
        #
        # This shouldn't happen. An empty file caused by a failed query or
        # because there's nothing to do should be cleared away immediately
        # rather than here where the file has been left hanging around.
        #
        echo "${yellow}${OUTFILE/$HOME/\~} exists but is empty. Deleting it.${reset}"
        rm -f "$OUTFILE"
    fi
fi

_DEBUG "Overwrite: $overwrite"

#
# If we're overwriting no collision check otherwise check check check!
#
if [[ $overwrite -eq 0 ]]; then
    #
    # Check for collisions.
    #
    # Look for individual files already created, taking the FROM and TO values
    # from their names. Look to see if the range START-END is in the range FROM-TO
    # or the other way round. Print all collisions. Any found mean the script
    # can't continue.
    #
    # Note that we have to force numbers to base 10 in case they have leading
    # zeroes (and will therefore be treated as octal).
    #
    collisions=0
    FILERE="${PREFIX}([0-9]{4})-([0-9]{4})\\.txt$"
    for f in "$TSU"/"${PREFIX}"*; do
        if [[ $f =~ $FILERE ]]; then
            FROM="${BASH_REMATCH[1]}"
            FROM=$((10#$FROM))
            TO="${BASH_REMATCH[2]}"
            TO=$((10#$TO))
            if [[ (( $START -ge $FROM && $START -le $TO ) ||\
                ( $END -ge $FROM && $END -le $TO )) || \
                (( $FROM -ge $START && $FROM -le $END ) ||\
                ( $TO -ge $START && $TO -le $END )) ]]; then
                printf \
                    '%sCollision: range %04d-%04d overlaps the range %04d-%04d (in '%s')%s\n' \
                    "${red}" "$START" "$END" "$FROM" "$TO" "${f##*/}" "${reset}"
                ((collisions++))
            fi
        fi
    done

    if [[ $collisions -gt 0 ]]; then
        echo "${red}Found $collisions collisions; aborting${reset}"
        exit 1
    fi
fi

#
# Define the SQL.
# 2021-06-20: Now we make a simpler query and rely on a script and template to
# format everything.
#
SQL=$(cat <<ENDSQL
SELECT
    id, summary, tags
FROM eps
WHERE id BETWEEN $START AND $END
AND (length(summary) = 0 OR length(tags) = 0)
ORDER BY id
ENDSQL
)

_DEBUG "----" "$SQL" "----"


#
# Run MySQL with the query.
# 2021-06-20: The script below does all we want using the predefined template
#
$GENERATOR -config="$LIVECFG" -template="$TEMPLATE" "$SQL" > "$OUTFILE"
RES=$?

#
# Die if the query failed, and clear up the empty output file if found
#
[ $RES -eq 0 ] || {
    echo "${red}Query failed; aborting${reset}"
    if [[ -e $OUTFILE && ! -s $OUTFILE ]]; then
        rm -f "$OUTFILE"
    fi
    exit 1
}

#
# An empty file could be "successfully" created. If so we delete it
#
if [[ -s $OUTFILE ]]; then
    #
    # Report the file created.
    #
    # 2021-06-20: The original sed call is not needed any more because the
    # script we ran made the file in the form we want.
    #
    echo "${green}Output is in ${OUTFILE/$HOME/\~}${reset}"
else
    rm -f "$OUTFILE"
    echo "${yellow}No episodes need work in that range${reset}"
fi

exit

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