#!/bin/csh -f

# /bin/csh -xv

# dodelete  Delete data from 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>Delete data from the U.S. GLOBEC Georges Bank Inventory, "
echo " specify levels </title>"
echo "<h2 align=center>Delete data from the U.S. GLOBEC Georges Bank Inventory,"
echo "<br>specify levels</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

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

set existing=(`grep $POSTDATA[1] ${inventorytop}`)
#echo " <p>**DEBUG POSTDATA[1]=$POSTDATA[1]"
#echo " <p>**DEBUG existing=$existing"
#echo " <p>**DEBUG numb of elements in existing=$#existing"

if(! $#existing == 0) then

#	Display data record

	echo "<p>You will delete information about the following project"
	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="$datatop/$existing[1].dat"
##	echo "<p>**debug, datafile = $datafile"
	if(-e inventoryopt) \rm inventoryopt
	echo "$existing[1-10]	$datafile" > inventoryopt
	
else
	echo "<h3>There are no data for project $POSTDATA[1] in the inventory."
	echo "Nothing can be deleted.  Click on the <i>Back</i> button"
	echo "to retype the project name to delete.</h3>"
	exit 0  
endif

#

echo "<form action="\""/$scriptcgi/delinv_data"\"" "
echo "method="\""POST"\"">"

#Need to read level 1 data file and display data types it contains.
if ( -e /tmp/temp.dodelete) \rm /tmp/temp.dodelete
if (! -e $datafile) then
	echo " <p>The data file for project $POSTDATA[1] called "
	echo " $datafile does not exist.  But I can continue processing."
	set data_types=" "
else
	grep -v "#" $datafile | tail +3 > /tmp/temp.dodelete
	set data_types=(`awk '{print $1}' /tmp/temp.dodelete`)
endif

#echo " <p>**DEBUG data_types=$data_types"
echo " <p><h3>Check off each data inventory element to delete, then click here</h3>"
	echo "<input type="\""submit"\""value="\""Delete specified "
	echo "data"\""><p>"

echo "<p>"
echo "<input name="\""project"\"" "
echo "size="\""15"\"
	echo "value="\""${POSTDATA[1]}"\""> "

	echo " Delete? <select name="\""project_del"\"" >"
	echo "<option>Yes"
	echo "<option selected>No"
	echo "</select>"
	echo "<p>Note: if you delete the project, then all data elements"
	echo "relating to this project are also deleted <b>whether or not</b>"
	echo "you check off the individual data types."

if ( $data_types != " " ) then
  foreach type ($data_types)
	echo "<p>"
	echo "<input name="\""${type}"\"" "
	echo "size="\""15"\"
	echo "value="\""$type"\""> "

	echo " Delete? <select name="\""${type}_del"\"" >"
	echo "<option>Yes"
	echo "<option selected>No"
	echo "</select>"

  end
endif
echo "<p><input type="\""reset"\""value="\""Reset form"\""><p>"

echo "</form>"
exit 0
