#!/bin/csh

##########################################################################
# fwADSC_MSOC.csh   Fairwood - Automatic Downlink Secure Copy (for MSOC)        
#
#                    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 "######### Starting fwADSC_MSOC.csh"

# #### incoming monitoring directory ####
set monitor_dir="~sfhviid/mvacs/monitor"    #monitoring directory

# #### copy destinations ####
set destinations=(stubbe@kirstine.fys.ku.dk:~stubbe/tmp)
set destinations=($destinations stubbe@kirstine.fys.ku.dk:~stubbe/tmp/s1)
set destinations=($destinations stubbe@kirstine.fys.ku.dk:~stubbe/tmp/s2) 
		  
# #### sleep delay ####
set delay=5                                 #sleep delay

# #### verbose output ####
set vb=1                                    #verbose output if vb=1
		  

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

eval `ssh-agent -c`
ssh-add

while(1)
    set new_count=`ls $monitor_dir|wc -l`

    if($vb) echo Found $new_count new files

    if($new_count != 0) then
    echo Start Run
    foreach i($monitor_dir/* )
	if($vb) echo start scp cycle for file $i 
	foreach d($destinations)
	    if($vb) echo scp to $d
	    scp -q $i $d
	end
	
	if($vb) echo rm $i
	rm $i
    end
    echo End Run
    endif

    sleep $delay
end

