#!/bin/sh

#*******************************************************************************
# Copyright   : Smaser AG
# Filename    : loadix
# Purpose     : Module to automate the loading and indexing of a database.
#*******************************************************************************

if [ "$1" = "--help" -o "$1" = "-h" ]; then
	echo "USAGE: loadix [OPTIONS]"
	echo "  -d database          Name of database to load and index"
	echo "  -f tform             TFORM file to load"
	echo "  -t tstamp            Timestamp (YYYY-MM-DD HH:MM:SS)"
	echo "  --[no-]analyse-text  Enable or disable scanning of paragraphs and sentences"
	echo "  --[no-]reindex       Index incrementally or perform reindex"
	echo "  --defaults           When possible, use default values without prompting"
	exit 0
fi

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

$bin/load $*
loadstat=$?

if [ $loadstat -eq 0 -o $loadstat -eq 1 -o $loadstat -eq 3 ]
then
	$bin/index $*
 	indexstat=$?
	if [ $indexstat -ne 0 ]; then
	    exit $indexstat
	else
	    exit $loadstat
	fi
fi

exit 1
