#!/bin/csh -f

# /bin/csh -xv

# doupate_get_si  Update the U.S. GLOBEC Georges Bank Inventory files
# get the si names for each data type specified

# Edited March 17 1998 Change /data to /data5 & httpd.alt reference. rcg
# December 16, 1996 rcg

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

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

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

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"

set sinames="/data/rgroman/Inventory/sinames"
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`)
#echo " <p>**DEBUG POSTDATA = $POSTDATA"
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}`"
set dolist=yes

if($#existing == 0) then

#	Do update of new record
###	set curopt="`cat inventoryopt | tr ' ' '\011' `"
	echo "You are addinga <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
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."
#	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  "
echo "information about the $POSTDATA[1] project.<p>"
set dolist=no

echo "<p><hr>Select an investigator name for each data type listed.  <p>"

echo "When your are satisfied with your choices, "

echo "click on <form action="\""/$scriptcgi/doupdate-level1 "\"" "
echo "method="\""POST"\"">"

	echo "<input type="\""submit"\""value="\""Add to Level 1"\""><p>"

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

foreach type ($POSTDATA[2-])
	echo "<p>"
	echo "For data type <input name="\""${type}"\"" "
	echo "size="\""15"\"
	echo "value="\""$type"\""> "

	echo " ... ... SI name: <select name="\""${type}_SI"\"">"
	$script_loc/nawkscr < $sinames
	echo "</select>"

end
echo "</form>"
exit 0
