#!/bin/csh -f

# /bin/csh -xv

# doupate-level1 Update the U.S. GLOBEC Georges Bank Inventory level 1 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, "
#echo "continued</title>"
#echo "<h2 align=center>Update to U.S. GLOBEC Georges Bank Inventory, "
#echo "<br>level 1, continued</h2>"

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

set datadir = /data/rgroman/Inventory
#set datadir = /data/rgroman/Inventory/debug
#echo "<p> *** Note: DEBUG mode in doupdate-level1, datadir = $datadir <p>"

set inventorytop=$datadir/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`)
#echo "<p>**debug POSTDATA = $POSTDATA <p>"

set numb_words = ${#POSTDATA}
#echo "<p>**debug numb_words = $numb_words <p>"

setenv TMP `date '+%Y.%m.%d'`
#echo "<p> **debug TMP = $TMP <p>"

set current_project=$POSTDATA[1]
#echo "<p>**DEBUG current_project = $current_project <p>"

#set existing="`grep $current_project ${inventorytop}`"
#echo "<p>**debug existing = $existing <p>"

#For each data type, add data line in level 2 file, create data 
#file at level 3, make entries in level 3 file 

if ($#POSTDATA == 1) then
	echo "Content-type: text/html"
	echo ""
	echo "<p>You have not specified any data types to add or modify."
	echo "Ending inventory update procedure."
	exit 0
endif

#echo "<p> **DEBUG before call to addinventory-level1 routine"
$script_loc/addinventory-level1 $POSTDATA

#echo "<p> **DEBUG after call to addinventory-level1 routine"

#echo "<p> **DEBUG current_project = $current_project"
set record = $current_project
#echo "<p> **DEBUG record = $record"
@ counter = 1

foreach datatype ($POSTDATA[2-])
#	echo " <p>**DEBUG datatype = $datatype"
	@ counter = $counter + 1
	@ skip=$counter % 2
#	echo " <p>**DEBUG counter = $counter and skip = $skip"
	if ( $skip == 1) continue
	set record=("$record $datatype")
#	echo " <p> **DEBUG record = $record"
end
set newrecord = (`echo $record | tr ' ' '\012' `)
#echo " <p>Data records in Inventory level 1 for data types<br>"
#echo " $newrecord[2-] <br>have been added"
#echo " <p><hr>"
if(-e inventoryopt1) \rm inventoryopt1
echo $newrecord > inventoryopt1

#echo "<p> **DEBUG before call to doupdate_status routine"
#echo "<p>When you are ready to continue, "
#echo "click on <form action="\""/$scriptcgi/doupdate_status "\"" 
#echo "method="\""POST"\"">"
#echo "<input type="\""submit"\""value="\""Continue inventory update"\""><p>"

$script_loc/doupdate_status
exit 0
