#! /bin/csh
# getm2 - C Shell script to download data from M2
#	     and extract the data with logging
#
# Copyright (C) 2001 MBARI
# Author: Kent Headley
# MBARI provides this documentation and code "as is", with no warranty,
# express or implied, of its quality or consistency. It is provided without
# support and without obligation on the part of the Monterey Bay Aquarium
# Research Institute to assist in its use, correction, modification, or
# enhancement. This information should not be published or distributed to
# third parties without specific written permission from MBARI.

set OASIS = /oasis
set CFG = $OASIS/cfg
set RAW = $OASIS/raw
set BIN = $OASIS/bin

set can = m2
set tnccall = 2002M2
set tncaddr = "$tnccall"
# p: PACCOMM k: Kenwood
set tnctype = p
set tncport = tnc2
set dllog = "dlinfo"
set oldlog = "dlinfo.log"
set cfgfile = $CFG/$can.cfg

cd $RAW
set noclobber
set noglob
set failmail = "headley@mbari.org,w3ab@mbari.org,eric@mbari.org,zorba@mbari.org"
set datafilebase = `date +%Y%j`
set datafile = $can.$datafilebase.00
set errorFilePath = $OASIS/$can/error

set i = 0

while ( -e $datafile )
	@ i++
	set datafile = $can.$datafilebase.`printf "%02d" $i`
end

set starttime = `date +"%x %H:%M:%S"`

$BIN/getoasis -a "$tncaddr" -T $tnctype -p $tncport -c $can >& $datafile
set rtnsts = $status

set endtime = `date +"%x %H:%M:%S"`

set filesize = `ls -l $datafile|sed -f $BIN/fs.sed`
unset noclobber
echo "$datafile,$starttime,$endtime,$filesize,$rtnsts" >> $dllog.$datafilebase
echo "$datafile,$starttime,$endtime,$filesize,$rtnsts" >> $oldlog
set noclobber

#echo -n $rtnsts "  " >> filelist
#ls -l $datafile >> filelist

if ( ($rtnsts) != 0 ) then
	/bin/mailx -s "Failed OASIS Download" $failmail << !
Download failure of file $datafile
Return code was $rtnsts
	0 = Success
	1 = Argument error (bad command line arguments)
	2 = Couldn't acquire TNC port (hung download or port in use)
	3 = Couldn't connect to OASIS (it's not listening)
	4 = Download error (something broke after it connected)
	5 = Abort error (somebody killed the download)
`date` *** message from $tnccall ***
!
endif

set errorfile = $errorFilePath/$datafile

if ( (-e $datafile) && !(-z $datafile) ) then
	$BIN/extract -r -n -y 1 -c $cfgfile $RAW/$datafile >& $errorfile
endif

if ( -z $errorfile ) \
	rm -f $errorfile





