#!/usr/bin/env bash
#
#  Start the daily BOSS spectro pipeline.  This script is passed onto rundaily by 
#  uurundaily_cron using  "uurundaily_script $MJD" where $MJD is the mjd to process.
#
#  Written by Gary Kushner (LBL).  Jan 2010.

function usage
{
    local execName=$(basename $0)
    ( 
	echo "UURUNDAILY_SCRIPT: usage $execName \$MJD [noupdate]"
    ) >&2
    exit 1
}

function screamAndDie
{
    echo "UURUNDAILY_SCRIPT:  Failed $*" | tee -a $LOGDIR/$MJD.warning

    echo "UURUNDAILY_SCRIPT: Failing"

	#  Make it easy to see the failure
	echo "UURUNDAILY_SCRIPT: Failed!" >>$LOGDIR/$MJD.failed
	
	### This hack is too ugly, need to change it ###
	echo "UURUNDAILY_SCRIPT: Mailing Error Report"
	mail -s "$MJD failed" joelbrownstein@physics.utah.edu < $LOGDIR/$MJD.warning
	
    exit 1
}

function printAndRun
{
    echo "UURUNDAILY_SCRIPT: Checkpoint at $(date)"
    echo UURUNDAILY_SCRIPT: "$@"

# 	if "$dryMode"; then
#		return
#	fi
		
    # Silently do nothing
    if test "_$*" = "_"; then
        return
    fi
    eval "$@"
 
    if test $? -ne 0; then
        screamAndDie "failed running $*"
    fi
}


function tryAndRun
{
    echo "$*"

# 	if "$dryMode"; then
#		return
#	fi
		
    # Silently do nothing
    if test "_$*" = "_"; then
        return
    fi

    eval "$@"
}

function svnUp
{
	echo "UURUNDAILY_SCRIPT: Attempting to svn "
	
	tryAndRun "svn up $1"
	if test $? == 0; then
		return
        fi
        echo "UURUNDAILY_SCRIPT: ssh-add -l"
        ssh-add -l
	echo "UURUNDAILY_SCRIPT: svn up $1 failed.  Going to try a cleanup." | tee -a $LOGDIR/$MJD.warning
	tryAndRun "sleep 600"
	tryAndRun "svn cleanup $1"
	printAndRun "svn up $1"
}

function sdss3rsync
{
        echo "UURUNDAILY_SCRIPT: Attempting to rsync MIRROR"

        tryAndRun "rsync -aLrvz --password-file $SDSS3_PASSWD_FILE $1"
        if test $? == 0; then
                return
        fi
        echo "UURUNDAILY_SCRIPT: rsync MIRROR failed. Attempting to rsync MAIN"  | tee -a $LOGDIR/$MJD.warning
        printAndRun "rsync -aLrvz --password-file $SDSS3_PASSWD_FILE $2"
}

#
###	 Start of script
#

###	 Parse command line

update=true
if test $# == 0; then
  	echo "UURUNDAILY_SCRIPT: Wrong number of parameters"
  	usage
elif ! test $# > 2; then
  	echo "UURUNDAILY_SCRIPT: Wrong number of parameters"
  	usage
elif test $# == 2; then
    if test $2="noupdate"; then
        update=false
    else
        echo "UURUNDAILY_SCRIPT: Second parameter must be noupdate"
        usage
    fi
fi
if ! test -z $(perl -n -e 'print "not" unless /^[0-9]{5}$/' <<< $1); then
	echo "UURUNDAILY_SCRIPT: First parameter must be an MJD not $1"
	usage
fi

MJD=$1

###  Start logging

LOGDIR=$HOME/daily/logs
exec >>$LOGDIR/$MJD.log 2>&1
echo "UURUNDAILY_SCRIPT: Starting at $(date), processing $MJD"

module switch eboss eboss/daily
module list
shopt -s expand_aliases

###  Sanity Checks

if test -z $IDLSPEC2D_DIR; then
	screamAndDie "idlspec2d must be setup"
fi
if test -z $PHOTOOP_DIR; then
	screamAndDie "photoop must be setup"
fi

###  Setup environment variables

#  Determine idlspec2d version and set
spec2d=$(perl -p -e 's{.*/(.*)$}{\1}' <<< $IDLSPEC2D_DIR)
echo "UURUNDAILY_SCRIPT: Using idlspec2d version $spec2d"

###
###  Start of main processing
###

skip_photoplate_dir=True
if $update; then
    if [ -z $skip_photoplate_dir ]; then
        echo "UURUNDAILY_SCRIPT: rsync photoObj plates"
        sdss3rsync "--include '*/' --include 'photo*.fits'  --exclude '*' rsync://sdss3@mirror.sdss3.org/sas/bosswork/boss/photoObj/plates/2010-05-23 $BOSS_PHOTOOBJ/plates/" "--include '*/' --include 'photo*.fits'  --exclude '*' rsync://sdss3@data.sdss3.org/sas/bosswork/boss/photoObj/plates/2010-05-23 $BOSS_PHOTOOBJ/plates/"
    else
        echo "UURUNDAILY_SCRIPT: skipping rsync $PHOTOPLATE_DIR (currently this is an eboss.sdss.org job )"
    fi
    echo "UURUNDAILY_SCRIPT: Processing svn updates"
    svnUp $PLATELIST_DIR
    svnUp $SPECLOG_DIR
    svnUp $SPINSPECT_DIR
else
    echo "UURUNDAILY_SCRIPT: Skipping updates"
fi

echo "UURUNDAILY_SCRIPT: Running IDL Pipeline"
if $update; then
    printAndRun "idl -e 'platelist, /create '"
fi
printAndRun "idl -e 'spplan2d, MJD=$MJD '"
printAndRun "idl -e 'spplan1d, MJD=$MJD '"
UUBATCHPBS="uubatchpbs,  /kingspeak, /daily, /verbose, qos='sdss-fast', pbs_walltime='48:00:00', pbs_nodes=1, MJD=$MJD "
printAndRun 'idl -e "$UUBATCHPBS" '

if $update; then
    printAndRun "idl -e 'platelist, /create '"
    printAndRun "idl -e 'platemerge '"
fi

### Generating spec files for this MJD 
printAndRun "reformat_spectra_daily -l -M $MJD" 
printAndRun "reformat_spectra_daily -M $MJD" 

###  All Done
echo "UURUNDAILY_SCRIPT: Completing at $(date)"

###  This is a TEMPORARY hack just to make sure things are looking ok!
grep 'UUBATCHPBS' $LOGDIR/$MJD.log | mail -s "$UUBATCHPBS"  joelbrownstein@physics.utah.edu

