#!/bin/sh

#******************************************************************
# Copyright  :  Smaser AG
# Filename   : 	glbupd
# Purpose    :  Call $TDBS_EXE/glbupdit to update records
# Parameters :	$1 = database
#		$2 = GIF file
#		$3 = Scratch directory  (optional)
#		$4 = Logfile            (optional)
#*******************************************************************

pid=$$

if [ -f /usr/bin/dirname ] ; then
	dirname=/usr/bin/dirname
else
	dirname=dirname
fi

if [ -f /usr/bin/awk ] ; then
	awk=/usr/bin/awk
else
	awk=awk
fi

if [ -f `$dirname $0`/queryrcs ] ; then
	queryrcs=`$dirname $0`/queryrcs
else
	queryrcs=queryrcs
fi

bin=`$queryrcs TDBS_EXE | $awk -F\" '{print $2}'`
if [ "$bin" = "" -o "$bin" = "<nil>" ]
then
	echo Invalid TRIP installation - cannot locate 'bin' directory
	exit 1
fi

tbin=`echo $bin | $awk '{print $2}'`
if [ "$tbin" != "" ]
then
	echo Invalid TRIP installation - ambiguous definition of TDBS_EXE
	exit 1
fi

echo $2 | tr -s ' ' '\012' | $bin/glbupdit
tdbstat=$?

if [ $tdbstat -ne 1 ]
then
	echo "Error during Global Update to database $1." > $HOME/glbtmp$pid
	$bin/err_mail $HOME/glbtmp$pid $4
	exit 1
fi

noix=`$queryrcs TDBS_NO_GLBUPD_INDEX | $awk -F\" '{print $2}'`
if [ "$noix" = "<nil>" ]
then
	$bin/index $1 "" N "$4"
	tdbstat=$?
else
	# Only running load, which if we get here has succeeded.
	tdbstat=0
fi

exit $tdbstat
