#!/bin/bash -
#===============================================================================
#
#         FILE: do_upload
#
#        USAGE: ./do_upload <epno> [files...]
#
#  DESCRIPTION: Uploads the processed HTML back to the HPR server. Also
#               capable of uploading supplementary files such as a missed
#               audio file, pictures, etc.
#
#               There are two main types of uploads:
#               1) Extracted and edited show notes to go back to the server as
#                  HTML. Possiblly altered 'shotnotes.txt' as a means of
#                  fixing errors in the title, summary or tags.
#               2) Assets collected by 'parse_JSON' and possibly changed by
#                  'do_pictures' if there are pictures provided and referenced
#                  in the notes. Any archives will have had files extracted so
#                  that there is much less to do on the server.
#
#               Assets are sent to a directory under '~hpr/www/eps/' named
#               'hprNNNN'. This is created here and files copied with 'rsync'.
#               There is a local directory in the show directory which is
#               called 'uploads'. Unless the host has provided archives to be
#               downloaded with their show we will not send such files to the
#               server. We ask that such files be enclosed in an archive when
#               sent - so we never extract the archives we have extracted from
#               the sent archives!
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: 2021-03-15: Added code to place the "assets" in the final
#               directory on the server. This will be 'www/eps/hprNNNN/'.
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.1.2
#      CREATED: 2017-03-06 19:11:51
#     REVISION: 2023-06-14 23:12:04
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

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

VERSION="0.1.2"

STDOUT="/dev/fd/2"

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

#
# Colour codes
#
define_colours

# {{{ --- Functions: usage, dryrun ---

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

Version: $VERSION

Uploads the processed HTML back to the HPR server. Also
capable of uploading supplementary files such as a missed
audio file.

Options:
  -h                    Print this help
  -a                    Send any assets to the show sub-directory on the HPR
                        server
  -d                    Select dry run mode
  -D                    Select debug mode (works the same; more output)

Arguments:
    shownumber
    list of files to be uploaded in addition

Examples
  ./${SCRIPT} -h
  ./${SCRIPT} -d 3099
  ./${SCRIPT} -d 3123 otherfile1.txt otherfile2.dat

endusage
    exit
}

#===  FUNCTION  ================================================================
#         NAME: _dryrun
#  DESCRIPTION: Output a dry run message
#   PARAMETERS: List of messages
#      RETURNS: Nothing
#===============================================================================
_dryrun () {
    for msg in "$@"; do
        printf 'Dry run: %s\n' "$msg"
    done
}

#===  FUNCTION  ================================================================
#         NAME: trimpath
#  DESCRIPTION: Trim a file path to its last few components for display
#   PARAMETERS: $1 - path to trim
#               $2 - number of elements to retain
#      RETURNS: Trimmed path
#===============================================================================
# trimpath () {
#     local path=${1:?Usage: mpath path elements}
#     local elements=${2:?Usage: mpath path elements}
#     local -a arr1 arr2
# 
#     # Split the path
# #   IFS='/' arr1=($path)
#     IFS='/' mapfile -d'/' -t arr1 <<<"$path"
# 
#     if [[ ${#arr1[@]} -gt $elements ]]; then
#         # Put the last elements in another array
# #       arr2=(${arr1[@]: -$elements})
#         mapfile -t arr2 < <(printf '%s\n' "${arr1[@]: -$elements}")
# 
#         # return the second array interleaved with '/'
#         echo "${arr2[*]/#/}"
#     else
#         echo "$path"
#     fi
# }

# }}}

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

#
# Option defaults
#
ASSETUPLOAD=0
DEBUG=0
DRYRUN=0

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

