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

# Adds new status entries to level 2 inventory file.

# R. Groman February 20, 1997

# Passed parameters are:
#	1 == project
#	2 == datatype
#	3 - end == status and desciption, in pairs

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

set backup=/data/rgroman/Inventory/backup

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

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

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

set numbarg=$#argv
#echo " <p>**DEBUG argv = $argv"
#echo " <p>**DEBUG numbarg= $numbarg"

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 > /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 extract out field names"

@ counter = 3
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"

	
	#Add new record to level 1 (must replace spaces with tabs first)
	set record="${statustype} $year $month $day ${description}"
	#echo " <p>**DEBUG record = $record"
	echo $record  | tr '\040' '\011' >>/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
