#!/bin/bash
#
# Variable and function definitions
#
AUTHOR='S.Bardeau <gildas@iram.fr>'
VERSION=`echo '$Revision: 1.6 $' | cut -d' ' -f2`
PROJECT='GILDAS <http://www.iram.fr/IRAMFR/GILDAS>'
PROGNAME=`basename $0`
#
# Administration
MANAGER=pety          # Account used for non-anonymous commands
MODULE=gildas
WORKDIR=$HOME/gildas-cvs
LOGFILE=$WORKDIR/logs/update-pdb-calibrators.txt
#
# Location of the file
PH_MACHINE=pipeline-pdb.iram.fr
PH_ORIGIN=/users/PdB/oper/PdbSettings/Base/phase-pdb.sou
PH_TARGET=$MODULE/packages/astro/etc/phase-pdb.sou
FL_MACHINE=astro2.iram.fr
FL_ORIGIN=/noemaprojects/PdB/bure/clic-pro/aflu-pipeline.dat
FL_TARGET=$MODULE/packages/clic/etc/aflu-pipeline.dat
#
# Mail reports
RECIPIENTS_FAILURE="bardeau"
RECIPIENTS_SUCCESS="bardeau"
MAILHEADER="[cvs-update-pdb-calibrators]"
#
###########################################################################
#
# Ensure a stable $gagadmdir to start the process
#
export gagadmdir=$WORKDIR/gildas-admin
#
###########################################################################
#
# Define local functions
#
function showversion() {
    echo "$PROGNAME version $VERSION, by $AUTHOR"
    echo "Project: $PROJECT"
}
#
function message() {
    echo "$PROGNAME: $1"
}
#
function usage() {
    cat <<EOF 1>&2

Script used to automatically update the PdB calibrators fluxes on CVS
HEAD:
$PH_MACHINE:$PH_ORIGIN -> $PH_TARGET
$FL_MACHINE:$FL_ORIGIN -> $FL_TARGET

usage: $PROGNAME

options:
  -h   Show this help page
  -v   Show version information

EOF
}
#
function error_exit() {
    #
    # Write message to STDOUT
    message ""
    message "$PROGNAME error: $1"
    message ""
    #
    # Write message to logfile
    echo                       >> $LOGFILE
    echo "$PROGNAME error: $1" >> $LOGFILE
    echo                       >> $LOGFILE
    #
    # Mail report
    RECIPIENTS=$RECIPIENTS_FAILURE
    report "Failure"
    #
    clean
    #
    exit 1
    #
}
#
function report() {
    #
    echo >> $LOGFILE
    date >> $LOGFILE
    #
    SUBJECT="$MAILHEADER $1"
    #
    message "Sending report"
    message "File =       $LOGFILE"
    message "Subject =    $SUBJECT"
    #
    if [ -n "$RECIPIENTS" ]; then
	message "Recipients = $RECIPIENTS"
	cat $LOGFILE | $gagadmdir/sendmail.pl -s "$SUBJECT" $RECIPIENTS
    else
	message "Recipients = <none>"
    fi
    #
}
#
function clean() {
    cd $HOME
    rm -rf $tmpdir
}
#
########################################################################
#
# Option parsing
#
temp=`getopt "hv" "$@"`
if [ $? -ne 0 ]; then usage; exit 1; fi
eval set -- "$temp"
unset temp
while [ $1 != -- ]; do
    case "$1" in
    -v) showversion; exit 0 ;;
    -h) usage; exit 0 ;;
    *) usage; exit 1 ;;
    esac
    shift # Next flag
done
shift # Skip double dash
set abc; shift # This line to avoid remanence effect in a portable way
#
########################################################################
#
# Start work by tracking time.
#
date > $LOGFILE
echo >> $LOGFILE
#
# Need repository write access:
export CVSROOT=:pserver:$MANAGER@cvs.iram.fr:/CVS/GILDAS
#
# Get 'phase-pdb.sou' file
tmpdir=/tmp/$MODULE-$$
mkdir -p $tmpdir || error_exit "Could not mkdir -p $tmpdir"
cd $tmpdir
message "Checking out $PH_TARGET and $FL_TARGET"
cvs checkout $PH_TARGET $FL_TARGET
#
# Copy new files
rm -f $PH_TARGET $FL_TARGET
scp $PH_MACHINE:$PH_ORIGIN $PH_TARGET || error_exit "Failed scp $PH_MACHINE:$PH_ORIGIN $PH_TARGET"
scp $FL_MACHINE:$FL_ORIGIN $FL_TARGET || error_exit "Failed scp $FL_MACHINE:$FL_ORIGIN $FL_TARGET"
#
# Update 'phase-pdb.sou' on CVS HEAD.
cvs commit -m "Automatically updated" $PH_TARGET $FL_TARGET ||  \
    error_exit "Committing '$PH_TARGET' or '$FL_TARGET' failed"
message "Updated '$PH_TARGET' and '$FL_TARGET'"
echo    "Updated '$PH_TARGET' and '$FL_TARGET'" >> $LOGFILE
#
# Cleaning
clean
#
# Report
RECIPIENTS=$RECIPIENTS_SUCCESS
report "Success"
#
###########################################################################
