#!/bin/csh -f

# /bin/csh -xv

# doupate  Update the U.S. GLOBEC Georges Bank Inventory files

# Edited March 17, 1998  Change /data to /data5 & httpd.alt reference. rcg
# February 20, 1997 rcg

echo "Content-type: text/html"
echo ""

echo "<title>Update to U.S. GLOBEC Georges Bank Inventory, level 1</title>"
echo "<h2 align=center>Update to U.S. GLOBEC Georges Bank Inventory,"
echo "<br>level 1</h2>"

setenv HD /data5/globec
set script_loc=/data/rgroman/Inventory/scripts
set instrument_loc=/tmp/instruments

set datatop = /data/rgroman/Inventory
#set datatop = /data/rgroman/Inventory/debug
#echo "<p> *** Note: you are in DEBUG mode, datatop = $datatop <p>"

set inventorytop=$datatop/toplevel.dat

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

cd $script_loc

setenv PATH_INFO /globec/gb/inventory.html0

set tmp=$PATH_INFO
setenv OBJ $tmp:r
unsetenv PATH_INFO

#set POSTDATA=(`/data/rgroman/Inventory/scripts/test-env`)
set  POSTDATA=(`/data/rgroman/Inventory/scripts/poststring`)

setenv TMP `date '+%Y.%m.%d'`

#set existing=`/data5/globec/bin/transfer http://globec.whoi.edu/jg/serv/globec/gb/inventory.htm0 | grep "$POSTDATA[1]"`

set existing="`grep $POSTDATA[1] ${inventorytop} | tr ' ' '\012' | tr '\011' '\012' `"
set dolist=yes

if($#existing == 0) then

#	Do update of new record
###	set curopt="`cat inventoryopt | tr ' ' '\011' `"
	echo "You are adding the following <b>new</b> record to the top level inventory:"
	echo " <ul>"
	echo " <br><li>project = $POSTDATA[1]"
	echo " <br><li>platform = $POSTDATA[2]"
	echo " <br><li>year_p_start = $POSTDATA[3]"
	echo " <br><li>month_p_start = $POSTDATA[4]"
	echo " <br><li>day_p_start = $POSTDATA[5]"
	echo " <br><li>year_p_end = $POSTDATA[6]"
	echo " <br><li>month_p_end = $POSTDATA[7]"
	echo " <br><li>day_p_end = $POSTDATA[8]"
	echo " <br><li>name_prin = $POSTDATA[9]"
	echo " <br><li>brief_desc = $POSTDATA[10]"
	echo " </ul>"
	set dolist=no
	set datafile="$POSTDATA[1].dat"
##	echo "<p>**debug, datafile = $datafile"
	if(-e inventoryopt) \rm inventoryopt
	echo "$POSTDATA[1-10]	$datafile" > inventoryopt
	$script_loc/addinventory-level0
else
	set datafile="$POSTDATA[1].dat"
#	echo "<p>**debug, datafile = $datafile"
	if(-e inventoryopt) \rm inventoryopt
#	echo "<p>**DEBUG existing=$existing"
	echo "$existing[1-10]	$datafile" > inventoryopt
	$script_loc/updateinv-level0
#	echo "<p>**DEBUG called updateinv-level0"
endif

# Do updating of existing record
##echo "<p>**debug - after set EXISTING"
##echo "<p>**debug EXISTING = ${EXISTING} <p>"

if($dolist == "yes") then
	set EXISTING="`grep $POSTDATA[1] ${inventorytop} | head -1 | tr ' ' '\012' | tr '\011' '\012' `"
	echo "<p>You are updating an existing inventory record with "
	echo "the following values:"
	echo " <ul>"
	echo " <br><li>project = $EXISTING[1]"
	echo " <br><li>platform = $EXISTING[2]"
	echo " <br><li>year_p_start = $EXISTING[3]"
	echo " <br><li>month_p_start = $EXISTING[4]"
	echo " <br><li>day_p_start = $EXISTING[5]"
	echo " <br><li>year_p_end = $EXISTING[6]"
	echo " <br><li>month_p_end = $EXISTING[7]"
	echo " <br><li>day_p_end = $EXISTING[8]"
	echo " <br><li>name_prin = $EXISTING[9]"
	echo " <br><li>brief_desc = $EXISTING[10]"
	echo " </ul>"
	set datafile="$EXISTING[1].dat"
##	echo "<p>**debug, datafile = $datafile"
endif

#Create the so called level 2 (really level 1) file if necessary
if( -e ${datatop}/{$datafile} ) then
	echo "<p> The ${datatop}/${datafile} level 1 data file "
	echo "already exists. "
	echo "The existing file will be used."
else
	cp ${datatop}/level2-stub.dat ${datatop}/${datafile}
	echo "<p> Successfully created the ${datatop}/${datafile} "
	echo "level 1 data file stub."
endif

echo "<p>Fill in the form below to add additional "
echo "information about the $POSTDATA[1] project.<p>"
set dolist=no

echo "<p><hr>Check off the appropriate data type(s) and select an "
echo "investigator name.  <p>"

echo "When your are satisfied with your choices, "

echo "click on <form action="\""/$scriptcgi/doupdate_get_si "\"" "
echo "method="\""POST"\"">"

	echo "<input type="\""submit"\""value="\""Next step: add "
	echo "si names"\""><p>"

echo "For the project: <input name="\""project"\"" size="\""15"\"
echo "value="\""$POSTDATA[1]"\""> "
echo "<p>"

#Read list of instruments from file, construct checkboxes for each.
$script_loc/listvalidinst
if ( ! -e $instrument_loc) then
	echo " Could not open list of instruments from $instrument_loc"
endif

set instlist=`$script_loc/squish $instrument_loc`

echo "Select all data types that apply: "
echo "<select multiple name="\""data_type"\"">"

foreach type ($instlist)
	echo "<option> $type"
end

echo "</select>"
echo "</form>"
exit 0
