#!/bin/bash

#########################################
# Name: rsync plot
#
# Summary: sync archive, generate PDF 
#          plots, convert to HTML
#
# Description: 
# Uses rsync, qsee, pdftohtml.
#
# Invoke like this
# rsync-plot -rqw
#
# Author: kheadley
#
# Copyright MBARI 2014
#
#########################################

#########################################
# Script configuration defaults
# casual users should not need to change
# anything below this section
#########################################

PLOT_TIME=72

# xfgw3->fangtooth
RSKEY="ssh -i /home/ops/rskeys/fangtooth-rsync-key"
SRC="ops@xfgw3.shore.mbari.org:/mnt/xfoce/build/xfoce-0.55/arch"
DEST="/home/ops/xfgw3-mirror/"

# qfoce->fangtooth
#RSKEY="ssh -i /home/ops/rskeys/fangtooth-rsync-key"
#SRC="ops@kelpbed.swfoce.mbari.org:/home/ops/xfoce/xfoce-0.53/arch"
#DEST="/home/ops/kelpbed-mirror"

# xfgw3->brightwork
#RSKEY="ssh -i /home/headley/rskeys/brightwork-rsync-key"
#SRC="ops@xfgw3.shore.mbari.org:/mnt/xfoce/build/xfoce-0.55/arch"
#DEST="/home/headley/host/xfgw3-qfoce-09jul14"

if [ -z "${RSKEY}" ] || [ -z "${SRC}" ] || [ -z "${DEST}" ]
then
	echo "One or more of RSKEY, SRC, DEST not defined"
	exit -1
fi

UPDATE_TOP="$DEST/last72"
PDF_DIR="$UPDATE_TOP/plot-output/pdf"
CSV_DIR="$UPDATE_TOP/plot-data"
PUB_DIR="$UPDATE_TOP/pub"
PUB_DATA="$PUB_DIR/data"
PUB_METRICS="$PUB_DIR/metrics"

CONF_DIR="$DEST/conf/qs-qfoce.conf"

PUB_DATA_FILES="$PDF_DIR/dp*pdf $CSV_DIR/*csv"
PUB_METRICS_FILES="$PDF_DIR/mp*pdf $CSV_DIR/*txt"

#QS_HTML="$UPDATE_TOP/html/xfgw"

#################################
# Script variable initialization
#################################
VERBOSE="FALSE"

unset DO_RSYNC
unset DO_QSEE
unset DO_HTML

#################################
# name: printUsage
# description: print use message
# args: none
#################################
printUsage(){
    echo
    echo "usage: `basename $0`"
    echo "-r      : do rsync            [$DO_RSYNC]"
    echo "-q      : do quick plots      [$DO_QSEE]"
    echo "-w      : do html             [$DO_HTML]"
    echo "-t <n>  : time (last n hours) [$PLOT_TIME]"
    echo "-V      : verbose output      [$VERBOSE]"
    echo "-h      : print use message"
    echo ""
    echo
}

########################################
# name: vout
# description: print verbose message to stderr
# args:
#     msg: message
########################################
vout(){
    if [ "$VERBOSE" == "TRUE" ]
    then
	echo "$1" >&2
    fi
}

########################################
# name: processCmdLine
# description: do command line processsing
# args:
#     args:       positional paramters
#     returnCode: none
########################################
processCmdLine(){
	OPTIND=1
	vout "`basename $0` all args[$*]"

	while getopts qrt:whv Option
	do
		vout "processing $Option[$OPTARG]"
		case $Option in
			r ) DO_RSYNC="Y"
			;;
			q ) DO_QSEE="Y"
				rsync_ok=0
			;;
			t ) PLOT_TIME=$OPTARG
			;;
			w ) DO_HTML="Y"
				rsync_ok=0
				qsee_ok=0
			;;
			v ) VERBOSE="TRUE"
			;;
			h)printUsage
				exit 0
			;;
			*) exit 0 # getopts outputs error message
			;;
		esac
	done
}

##########################
# Script main entry point
##########################

# use simple arg processing
processCmdLine $*

# sync archive
if [ "${DO_RSYNC}" ]
then
	rsync -aivz -e "${RSKEY}" $SRC $DEST
	rsync_ok="$?"
fi

# generate PDR plots
if [ "${DO_QSEE}" ] && [ "${rsync_ok}" -eq 0 ]
then
	cd $UPDATE_TOP
	qsee -l $PLOT_TIME < $CONF_DIR
	qsee_ok="$?"
fi



# generate HTML
if [ "${DO_HTML}" ] && [ "${qsee_ok}" -eq 0 ]
then
	if [ ! -d "${PUB_DIR}" ]
	then
		mkdir ${PUB_DIR}
	fi
	
	if [ ! -d "${PUB_DATA}" ]
	then
		mkdir ${PUB_DATA}
	fi
	
	if [ ! -d "${PUB_METRICS}" ]
	then
		mkdir ${PUB_METRICS}
	fi
		
	PUB_DATA_SET=`ls $PUB_DATA_FILES`
	PUB_METRICS_SET=`ls $PUB_METRICS_FILES`
	
	for FILE in "$PUB_DATA_SET"
	do 
		cp ${FILE} ${PUB_DATA}
	done
	
	for FILE in "$PUB_METRICS_SET"
	do 
		cp ${FILE} ${PUB_METRICS}
	done
	html_ok="$?"
fi
