#! /bin/csh
#! /bin/csh
# tarraw - C shell script to tar raw mooring data into .tar.gz files
#	Note - this script then moves the raw files into directory
#	    ./raw/erase.  The intent is to allow ops/TSD folks to
#	    inspect the *.dir files to make sure everything is tar'd
#	    OK, and then erase the raw files from ./raw/erase.  It does
#	    this rather than rm'ing the files in order to allow recovery
#	    from script/system errors.
# This version has been modified so that it can be run after midnight,
# processing only the previous day's files. It takes jan 1 processing
# into account, correcting for leap years appropriately. ( 3/16/01 KLH)

set oasis = /oasis/test
set nonomatch

cd $oasis/raw/tar

foreach mooring (m2)

# TEST CODE ONLY
# Make a file list out of all the files NOT today's...
# set today = `date +%j`
# set filelist = ""
# foreach i (`ls $mooring.* | awk '$1 !~ /'"$today"'/ { print $1 }'`)
# set filelist = "$filelist $i"
# end
# echo $filelist
# exit

# Make a file list of YESTERDAY's files
# if TODAY is Jan 1, then check to see if last year was
# a leap year and assign YESTERDAY accordingly
 
set today = `date +%j`
@ wrap = ($today == "001")

if ( $wrap == 1 ) then
	@ year = `date +%Y`
	@ year -= 1
	@ isleap = (($year % 4) == 0 && ($year % 100) != 0 || ($year % 400) == 0)

	if ($isleap == 1) then
		@ yesterday = (366)
	else
		@ yesterday = (365)
	endif
else
	@ yesterday = ($today - 1)
endif

set filebase = `date +%Y``printf "%03d" $yesterday`

# TEST CODE ONLY
#set filelist = ""
#foreach i ($mooring.$filebase.*)
#if (-e $i) then
#set filelist = "$filelist $i"
#endif
#end


if ( -e $mooring.tar.gz ) \
	/usr/mbari/bin/gunzip $mooring.tar.gz >& /dev/null

cd $oasis/raw
tar rvf tar/$mooring.tar $mooring.$filebase.* >& /dev/null
mv $mooring.$filebase.* erase
cd $oasis/raw/tar
tar tvf $mooring.tar >& $mooring.dir
/usr/mbari/bin/gzip $mooring.tar >& /dev/null

end




