#!/bin/csh -f
# addinventory-level2

# Adds new status entries to level 2 inventory file.

# Makes this work for Southern Ocean Data  
#   Oct. 15, 2001     mda
#
# December 28, 1998.  Try to remove more or less blank comment lines and
#	duplicate "Last updated" comments.  rcg
# R. Groman May 1, 1997
# Enhance handling of desciptions with embedded spaces

# Passed parameters are:
#	1 == project
#	2 == datatype
#	3 - end == status and description, in pairs
#
# Notes: The description field is "translated" before output as follows:
#		% ===> space
#		] ===> > (greater than symbol)
#		[ ===> < (less than symbol)
#		{ ===> " (double quote)

#echo " <p>**DEBUG in routine addinventory-level2"
#echo " <p>**DEBUG - debug version of addinventory-level2"
cd /data/Inventory/scripts

set backup=/data/Inventory/so/backup

set datadir=/data/Inventory/so
#set datadir=/data/rgroman/Inventory/debug

#echo " <p>**DEBUG datadir=$datadir"

#set scriptcgi="invtest"
set scriptcgi="inv"

set numbarg=$#argv
#echo " <p>**DEBUG addinventory-level2, argv = $argv"
#echo " <p>**DEBUG numbarg= $numbarg"
#echo " <p>**DEBUG addinventory-level2, argv[4] = $argv[4]"

set current_project=$argv[1]
#echo " <p>**DEBUG current_project = $current_project"
	
if ( $numbarg == 1 ) exit
set datatype=$argv[2]
#echo "<p>**DEBUG datatype = $datatype"

#Create stub datafile (called level3_stub), but really level 2
if ( ! -e ${datadir}/${current_project}-${datatype}.dat ) then
	cp ${datadir}/level3-stub.dat ${datadir}/${current_project}-${datatype}.dat
#	echo " <br>Created file ${datadir}/${current_project}-${datatype}.dat"
else
	:
#	echo " <br>File ${datadir}/${current_project}-${datatype}.dat exists."
#	echo " Will use existing version."
endif

set day=`date '+%d'`
set month=`date '+%m'`
set year=`date '+%Y'`
#echo " <p>**DEBUG year month day = $year $month $day"
set level2file=${1}-${2}.dat
#echo " <p>**DEBUG level2file = $level2file"
#Make backup of existing data file just in case
set TMP=`date '+%Y.%b.%d.%H%M'`
cp ${datadir}/$level2file $backup/${level2file}-$TMP
#echo " <p>**DEBUG made backup of level two file"

if ( -e /tmp/$level2file ) \rm -f /tmp/$level2file
cp ${datadir}/$level2file /tmp/$level2file
#echo " <p>**DEBUG made temp version for manipulation purposes"

#Save comments
if ( -e /tmp/comments ) \rm -f /tmp/comments
grep "#" /tmp/$level2file | grep '# \{0,10\}[a-z,A-Z,0-9]' | grep -v "Last updated" > /tmp/comments
#echo " <p>**DEBUG made temp comments file"

#Add current date to comments
echo "#Last updated: $TMP" >> /tmp/comments

#Save existing data with and without field names
if ( -e /tmp/data ) \rm -f /tmp/data
grep -v "#" /tmp/$level2file > /tmp/data
\rm /tmp/$level2file
#echo " <p>**DEBUG extract out data with field names"

#Save any data lines
if ( -e /tmp/data1 ) \rm -f /tmp/data1
tail +2 /tmp/data > /tmp/data1
#echo " <p>**DEBUG extract out data only"

#Save field name lines
if (-e /tmp/fieldnames ) \rm -f /tmp/fieldnames
head -1 /tmp/data > /tmp/fieldnames
#echo " <p>**DEBUG addinventory-level2, extract out field names"

@ counter = 3
#echo " <p>**DEBUG addinventory-level2, argv[3-numbarg]=$argv[3-$numbarg]"
foreach statustype ($argv[3-$numbarg])
#	echo " <p>**DEBUG statustype = $statustype"
	@ counter = $counter + 1
#	echo " <p>**DEBUG counter = $counter"	
	@ skip=$counter % 2
#	echo " <p>**DEBUG counter = $counter and skip = $skip"
	if ( $skip == 1 ) continue
	@ pos = $counter
#	echo " <p>**DEBUG pos = $pos"
	set description=\"$argv[$pos]\"
#	echo " <p>**DEBUG description = $description"

#	Try to create link if possible.
	if ($datatype == "eventlog" && $argv[$pos] == "on-line") then
		set description="[a%href={http://globec.whoi.edu/jg/serv/globec/soglobec/eventlogs.html1?cruiseid=$current_project{]on-line[/a]"
	endif
#	Add new record to level 1 (must replace spaces with tabs first)
#	Assumes desciption field used % to mean spaces to be retained
#	Converts % to space just before writing, and enclosing text
#	in double quotes.
#	echo " <p>**DEBUG addinventory-level2, description=$description"
	set record="${statustype} $year $month $day ${description}"
#	echo " <p>**DEBUG record = $record"
	echo "$record"  | tr '\040' '\011' | tr '\045' '\040' | tr '\133' '\074' | tr '\135' '\076' | tr '\173' '\042' >>/tmp/data1

end

\rm -f /tmp/data

cat /tmp/comments /tmp/fieldnames /tmp/data1 >$datadir/$level2file
\rm /tmp/data1
\rm /tmp/comments
\rm /tmp/fieldnames
exit 0
