#!/bin/bash
#
# Script:       saveIt.ksh
#
# Description:  Backup files to directory under 'sourceCodeControl/SAFEscripts/'
#
#               The master copy of this script is in ~/private/tools/saveIt.ksh
#               and all local copies are linked from it.
#
#               This will backup Railwatch, Scotland and Wales website scripts
#               under a directory for the website (e.g. www.railwatch.org.uk),
#               otherwise it will be backed up under the main directory (and its
#               sub-directories).
#
# Author:       Jerry Alderson, Billing Specialists Ltd.
#
# Date Written: 13th July 2019
#
# Last changed: 23rd March 2024
#
# Usage:        <filename>
#               -h = show history <filename>
#               -d = display directory 
#

#--- Functions ---#

saveFile() {
    NAME="$1"
    #
    SAFENAME="SAFECOPY_${NAME}_$(date +"%Y%m%d_%H%M%S")"
    SAFEPATH="${SAFEDIRPATH}/${SAFENAME}"
    #
    if cp ${NAME} ${SAFEPATH}
    then
        echo "Saved to ${SAFEPATH}"
    else
        echo "Failed to save ${NAME} to ${SAFEPATH}"
        exit 1
    fi
}

showLatestFileBackup() {
    HISTFILENAME="$(ls ${SAFEDIRPATH}/SAFECOPY_${NAME}_* 2>/dev/null | tail -1)"
    #
    if [ "${HISTFILENAME}" != "" ]
    then
        ls -last ${SAFEDIRPATH}/SAFECOPY_${NAME}_* | head -1
        CMD="diff ${NAME} ${HISTFILENAME}"
        if ${CMD} >/dev/null
        then
            echo "Contains current file (no differences found from ${CMD}:"
        else
            echo "Differences found from:"
            wc ${NAME} ${HISTFILENAME} | grep -v ' total$'
            echo ""
            echo "    ${CMD}"
            echo ""
        fi
    else
        echo "No saved copy of '${NAME}'"
    fi
}

#--- Main Program ---#

# Security check to make sure that this is not run from a browser

if [ "${SHELL}" != "/bin/bash" ]
then
    echo "This cannot be run from a browser!"
    exit 1
fi

echo "Executing: $(readlink $0)"

# Identify the source code control 'safe' area

SAFEAREA="${HOME}/private/sourceCodeControl/SAFEscripts"

THISFULLDIRPATH="$(pwd)"

case "${THISFULLDIRPATH}" in
    */public_html/railfuturescotland.org.uk)
        GROUPNAME="$(basename "${THISFULLDIRPATH}")/public_html"
        ;;
    */public_html/railfuturewales.org.uk)
        GROUPNAME="$(basename "${THISFULLDIRPATH}")/public_html"
        ;;
    */public_html/railwatch.org.uk)
        GROUPNAME="$(basename "${THISFULLDIRPATH}")/public_html"
        ;;
    *tiki-like*)
        GROUPNAME="$(echo "${THISFULLDIRPATH}" | sed 's?^.*tiki-like/??')"
        ;;
    *)
        GROUPNAME="$(basename "${THISFULLDIRPATH}")"
        ;;
esac

# Either save the file or show how to get the list of differences

SAFEDIRPATH="${SAFEAREA}/${GROUPNAME}"

if [ -d "${SAFEDIRPATH}" ]
then
    if [ "${1}" = "-d" ]
    then
        echo "Save directory is: ${SAFEDIRPATH}"
    elif [ "${1}" = "-h" ]
    then
        shift
        #
        if [ "${1}" != "" ]
        then
            NAME="$1"
            #
            showLatestFileBackup "{NAME}"
        else
            ls -last ${SAFEDIRPATH}/SAFECOPY_*
        fi
    elif [ "${1}" != "" ]
    then
        saveFile "$1"
    else
        echo "ERROR: No file specified to save"
        exit 1
    fi
else
    echo "ERROR: No directory called '${SAFEDIRPATH}'"
    exit 1
fi

exit 0

# End of script.
