#!/bin/bash -
#===============================================================================
#
#         FILE: do_repair
#
#        USAGE: ./do_repair <epno>
#
#  DESCRIPTION: Run vim on the raw shownotes.txt file for a show and offer to
#               upload it to the server
#
#      OPTIONS: ---
# REQUIREMENTS: ---
#         BUGS: ---
#        NOTES: ---
#       AUTHOR: Dave Morriss (djm), Dave.Morriss@gmail.com
#      VERSION: 0.0.1
#      CREATED: 2019-04-28 11:06:45
#     REVISION: 2019-04-28 11:26:09
#
#===============================================================================

set -o nounset                              # Treat unset variables as an error

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

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

BASENAME="/home/cendjm/HPR/Show_Submission"
SHOWDIR="$BASENAME/shownotes/hpr${1}"
SHOWNOTES="$SHOWDIR/shownotes.txt"
SNORIG="$SHOWDIR/shownotes.txt.orig"
ORIGIN="$SHOWDIR/.origin"

#
# Backup the original file
#
if [[ ! -e $SNORIG ]]; then
    cp "$SHOWNOTES" "$SNORIG"
fi

upload_dir=$(cat "$ORIGIN")
MD5_1=$(md5sum "$SHOWNOTES")

#
# Edit the data from the form
#
vim "$SHOWNOTES"
RES=$?

if [[ $RES -eq 0 ]]; then
    echo "Edited $SHOWNOTES ok"

    MD5_2=$(md5sum "$SHOWNOTES")
    if [[ $MD5_1 = "$MD5_2" ]]; then
        echo "The file was not changed"
        exit
    else
        echo "Copying $SHOWNOTES to upload/$upload_dir/shownotes.txt on the HPR server"
        echo "Copying $SNORIG to upload/$upload_dir/shownotes.txt.orig on the HPR server"
        scp -P 22074 "$SHOWNOTES" "$SNORIG" "hpr@hackerpublicradio.org:upload/$upload_dir/"
    fi
else
    echo "Oops! Something went wrong!"
fi

exit

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