#!/bin/bash -
#===============================================================================
#
#         FILE: do_parse
#
#        USAGE: ./do_parse <epno>
#
#  DESCRIPTION: Run 'parse_JSON' on a given show
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.15
#      CREATED: 2016-05-14 14:21:34
#     REVISION: 2022-10-01 21:49:32
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

SCRIPT=${0##*/}
#DIR=${0%/*}
VERSION="0.0.15"

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

#
# Process options
#
while getopts :fh opt
do
    case "${opt}" in
        h) echo "Usage: $SCRIPT [-h] [-f] shownumber"; exit 0;;
        f) FORCE=1;;
        ?) echo "$SCRIPT: Invalid option; aborting"; exit 1;;
    esac
done
shift $((OPTIND - 1))

FORCE="${FORCE:-0}"

if [[ $# -ne 1 ]]; then
    echo "[${SCRIPT} ${VERSION}] Usage: ${red}$SCRIPT shownumber${reset}"
    exit
fi

show="$1"

BASENAME="$HOME/HPR/Show_Submission"
SHOWDIR="$BASENAME/shownotes/hpr${show}"
LOGDIR="$BASENAME/logs"
#PARSER="$BASENAME/parse_shownotes"
PARSER="$BASENAME/parse_JSON"
PARSELOG="$LOGDIR/${PARSER##*/}.log"
#FROM="$BASENAME/shownotes/hpr${show}/hpr${show}_shownotes.txt"
#FROM="$BASENAME/shownotes/hpr${show}/shownotes.txt"
FROM="$SHOWDIR/shownotes.json"
TO="$SHOWDIR/hpr${show}.out"
TOTPL="$SHOWDIR/hpr%d.out"
SLINK="$SHOWDIR/hpr${show}.html"
FMT="$SHOWDIR/.format"
REL="$SHOWDIR/.release"
PICTURES="$SHOWDIR/.pictures"
ASSETS="$SHOWDIR/.assets"
ZIP="$SHOWDIR/.backup.zip"
#JSONTPL="$SHOWDIR/hpr%d.json"
SHOWLOG="$SHOWDIR/error.log"

#
# Sanity checks
#
[ -e "$PARSER" ] || {
    echo "$SCRIPT: ${red}$PARSER not found${reset}"
    exit 1
}

if [[ ! -e $FROM ]]; then
    echo "$SCRIPT: ${red}File not found: $FROM${reset}"
    exit 1
fi

#
# Allow overwriting of the output file if the -f option is given. Also, if
# there's a link delete it so parse_shownotes can re-create it (we use '-ef'
# to check that the files have the same inode).
#
if [[ $FORCE -eq 0 ]]; then
    if [[ -e $TO ]]; then
        echo "$SCRIPT: ${red}The output file $TO already exists${reset}"
        exit 1
    fi
else
    if [[ $TO -ef $SLINK ]]; then
        rm -f "$SLINK"
    fi
fi

if [[ ! -s $FROM ]]; then
    echo "$SCRIPT: ${red}Input file $FROM is empty${reset}"
    exit 1
fi

#
# Run the parser, don't validate, and use the show number argument. Read the
# input file 'shownotes.txt' and generate an output file derived from the
# template '$TOTPL'. Write a JSON version of the output and write the declared
# format to a file for future reference.
#
# Updated 2021-02-23: Now we parse the incoming JSON file .shownotes.json'.
# There's no validation and we don't write JSON output.
#
# $PARSER -novalid -ep "${show}" -in "$FROM" -show "$TOTPL" \
#     -json "$JSONTPL" -format="$FMT"
#
$PARSER -ep "${show}" -in "$FROM" -show "$TOTPL" \
    -format="$FMT" -release="$REL" -pictures="$PICTURES" -assets="$ASSETS" \
    -zip="$ZIP"
RES=$?

if [[ $RES -ne 0 ]]; then
    echo "$SCRIPT: ${red}Oops! Something went wrong!${reset}"
    exit 1
fi

#
# Make a reference copy of the output file
#
cp "$TO" "${TO%.out}.orig"

#
# Grep the common log file for error reports relating to this show
#
grep -E "^$(date +%Y/%m/%d).+ \[ERROR\] $show" "$PARSELOG" >> "$SHOWLOG"

exit

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