#
# Check there's at least one argument after removing any options. Abort if not
#
if [[ $# -eq 0 ]]; then
    echo "Missing argument(s)"
    _usage
fi

#
# Paths to files
#
# 2021-02-12 Not using the .format file any more
BASEDIR="$HOME/HPR/Show_Submission"
LOCAL_UPLOAD="$BASEDIR/upload"
SHOWDIR="$BASEDIR/shownotes/hpr${1}"
FROM="$SHOWDIR/hpr${1}.html"
# FORMAT="$SHOWDIR/.format"
ORIGIN="$SHOWDIR/.origin"
UPLOADED="$SHOWDIR/.uploaded"
STATUSFILE="$SHOWDIR/.status"

# If there are HTML errors
REPORT="$SHOWDIR/errors.txt"

# For differences
# 2021-02-12 $RE removed since we find differences for all formats
RAWNOTES="$SHOWDIR/hpr${1}.orig"
PROCESSED="$SHOWDIR/hpr${1}.out"
# RE='plain_text|markdown_standard|Markdown_GitHub|Markdown_Pandoc|restructured_text|txt2tags'
DIFFFILE="$SHOWDIR/hpr${1}.diff"

# PORT=22074
PORT=22

#
# Asset locations for upload
#
# If there are supplementary files
# SUPPDIR="$SHOWDIR/hpr${1}"
TARFILE="$SHOWDIR/hpr${1}.tgz"
PICLIST="$SHOWDIR/.pictures"
ASSETLIST="$SHOWDIR/.assets"

# Where any "assets" (pictures, scripts, etc) come from and will go on the server
LOCAL_ASSETDIR="$SHOWDIR/uploads"
REMOTE_ASSETDIR="www/eps/hpr${1}"
CMDTPL='ssh hpr@hackerpublicradio.org -p %d %s'

# Use to build the rsync command, assuming the tunnel is open:
# rsync -a -e 'ssh -p 22' $SHOWDIR/hpr3656/ hpr@hpr:www/eps/hpr3656/
#
RSYNCTPL="rsync -a -e 'ssh -p %d' %s hpr@hpr:www/eps/%s"

# Did the following by hand for uploading
# ssh -p $PORT hpr@hpr 'mkdir ~/www/eps/hpr3685'
# scp -P $PORT shownotes/hpr3685/Budget_sample_2022-9-9.ods hpr@hpr:www/eps/hpr3685/

EXTRA=0

#
# Is there a marker to indicate the show has already been uploaded? This
# isn't an error, but a warning is in order.
#
if [[ -e $UPLOADED ]]; then
    echo "$SCRIPT: ${yellow}Warning - the notes have already been uploaded${reset}"
fi

#
# Check we actually have a file to upload
#
if [[ ! -e $FROM ]]; then
    echo "$SCRIPT: ${red}File not found: ${FROM}${reset}"
    exit 1
fi

#
# If there's an error report then add it to the list of supplementary files
# (which may be none)
#
if [[ -e $REPORT ]]; then
    _DEBUG "Error report being added"
    set -- "$@" "${REPORT##*/}"
fi

#
# Deal with the raw notes extracted by 'parse_shownotes' and saved in
# hprXXXX.orig and the edited notes in hprXXXX.out
#
# 2021-02-12 Now using the same code for all formats
#
# if [[ -e $RAWNOTES && -e $PROCESSED ]]; then
#     FMT="$(cat "$FORMAT")"
#     if [[ $FMT =~ $RE ]]; then
#         diff "$RAWNOTES" "$PROCESSED" > "$DIFFFILE"
#         if [[ -s $DIFFFILE ]]; then
#             set -- "$@" "${DIFFFILE##*/}"
#         else
#             echo "$SCRIPT: ${red}No differences found${reset}"
#         fi
#     fi
# fi

if [[ -e $RAWNOTES && -e $PROCESSED ]]; then
    _DEBUG "Differences being determined:" "$RAWNOTES vs $PROCESSED"
    diff "$RAWNOTES" "$PROCESSED" > "$DIFFFILE"
    if [[ -s $DIFFFILE ]]; then
        _DEBUG "Differences found"
        set -- "$@" "${DIFFFILE##*/}"
    else
        _DEBUG "Differences file is empty"
        echo "$SCRIPT: ${red}No differences found${reset}"
    fi
fi

#
# If there's a picture list and a tarfile has been made add the latter to the
# upload list
# TODO: Not needed in next iteration
#
if [[ -e $PICLIST && -e $TARFILE ]]; then
    _DEBUG "Tar file being added"
    set -- "$@" "${TARFILE##*/}"
fi

#
# Check any supplementary files in the argument list (added by the user or
# this script)
#
if [[ $# -gt 1 ]]; then
    EXTRA=1
    shift               # Delete argument 1, the show number
    for arg; do
        if [[ ! -e "$SHOWDIR/$arg" ]]; then
            echo "$SCRIPT: ${red}File missing: $SHOWDIR/$arg${reset}"
            echo "Can't continue"
            exit 1
        fi
    done
    echo "$SCRIPT: ${blue}Number of supplementary files to upload: $#${reset}"
fi

#
# Check we have a record of where the files are to go
#
if [[ ! -e $ORIGIN ]]; then
    echo "$SCRIPT: ${red}Unable to find the .origin file${reset}"
    echo "(This holds the directory on the server where the files are to be sent)"
    exit
fi

#
# Check there's a local copy of the upload directory. If not, then the rsync
# run has deleted it because the directory on the server has been deleted.
# This implies the show has been processed on the server.
#
upload_dir="$(cat "$ORIGIN")"
_DEBUG "upload_dir = $upload_dir"
if [[ ! -e "$LOCAL_UPLOAD/$upload_dir" ]]; then
    echo "$SCRIPT: ${red}The upload directory seems to have been deleted${reset}"
    echo "(This happens after the show is processed on the server)"
    exit
fi

#
# Upload the processed show notes
#
if [[ $DRYRUN -eq 0 ]]; then
    echo "Copying $FROM to upload/$upload_dir/shownotes.html on the HPR server"
    scp -P $PORT "$FROM" "hpr@hackerpublicradio.org:upload/$upload_dir/shownotes.html"
    RES=$?

    if [[ $RES -eq 0 ]]; then
        echo "$SCRIPT: ${green}Uploaded successfully${reset}"
        touch "$UPLOADED"
    else
        echo "$SCRIPT: ${red}Oops! Something went wrong!${reset}"
        echo "$SCRIPT: Aborting now"
        exit 1
    fi
else
    _dryrun "would have copied $FROM to the server"
fi

#
# Handle supplementary files if there are any. The remaining arguments are
# these file names without any path information. Variable 'EXTRA' is
# true/false if there are/are not extra arguments. If there are we have
# already deleted the first argument and we've checked the existence of the
# file(s).
#
if [[ $EXTRA -eq 1 ]]; then
    _DEBUG "Uploading supplementary files"
    for arg; do
        FROM="$SHOWDIR/$arg"
        if [[ $DRYRUN -eq 0 ]]; then
            echo "Copying $FROM to upload/$upload_dir/$arg on the HPR server"
            scp -P $PORT "$FROM" "hpr@hackerpublicradio.org:upload/$upload_dir/$arg"
            RES=$?

            if [[ $RES -eq 0 ]]; then
                echo "$SCRIPT: ${green}Uploaded ${arg} successfully${reset}"
            else
                echo "$SCRIPT: ${red}Oops! Something went wrong with ${arg}!${reset}"
                echo "$SCRIPT: Aborting now"
                exit 1
            fi
        else
            _dryrun "would have copied $FROM to the server"
        fi
    done
fi

#
# Update the status file
#
if [[ $DRYRUN -eq 0 ]]; then
    echo "uploaded" >> "$STATUSFILE" || \
        { echo "Failed to update $STATUSFILE"; exit 1; }
fi

exit

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