#!/usr/bin/env bash
#===============================================================================
#
#         FILE: copy_shownotes
#
#        USAGE: ./copy_shownotes
#
#  DESCRIPTION: Copies the shownotes (and related files) downloaded from the
#               HPR server. This happens after 'sync_hpr' has been run to
#               collect updates from the ~hpr/upload/ directory on the server
#               and store the result in the local upload/ directory.
#               2022-12-17: Converted to shownotes.json.
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.10
#      CREATED: 2015-09-16 21:51:15
#     REVISION: 2024-06-18 20:42:17
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

SCRIPT=${0##*/}

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

#
# Colour codes
#
define_colours

#
# Directories
#
BASEDIR="$HOME/HPR/Show_Submission"
LOGS="$BASEDIR/logs"
UPLOAD="$BASEDIR/upload"
CACHE="$BASEDIR/shownotes"

#
# Filenames
#
LOGFILE="$LOGS/${SCRIPT}.log"
#SHOWNOTES='shownotes.txt'
SHOWNOTES='shownotes.json'
ORIGIN='.origin'
STATUS='.status'
DUMMY='.dummy'

#
# Loop through everything in the $UPLOAD directory using a regular expression
# to find sub-directories. These look like:
# 1445633350_1906_2015-11-23_f348faf9125c129c1ebe0dd0edd721a0562a9d464bbbf
#
# The regex used in find takes into account that it needs to match the full
# path.
#
count=0
target=".*/[0-9]+_[0-9]{4}_[0-9]{4}-[0-9]{2}-[0-9]{2}_.+$"
while read -r d
do
    #
    # Parse out the show number
    #
    show="$(echo "$d" | cut -f2 -d_)"
    from="$UPLOAD/$d/"
    dir="$CACHE/hpr${show}"
    to="$dir/$SHOWNOTES"
    origin="$dir/$ORIGIN"
    status="$dir/$STATUS"
    dummy="$dir/$DUMMY"

    #
    # Make the receiving directory if it doesn't exist
    #
    if [[ ! -e $dir ]]; then
        mkdir "$dir"
    fi

    #
    # Copy files if there are no shownotes or the file exists and is empty.
    #
    # 2022-12-17: We're soon not using shownotes.txt any more. The data is in
    # shownotes.json instead. Also, dummy shows have a '.dummy' file rather
    # than empty notes.
    #
    # if [[ ! -e $to || ! -s $to ]]; then
    if [[ ! -e $to || -e $dummy ]]; then
        rsync -vaP "$from" "${dir}/"
        echo "$d" > "$origin"
        echo "copied" > "$status"
        ((count++))
        printf '%(%F %T)T %s\n' -1 "$dir" >> "$LOGFILE"
        echo "${green}Copied notes for show $show${reset}"
    fi
done < <(find "$UPLOAD" -maxdepth 1 -regextype posix-egrep -regex "$target" -type d -printf '%f\n')

if [[ $count -eq 0 ]]; then
    echo "${yellow}Nothing to do${reset}"
else
    echo "${green}Notes copied: $count${reset}"
fi

exit

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