#! /bin/bash
#
# push_cips_hires_to_pdc.sh
#
# Author: Lon Riesberg
#
# Purpose:
#   Pushes the most recent CIPS Hi-Res L2 strips to the Hampton
#   University ftp site.  This is set up specifically for 
#   access by NASA for shuttle missions and ISS coordination.    
#
# Notes:
#   This script run as a cron job on mods-apps.
#      
# Date
#   November 17, 2008
#

pathToOutgoingData="/aim/data/cips/hires/outgoing/"
pathToSentData="/aim/data/cips/hires/sent/"

# directory for today (e.g. 2009-345/) at hamptonu
year=`date '+%Y'`
today=`date '+%j'`
yesterday=$((today - 1))
twoDaysAgo=$((today - 2))

# if files available, push to hampton
outgoingFiles=`ls ${pathToOutgoingData}`
for file in ${outgoingFiles}
   do
    
   # files are created for the previous 48 hours and put into 
   # ${pathToOutgoingData}.  that means this script may find
   # files that should be pushed to different directories.
   # in the case that there are files in ${pathToOutgoingData}
   # that are older than a couple days, those files will
   # need to moved manually.
   moveFile=0
   
   if [[ "$file" == *"${year}-${twoDaysAgo}"* ]]
   	then
	   destination_dir="~/ftp_shuttle/${year}-${twoDaysAgo}"
	   moveFile=1     
   
   elif [[ "$file" == *"${year}-${yesterday}"* ]]
   	then
	   destination_dir="~/ftp_shuttle/${year}-${yesterday}" 
	   moveFile=1 
		    
   elif [[ "$file" == *"${year}-${today}"* ]]
   	then
	   destination_dir="~/ftp_shuttle/${year}-${today}"
	   moveFile=1  
	fi  	   
	
	# scp $file to hampton.
	# on success, move ${pathToOutgoingData}/${file} to ${pathToSentData}
   if [ $moveFile == 1 ]
   	then   
		`scp -v ${pathToOutgoingData}${file} aim_cips@aim.hamptonu.edu:${destination_dir}/${file}`  
		if [ $? == 0 ]
			then
			fromFile=${pathToOutgoingData}/${file}
			destFile=${pathToSentData}/${file}
			`mv ${fromFile} ${destFile}`
		fi
	fi
      
done


exit 0
#Done
