#!/bin/bash
#
# Variable and function definitions
#
AUTHOR='S.Bardeau <gildas@iram.fr>'
VERSION=`echo '$Revision: 1.2 $' | 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
LOGFILE=$WORKDIR/logs/update-pdb-calibrators.txt
#
# Location of the file
ORIGIN=~pdbsog/www/calibration/phase-pdb.sou
TARGET=$MODULE/packages/astro/etc/phase-pdb.sou
#
# Mail reports
RECIPIENTS_FAILURE="pety bardeau"
RECIPIENTS_SUCCESS="pety bardeau reynier sog"
MAILHEADER="[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 ($TARGET). File is copied from
$ORIGIN.

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 $TARGET"
cvs checkout $TARGET
#
# Copy new file
rm -f $TARGET
if [ -e $ORIGIN ]; then
    message "Copying: cp -f $ORIGIN $TARGET"
    cp -f $ORIGIN $TARGET
    PHASE_PDB_VERS=`head -1 $ORIGIN`
else
    error_exit "No such file $ORIGIN"
fi
#
# Update 'phase-pdb.sou' on CVS HEAD.
cvs commit -m "Automatically updated ($PHASE_PDB_VERS)" $TARGET ||  \
    error_exit "Committing '$TARGET' failed"
message "Updated '$TARGET' ($PHASE_PDB_VERS)"
echo    "Updated '$TARGET' ($PHASE_PDB_VERS)" >> $LOGFILE
#
# Cleaning
clean
#
# Report
RECIPIENTS=$RECIPIENTS_SUCCESS
report "Success"
#
###########################################################################
