#!/bin/bash

########################################
# MVACS data pipeline master startup
#
# Created: 18/10-1999 Stubbe F. Hviid
#
echo ""
echo "######################################################"
echo "# Welcome to the Fairwood MVACS DMS Master Script v2.0"
echo "# (c) 1999 Stubbe F. Hviid"
echo "#"
echo Start Time `/bin/date`
echo ""

########################################
# Debugging verbosity control

declare -ri TERSE=1
declare -ri TRACE=4
declare -ri VERBOSE=8
declare -ri BOMBASTIC=16

if [ -z "$SSI_VERBOSITY" ]; then
    declare -i debugLevel=4
else
    declare -i debugLevel=$SSI_VERBOSITY
fi

# Twisted way to get down to the fundamental script name.
tmp=${0##/*/}
tmp=${tmp%%.bash}
tmp=${tmp##*.}
tmp=${tmp##*/}
declare -r SCRIPT_NAME=$tmp


# $1 is the level to compare against debugLevel
# $2 is the string to echo to stdout.
DebugEcho()
{
    declare -ir val=$1
    if [ "$debugLevel" -ge "$val" ]; then
	#echo $2
	echo "${SCRIPT_NAME}(`printf "%02d" $1`) $2"
    fi
}

#DebugEcho $TERSE     "Terse is on"
#DebugEcho $TRACE     "Trace is on"
DebugEcho $VERBOSE   "Verbose is on"
DebugEcho $BOMBASTIC "Bombastic is on"

DebugEcho $TERSE "debugLevel               = $debugLevel"

# ######## Set umask for every thing that follows
# group read and write + user read and write + everyone read
DebugEcho $TERSE "Setting umask = 002"
umask 002

# ######## declare host binaries (non project binaries)
declare -r cp=/bin/cp
declare -r rm=/bin/rm

# ######## declare variables
declare -ri EXIT_FAILURE=1
declare -ri EXIT_SUCCESS=0

declare legal_host=gluttony.mvacs.ucla.edu
declare base_dir=/proj/ops/ssi
declare task=mars
declare data_source=mist

# ######## DEFINE functions

# Make sure a directory exists.
createVerifyDir()
{
    declare -r newDir=$1
    if [ ! -d $newDir ]; then
	DebugEcho $VERBOSE "     Creating $newDir"
	mkdir -p $newDir
	chgrp sys.ssi $newDir
    fi
    if [ ! -d $newDir ]; then
	echo ls -l $newDir
	ls -l $newDir
	echo ""
	echo "ERROR: Bugger off.. $newDir is not a directory!"
	exit $EXIT_FAILURE
    fi
}


strToFile()
{
    #echo "$2    -> $1"
    echo $2>>$1
}


echo "######## Check that the computer is $legal_host"
if [ $legal_host != `hostname` ]; then
 echo "This script can only be run on $legal_host"
 echo "... NOT FAILING ... Just delaying ..."
 sleep 2
 #exit $EXIT_FAILURE
fi

# can have 1 or 2 arguments.  $# is not the same as the C argc.
if [ $# -eq 1 ]; then
    true
elif [ $# -eq 2 ]; then
    true
elif [ $# -eq 3 ]; then
    true
else
    echo 
    echo "ERROR: incorrect number of arguments."
    echo "syntax:   fwMaster.csh task_name [root directory]"
    DebugEcho $VERBOSE "Should give a bit more info here"
    exit $EXIT_FAILURE
fi

# ######## set base=root of the ssi filesystem
DebugEcho $BOMBASTIC "FIX: This does not seem to be working"
if [ $1 = "" ]; then
 echo "syntax:   fwMaster.csh task_name [root directory]"
 exit $EXIT_FAILURE
fi

# read task name from argv (exist if script reaches this point)
task=$1

# check if a alternative root directory is given on the argv
if [ ! -z $2 ]; then
 base_dir=$2
fi

if [ ! -z $3 ]; then
 data_source=$3
 echo "Data Source = $3 ($data_source)"
else
 echo "Using default data source (mist)"
fi

# ######## print the task and base value used
DebugEcho $TERSE "Base Directory = $base_dir"
DebugEcho $TERSE "Task Name      = $task"

echo "######## define directory variables"
# defaults to /proj/ops/ssi/mars/data
declare -r task_dir=$base_dir/$task
declare -r data_dir=$task_dir/data
declare -r bin_dir=$task_dir/bin
declare -r queue_dir=$task_dir/queues
declare -r store_dir=$data_dir/store
declare -r log_dir=$bin_dir/log
declare -r config_dir=$bin_dir/config
declare -r runtime_config_dir=$bin_dir/runtime_config
declare -r status_dir=$bin_dir/status

DebugEcho $VERBOSE ""
DebugEcho $VERBOSE "     Task Dir           = $task_dir"
DebugEcho $VERBOSE "     Data Dir           = $data_dir"
DebugEcho $VERBOSE "     Bin Dir            = $bin_dir"
DebugEcho $VERBOSE "     Queue Dir          = $queue_dir"
DebugEcho $VERBOSE "     Storage Dir        = $store_dir" 
DebugEcho $VERBOSE "     Log_dir            = $log_dir"
DebugEcho $VERBOSE "     Config Dir         = $config_dir"
DebugEcho $VERBOSE "     Status Dir         = $status_dir"
DebugEcho $VERBOSE "     Runtime Config Dir = $runtime_config_dir"

DebugEcho $VERBOSE ""
DebugEcho $VERBOSE "###### Declaring scripts and binaries to be used"
declare -r fwDMS=$bin_dir/`uname`/FwDMS
declare -r StartCal=$bin_dir/fwStartCal.bash
declare -r MCP=$bin_dir/MCP.bash
declare -r ADSC=$bin_dir/fwADSC_MSOC.csh
declare -r StartPio=$bin_dir/pioToWeb.bash

declare -r gnuDate=${bin_dir}/`uname`/gnu_date


if [ "IRIX64" == "`uname`" ]; then
    # This is where SGI puts java.
    declare -r java=/usr/java/bin/java
else
    declare -r java=/u1/tso/tools/Solaris_JDK_1.2.1_03/bin/java
    DebugEcho $TERSE " ------ WARNING Using non official version of java"
fi

DebugEcho $VERBOSE "     Using following programs:"
DebugEcho $VERBOSE "     $fwDMS"
DebugEcho $VERBOSE "     $dummyCal"
DebugEcho $VERBOSE "     $MCP"
DebugEcho $VERBOSE "     $ADSC"
DebugEcho $VERBOSE "     $java"


DebugEcho $VERBOSE ""
DebugEcho $VERBOSE "###### Creating Directory Structure"
createVerifyDir $queue_dir
$cp $base_dir/doc/queue_description.txt $queue_dir
chgrp sys.ssi $queue_dir/queue_description.txt

createVerifyDir $queue_dir/raw_in
createVerifyDir $queue_dir/rawToWorld
createVerifyDir $queue_dir/toCal
createVerifyDir $queue_dir/rawToNT
createVerifyDir $queue_dir/icalToNT
createVerifyDir $queue_dir/ical_in
createVerifyDir $queue_dir/icalToWorld
createVerifyDir $queue_dir/toJames
createVerifyDir $queue_dir/toPio
createVerifyDir $queue_dir/webToWorld
# Will toMosaic be used?  Should it be renamed?
createVerifyDir $queue_dir/toMosaic
createVerifyDir $queue_dir/toSay
createVerifyDir $queue_dir/toWeb
chgrp sci.ssi $queue_dir/toWeb
chmod g+s $queue_dir/toWeb

createVerifyDir $data_dir
createVerifyDir $store_dir
createVerifyDir $store_dir/raw
createVerifyDir $store_dir/ical
createVerifyDir $data_dir/raw
createVerifyDir $data_dir/ical

createVerifyDir $data_dir/wits
createVerifyDir $data_dir/vap
createVerifyDir $data_dir/vap/vrml
createVerifyDir $data_dir/vap/disparity
createVerifyDir $data_dir/vap/range
createVerifyDir $data_dir/vap/mosaic
createVerifyDir $data_dir/vap/images
createVerifyDir $data_dir/vap/images/anaglyph
createVerifyDir $data_dir/vap/images/gif
createVerifyDir $data_dir/vap/images/jpg
createVerifyDir $data_dir/vap/images/tif

createVerifyDir $data_dir/vap/super-res
createVerifyDir $data_dir/vap/viz-product

createVerifyDir $data_dir/to-be-released
createVerifyDir $data_dir/released

$cp $base_dir/doc/README.vap $data_dir/vap/
$cp $base_dir/doc/README.data $data_dir/

DebugEcho $VERBOSE "Chgrp on $data_dir/vap/images to allow sci.ssi to write"
# Send stderr to dev null... probably won't own most of the files if 
# this is a restart.
chgrp sci.ssi $data_dir/to-be-released 2> /dev/null
chgrp sci.ssi $data_dir/released 2> /dev/null
chmod o+w $data_dir/to-be-released 2> /dev/null
chmod g+s $data_dir/to-be-released 2> /dev/null
chmod g+s $data_dir/released 2> /dev/null

chgrp sci.ssi $data_dir/vap/super-res 2> /dev/null
chgrp sci.ssi $data_dir/vap/viz-products 2> /dev/null
chgrp sci.ssi $data_dir/vap/mosaic 2> /dev/null
chgrp -R sci.ssi $data_dir/vap/images 2> /dev/null

chmod g+s $data_dir/vap/super-res 2> /dev/null
chmod g+s $data_dir/vap/viz-products 2> /dev/null
chmod g+s $data_dir/vap/mosaic 2> /dev/null
chmod -R g+s $data_dir/vap/images 2> /dev/null

$cp $base_dir/doc/README.vap.images $data_dir/vap/images/
chgrp sys.ssi $data_dir/vap/images/README.vap.images

DebugEcho $VERBOSE "     Done"

DebugEcho $VERBOSE ""
DebugEcho $VERBOSE "###### Change directory to $task_dir"
cd $task_dir


DebugEcho $VERBOSE ""
DebugEcho $VERBOSE "###### Test if binary directory already exists"

if [ -d $bin_dir ]; then
  declare -r do_bin_copy='no'
  DebugEcho $VERBOSE "     ---- $bin_dir already exists (Skipping file copy)"
else
  declare -r do_bin_copy='yes'
  DebugEcho $VERBOSE "     ---- $bin_dir not existing (do file copy)"
fi


DebugEcho $VERBOSE ""
DebugEcho $VERBOSE "##### Create Binary directory structure"
createVerifyDir $bin_dir
createVerifyDir $bin_dir/SunOS
createVerifyDir $bin_dir/IRIX
createVerifyDir $bin_dir/IRIX64
createVerifyDir $bin_dir/config
createVerifyDir $bin_dir/log
createVerifyDir $bin_dir/calibration
createVerifyDir $bin_dir/java
createVerifyDir $bin_dir/runtime_config
createVerifyDir $bin_dir/status
DebugEcho $VERBOSE "     Done"


DebugEcho $VERBOSE "##### Removing all rtc files from the queue directories"
for d in $queue_dir/*; do
    if [ -d $d ]; then
	DebugEcho $VERBOSE "Clearing rtc files from $d"
	$rm -f $d/*.rtc
    fi
done


DebugEcho $VERBOSE "##### Setting SSI_TOP_DIR"
declare -r SSI_TOP_DIR=$task_dir
export SSI_TOP_DIR

DebugEcho $VERBOSE ""
DebugEcho $VERBOSE "##### Setting SSI_RUNTIME_CONFIG_DIR"
DebugEcho $VERBOSE "      Write runtime configuration changes to this directory"   
export SSI_RUNTIME_CONFIG_DIR=$runtime_config_dir

DebugEcho $VERBOSE ""
DebugEcho $VERBOSE "##### Setting SSI_STATUS_DIR"
DebugEcho $VERBOSE "      Write PID and status messages to files in this directory"
export SSI_STATUS_DIR=$status_dir

DebugEcho $VERBOSE "##### Checkout files out of the root binary directory"
DebugEcho $VERBOSE "      !!! Note using cp at the moment, should use cvs"
if [ $do_bin_copy = "yes" ]; then
  echo "     ---- do filecopy"
  #if [ ! `/bin/cp -R $base_dir/bin/* $bin_dir` ]; then
  $cp -R $base_dir/bin/* $bin_dir
  declare -i result=$?
  if [ 0 -ne $result  ]; then
    echo "     $result ERROR ---- Source binary files not found ---- EXITING"
    echo "     tried to cp from $base_dir/bin/*"
    echo " Not exiting..."
    sleep 1
    #exit $EXIT_FAILURE
  fi
  chgrp -R sys.ssi $bin_dir
else
  DebugEcho $VERBOSE ""
  echo "     Skipping binary filecopy"
  DebugEcho $VERBOSE ""
fi

DebugEcho $VERBOSE "     Making sure that java is set up correctly"
if [ ! -L $bin_dir/`uname`/java ]; then
    $rm -f $bin_dir/`uname`/java 
    ln -s $java $bin_dir/`uname`/java
fi

# This must happen after the cp that makes sure the binary is in place.
declare -r dateStamp=`$gnuDate +%Y%m%d`
if [ -z "$SSI_SOL_UNKNOWN" ]; then
    declare -r SSI_SOL_UNKNOWN=sol_unknown_${dateStamp}
    DebugEcho $TRACE "setting SSI_SOL_UNKNOWN      = $SSI_SOL_UNKNOWN"
fi
export SSI_SOL_UNKNOWN


DebugEcho $VERBOSE ""
DebugEcho $VERBOSE "######### Generate fwDMS config files"

declare -r raw_config=$config_dir/fwDMS_raw.conf
declare -r raw_log=$log_dir/fwDMS_raw_stdio.log
$rm -f $raw_config
echo "     Building $raw_config"

strToFile $raw_config -store=$store_dir/raw
strToFile $raw_config -incoming=$queue_dir/raw_in
strToFile $raw_config -incoming=/proj/ops/${data_source}/${task}/d/fei
strToFile $raw_config -backup_store=/proj/ops/${data_source}/${task}/d/fei/ssi_rac
strToFile $raw_config -archive=$data_dir/raw
strToFile $raw_config -pipe=$queue_dir/rawToNT 
strToFile $raw_config -pipe=$queue_dir/rawToWorld 
strToFile $raw_config -pipe=$queue_dir/toCal
strToFile $raw_config -spipe=$queue_dir/toJames
strToFile $raw_config -process_name=fwDMS_raw
strToFile $raw_config -log_dir=$log_dir
strToFile $raw_config -status_dir=$bin_dir/status
strToFile $raw_config -default_sol=$SSI_SOL_UNKNOWN
strToFile $raw_config -npipe=$queue_dir/toPio
strToFile $raw_config -sleep=2000 
strToFile $raw_config -verbose 

# ---- fwDMS_ical ----
declare -r ical_config=$config_dir/fwDMS_cal.conf
declare -r ical_log=$log_dir/fwDMS_cal_stdio.log
$rm -f $ical_config
echo "     Building $ical_config"

strToFile $ical_config -store=$store_dir/ical 
strToFile $ical_config -incoming=$queue_dir/ical_in 
strToFile $ical_config -archive=$data_dir/ical 
strToFile $ical_config -pipe=$queue_dir/icalToWorld 
strToFile $ical_config -pipe=$queue_dir/icalToNT 
strToFile $ical_config -sleep=2000 
strToFile $ical_config -reprocess_stereo 
strToFile $ical_config -verbose 
strToFile $ical_config -status_file=$status_dir/fwDMS_ical.status
strToFile $ical_config -log_dir=$log_dir
strToFile $ical_config -process_name=fwDMS_ical
strToFile $ical_config -status_dir=$bin_dir/status
strToFile $ical_config -filter=*.cal
strToFile $ical_config -default_sol=$SSI_SOL_UNKNOWN


declare -r fei_ssi_config=$config_dir/fei_ssi.options

if [ ! -f $fei_ssi_config ]; then
DebugEcho $TRACE "Building $fei_ssi_config"
# NOTE: these must be hard/actual tabs for the "-" to remove them!!
cat <<- EOF > $fei_ssi_config
	replace
	logFile $log_dir/fei_ssi.log
	#mailMessageTo user@host
	limitMessages
EOF
else
    DebugEcho $VERBOSE "$fei_ssi_config already exists.  Will use that."
fi



declare -r fei_rac_config=$config_dir/fei_rac.options

if [ ! -f $fei_rac_config ]; then
DebugEcho $TRACE "Building $fei_rac_config"
# NOTE: these must be hard/actual tabs for the "-" to remove them!!
cat <<- EOF > $fei_rac_config
	replace
	logFile $log_dir/fei_rac.log
	#mailMessageTo luser@host
	limitMessages
EOF
else
    DebugEcho $VERBOSE "$fei_rac_config already exists.  Will use that."
fi

DebugEcho $VERBOSE ""
DebugEcho $VERBOSE "####### Starting processing demons #######"


# NOTE: IFPD is no longer necessary!!! Yippy!

#DebugEcho $VERBOSE "####### Starting the ctmIFPD.csh demon"
#DebugEcho $VERBOSE "Logging to: $log_dir/ctmIFPD.log"
#$fwIFPD $task "*.*" > $log_dir/ctmIFPD.log&
#$IFPD $task $data_source "*.img" > $log_dir/ctmIFPD.log 2>&1 &
#$IFPD $task $data_source "*.img" > $log_dir/ctmIFPD.log &

DebugEcho $VERBOSE ""
DebugEcho $VERBOSE "####### Start fwDMS demons"
DebugEcho $VERBOSE "     Logging to:"
DebugEcho $VERBOSE "       $raw_log"
DebugEcho $VERBOSE "       $ical_log"
#$fwDMS -file=$raw_config > $raw_log 2>&1 &
#$fwDMS -file=$ical_config > $ical_log 2>&1 &
$fwDMS -file=$raw_config > $raw_log 2>&1 &
$fwDMS -file=$ical_config > $ical_log 2>&1 &

DebugEcho $VERBOSE "########### Start calibration routine"
$StartCal $task > $log_dir/cal_msg.log 2>&1  &

DebugEcho $TRACE "###### Pio data pipe (fwStartPio)"
echo "$StartPio $queue_dir/toPio uc 30 > $log_dir/pio.log"
#$StartPio  uc 15 > $log_dir/pio.log 2>&1 &
$StartPio  uc 15 > $log_dir/pio.log 2>&1 &

DebugEcho $VERBOSE "###### Start JAMES"
echo "     Using $SSI_TOP_DIR  as SSI_TOP_DIR"
$MCP uc 15 > $log_dir/MCP.log 2>&1 &

echo "fwMaster.bash is done - have a nominal day."
