#!/bin/csh

##########################################################################
# IFPD.csh         
#                    set the variable <monitor_dir> to the fully qualified 
#                    path to the incoming monitoring directory
#        
#                    takes the parameters 
#                    <phase>     (phase equal to mort5, ort3 ...)
#                    <data source> (like mist, mspl, stl)
#                    <filter>      (like *.img)
#

echo "##################################################"
echo "## Starting ctmIFPD.csh"
echo "##"
echo Starttime `/bin/date`

set phase="mars"
set data_source="mist"
set filter="*.img"

    set phase=$1
    set data_source=$2
    set filter="$3"

set prefered_host='gluttony.mvacs.ucla.edu'

echo "Phase       = $phase"
echo "Data source = $data_source"
echo "Filter      = $filter"

if($prefered_host != `hostname`) then
    echo "###### WARNING ######     Script should run on $prefered_host"
    sleep 3
endif

# ### Directory Definitions ###
set monitor_dir="/proj/ops/$data_source/$phase/d/fei"
set archive_dir="/proj/ops/$data_source/$phase/d/rcd/sol_unknown/l0/ssirac"
set queue_dir="/proj/ops/ssi/$phase/queues/raw_in"
set status_file="/proj/ops/ssi/$phase/bin/status/IFPD.status"

echo "Monitor Dir = $monitor_dir"
echo "Archive Dir = $archive_dir"
echo "queue_dir   = $queue_dir"
echo "Status File = $status_file"


# #### sleep delay ####
set delay=5                                 #sleep delay

# #### verbose output ####
set vb=1                                    #verbose output if vb=1
	
# #### paused parameter
set paused="no"

# #### terminated parameter
set terminated="no"


echo "Starting IFPD" > $status_file

# ################# Start Script ########################
mkdir -p $archive_dir
mkdir -p $queue_dir

set gnudate="/proj/ops/ssi/bin/SunOS/gnu_date"
set timeStamp=`$gnudate +%Y%m%d%H%M%S`

while($terminated == "no")

	# Handle RTC control files
	cd $monitor_dir
	set rtc_count=`ls *.rtc|wc -l`
	if($vb) echo Found $rtc_count new RTC files     
	if($rtc_count != 0) then
		foreach j(*.rtc)
			if($j == "stop.rtc") then
				set terminated="yes"
				cp $j $queue_dir/$j
				rm $j
			endif
			if($j == "pause.rtc") set paused="yes"
			if($j == "resume.rtc") set paused="no"
		end
	endif

        set monitor_count=`ls $filter|wc -l`
	if($terminated == "no") then
		if($monitor_count != 0) then
			sleep 20
			echo "IFPD Processing files" > $status_file
 	    		cd $monitor_dir 
			echo Start Run
	        	foreach i($filter)
				if($vb) echo start cp cycle for file $i
		    		cp $i $archive_dir/"$i"_$timeStamp
				cp $i $queue_dir
	            		if(-f $archive_dir/"$i"_$timeStamp) then
	              			rm $i
	              			echo $i copied and removed from $monitor_dir
	            		endif 
				if( -r "stop.rtc") break #to be safe
			end #foreach-loop
		else
		    echo "IFPD Running but idle" > $status_file
		endif #if monitor_count loop
	endif
	echo End Run

    sleep $delay

end #while-loop		

echo "IFPD Stopped" > $status_file












