#!/bin/bash

##########################################################################
# fwIFPD.bash   Fairwood - Automatic copt script        
#
#                    csh script for automatically copying data from
#                    a incoming directory (data queue) to a number of 
#                    remote hosts using the secure shell routing scp
#
#                    set the variable <monitor_dir> to the fully qualified 
#                    path to the incoming monitoring directory
#
#                    add all required destinatinations to the <destinations>
#                    variable using the syntax illustrated in the 
#                    <copy destinations> section below
#
#                    make sure that you have added your ~/.ssh/identity.pub
#                    to the ~/.ssh/authorized_keys file on all the destinations
#

echo "##########################################################"
echo "##   Start fwIFPD.bash script"
echo "##"
#echo "StartTime = `time`"


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

# $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 "Debug(`printf "%2d" $1`)  $2"
    fi
}


declare phase=$1
declare source=mist
declare in_queue=/proj/ops/$source/$phase/d/fei
declare to_queue=/proj/ops/ssi/$phase/queues/raw_in
declare filter=$2

DebugEcho $TERSE "Phase    = $phase"
DebugEcho $TERSE "In Queue = $in_queue"
DebugEcho $TERSE "To Queue = $to_queue"
DebugEcho $TERSE "Filter   = $filter"

declare -r prefered_host='gluttony.mvacs.ucla.edu'

if [ $phase == "" ]; then
    echo "Start syntax:  fwADSC.csh <mission phase> <filter>"
    echo "    where <mission phase> is someting like mort5 ..."
    echo "          <filter>     is the file mask (*.img)"
    exit -1
fi

if [ $prefered_host != `hostname` ]; then
    echo "###### WARNING ######     Script should run on $prefered_host"
    sleep 2
fi


# #### incoming monitoring directory ####
declare -r monitor_dir="$in_queue"
DebugEcho $TERSE "monitor directory = $monitor_dir"

if [ ! -d $monitor_dir ]; then 
    DebugEcho $TERSE "Incoming directory NOT EXISTING"
    exit -1
fi
	  
# #### sleep delay ####
declare -r delay=5                                 #sleep delay

# #### verbose output ####
declare -r vb=1                                    #verbose output if vb=1
	
# #### paused parameter
declare -i paused=0

# #### terminated parameter
declare -i terminated=0

do_scp()
{
    DebugEcho $VERBOSE "Copy file: $1 to $to_queue"
    cp $1 $to_queue
}

runtime_control()
{
    DebugEcho $VERBOSE "Start runtime_control function"

    declare -i rtc_count=`ls *.rtc|wc -l`
    DebugEcho $VERBOSE "Found $rtc_count RTC command files"
    if [ $rtc_count -ne 0 ]; then
	DebugEcho $VERBOSE "Start processing of RTC command files"
        for rtc in *.rtc; do
	    DebugEcho $VERBOSE "RTC file $rtc" 

	    if [ "$rtc" == "stop.rtc" ]; then
		terminated=1
		export terminated
		DebugEcho $TERSE "stop.rtc found    TERMINATING"
	    fi
	    if [ "$rtc" == "pause.rtc" ]; then
		paused=1
		DebugEcho $TERSE "pause.rtc found   PAUSE PROCESSING"
	    fi
	    if [ "$rtc" == "resume.rtc" ]; then
		paused=0
		DebugEcho $TERSE "resume.rtc found  RESUMING PROCESSING"
	    fi

	    DebugEcho $VERBOSE "Start do_scp in runtime_control"
	    do_scp $rtc

	    DebugEcho $VERBOSE "remove $rtc from incoming queue"
	    rm $rtc
	done
    fi

    DebugEcho $VERBOSE "End runtime_control function"
}


# ################# Start Script ########################3

# ###### Change directory to incoming queue
cd $monitor_dir

while [ $terminated -eq 0 ]; do

    runtime_control
    DebugEcho $VERBOSE "Terminated=$terminated   Paused=$paused" 

    if [ $paused -eq 0 ]; then

	declare -i new_count=`ls $filter|wc -l`
	
	DebugEcho $VERBOSE "Found $new_count files (Starting to process)"
	if [ $new_count -ne 0 ]; then
	    for file in $filter; do
	        DebugEcho $TERSE "Copying: $file"
		do_scp $file
		runtime_control

		DebugEcho $VERBOSE "Terminated=$terminated   Paused=$paused"

		if [ $terminated -ne 0 ]; then
		    DebugEcho $VERBOSE "Terminate break"
		    break
		fi
		if [ $paused -ne 0 ]; then
		    DebugEcho $VERBOSE "Pause break"
		    break
		fi

		DebugEcho $VERBOSE "remove file: $file"
		rm $file
	    done
        fi
    fi

    sleep $delay;

done


DebugEcho $TERSE "Goodbye from fwIFPD"






