#! /usr/bin/tcsh
# doDeploySetup - C Shell script to move data for mooring deployment
# This script is generally used by the wrapper script deployMoor,
# which issues a warning, prompts for OK before proceeding, and 
# redirects stdout and stderr to a log file.
# Since it doesn't do any user interaction, doDeploySetup can be 
# used standalone, or be called from another script.
#

if ($#argv < 2 ) then
echo "Usage $0 <mooring> <archive dir> [<instrument>...]"
echo "mooring:    mooring directory name (m1)"
echo "archivedir: directory name (may2000)"
echo "instrument: new instrument directory names (fluor metsys)"
echo " "
echo "To capture to a logfile, use:"
echo "  $0 <mooring> <archive dir> [<instrument>...] "'2>&1 | tee -a <logfile>'
exit
endif

set mooring = $1
set archive = $2

set oasis = /oasisnt/oasis/test
set mooringdir = $oasis/$mooring
set archdir = $oasis/$archive
set tardir = $oasis/raw/tar
set rawdir = $oasis/raw
set cfgdir = $oasis/cfg
set srcdir = $oasis/src
set bindir = $oasis/bin
set headerdir = $oasis/cfg/$mooring
set erasedir = $oasis/raw/erase
set PROCESS = $oasis/bin/process

echo "\nThis is a potentially destructive script. Do not use it unless you know exactly what it does."
echo "Make sure that downloads are OFF, and that no download\n is currently in progess.\n"
echo "You are about to perform deployment file maintenance for $mooring."
echo "Old data will be stored in $archdir."

# wait for key press to continue (leave for wrapper to do)
#echo "Press ^C now to exit or any other key to proceed"
#set foo = ($<)

# check directories
echo "Checking directories..."
set ok=true
if ( ! -e "$mooringdir" ) then
 echo "warning: $mooringdir doesn't exist"
 set ok=false
endif
if ( ! -e "$tardir" ) then
 echo "warning: $tardir doesn't exist"
 set ok=false
endif
if ( ! -e "$rawdir" ) then
 echo "warning: $rawdir doesn't exist"
 set ok=false
endif
if ( ! -e "$cfgdir" ) then
 echo "warning: $cfgdir doesn't exist"
 set ok=false
endif
if ( ! -e "$srcdir" ) then
 echo "warning: $srcdir doesn't exist"
 set ok=false
endif
if ( ! -e "$bindir" ) then
 echo "warning: $bindir doesn't exist"
 set ok=false
endif
if ( ! -e "$headerdir" ) then
 echo "warning: $headerdir doesn't exist"
 set ok=false
endif
if ( ! -e "$erasedir" ) then
 echo "warning: $erasedir doesn't exist"
 set ok=false
endif

if ( "$ok" == "false" ) then
 echo "There are errors. Exiting."
 exit -1
endif

# initialize warning count
set warning = 0

echo "directories OK"
#echo "exiting...comment this line to continue"
#exit

# create archive directory
echo "\n\nCreating Directories..."
mkdir $archdir
mkdir $archdir/raw

if ( ! -e "$archdir" ) then
 echo "warning: $archdir doesn't exist"
 @ warning++
endif

if ( ! -e "$archdir/raw" ) then
 echo "warning: $archdir/raw doesn't exist"
 @ warning++
endif

echo "Processing Current Data..."
$PROCESS $mooring

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: $PROCESS returned $stat"
 @ warning++
endif

echo "Archiving Current Data..."
# TAR file cleanup...
cd $tardir
pwd
# unzip the tar archive...
gunzip $mooring.tar.gz

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: gunzip returned $stat"
 @ warning++
endif

cd $rawdir
pwd
cp $mooring.* $erasedir

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: could not copy $mooring data files in $rawdir to $erasedir ($stat)"
 @ warning++
endif

# put any remaining raw files in the archive...
tar -rvf $tardir/$mooring.tar $mooring.*

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: tar update failed ($stat)"
 @ warning++
endif

# remove the data files
rm $mooring.*

# update the tar directory file
# note parenthesis for subshell 
# (wrapper IO redirection won't work w/o this)
(tar -tvf $tardir/$mooring.tar > $tardir/$mooring.dir)

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: tar directory update failed ($stat)"
 @ warning++
endif

# zip up tar file and move to archive
cd $tardir
gzip $mooring.tar

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: gzip failed ($stat)"
 @ warning++
endif

echo "Moving Archive..."
mv $mooring.* $archdir/raw

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: could not move $tardir/$mooring.* archive files to $archdir/raw ($stat)"
 @ warning++
endif

mv $mooringdir $archdir

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: could not move $mooringdir  to $archdir ($stat)"
 @ warning++
endif

echo "Moving System Files..."
# archive bin, src, cfg directories
cp -r $bindir $archdir

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: copy $bindir failed ($stat)"
 @ warning++
endif

cp -r $srcdir $archdir

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: copy $srcdir failed ($stat)"
 @ warning++
endif

cp -r $cfgdir $archdir

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: copy $cfgdir failed ($stat)"
 @ warning++
endif

echo "Creating New Raw Data Archive..."
# create new tar files
# note parenthesis for subshell 
# (wrapper IO redirection won't work w/o this)
(date > newarch)
tar -cvf $mooring.tar newarch

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: could not create $mooring.tar  ($stat)"
 @ warning++
endif

# note parenthesis for subshell 
# (wrapper IO redirection won't work w/o this)
(tar -tvf $mooring.tar > $mooring.dir)

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: could not create $mooring.dir  ($stat)"
 @ warning++
endif

gzip $mooring.tar

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: gzip failed  ($stat)"
 @ warning++
endif

rm -f newarch

echo "Removing Headers..."
rm $headerdir/*.hdr

echo "Creating New Directories..."
# create new mooring directories

cd $oasis
echo "$mooring..."
mkdir $mooring

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: could not create $oasis/$mooring directory ($stat)"
 @ warning++
endif

cd $mooring
echo "error..."
mkdir error

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: could not create $oasis/$mooring/error directory ($stat)"
 @ warning++
endif

echo "data..."
mkdir data

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: could not create $oasis/$mooring/data directory ($stat)"
 @ warning++
endif

echo "oasis..."
mkdir oasis

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: could not create $oasis/$mooring/oasis directory ($stat)"
 @ warning++
endif

if ($#argv >= 2) then
@ i = 3
while ($#argv >= $i )
echo "$argv[$i]..."
mkdir $argv[$i]

set stat = $status
if ( "$stat" != 0 ) then
 echo "warning: could not create $oasis/$mooring/$argv[$i] directory ($stat)"
 @ warning++
endif

@ i++
end
endif

let archsz = `ls -l $tardir/$mooring.tar.gz|sed -f $bindir/fs.sed`
if( "$archsz" <= 0 )then
 echo "warning: archive size <= 0; check $tardir/$mooring.tar.gz"
 @ warning++
endif

if ( $warning > 0 ) then
 echo "There are $warning warnings."
endif

echo "Done.\n Copies of the most recent raw data are in $erasedir."
echo "Remember to:"
echo "  - Verify that new data archive exists and its size > 0"
echo "  - Update Header Files ($cfgdir/$mooring/)"
echo "  - Replace $bindir/$mooring.cfg"
echo "  - Modify TNC callsign in  $bindir/get$mooring"
echo "  - Reset oasis.rcd $mooring records"
echo "  - Restore Downloads"

