#!/bin/sh

. #ENVPT3#/bin/protopenv

TMPDIR=${TMPDIR-/tmp}
LOGDIR=${LOGDIR-/tmp}

MNAME="protop"

cd ${PROTOP}

if [ ! -d ${TMPDIR} ]
then
        mkdir ${TMPDIR}
fi

if [ ! -d ${LOGDIR} ]
then
        mkdir ${LOGDIR}
fi

errmsg()
{
	echo ""
	echo "${1}"
	echo ""
}

findPF()
{
	if [ -f ${1} ]
	then
		echo " -pf ${1}"
	fi
}

# save ${1} and shift the command args so that we can easily pass along $2 etc
#
# FRNAME is a "friendly name" in etc/dblist.cfg
#

FRNAME=${1}

if [ -z "${FRNAME}" ]
then
	errmsg "Please specify a friendly name in etc/dblist.cfg."
	exit
fi

shift
DBPATH=
INITDC=

# for a list of supported data collector "event names": grep -i "<evtName>" ${PROTOP}/ptdefs/tt_protop.xml

while [ ! -z "${1}" ]
do

	grep -i "<evtName>${1}" ${PROTOP}/ptdefs/tt_protop.xml > /dev/null 2>&1

	if [ $? != 0 ]
	then
		echo "Unknown data collector: ${1}"
		echo "<enter> to continue"
		read something
	else
		if [ -z "${INITDC}" ]
		then
			INITDC="${1}"
		else
			INITDC="${INITDC},${1}"
		fi
	fi

	shift

done

# echo ${INITDC}

getDBPATH ${FRNAME}

CUSTID=`cat etc/custid.cfg`

# echo "DBPATH = " ${DBPATH}
# read cmd

# if DBPATH has a value then we found a friendly name and the path is in DBPATH
#

if [ ! -z "${DBPATH}" ]					# friendly name!
then
	PARAM='-param '"${FRNAME}"			# pass the friendly name into protop.p
else
	errmsg "Please specify a friendly name in etc/dblist.cfg."
	exit
fi

if [ -f ${DBPATH}.db ] || [ "${DBPATH}" = "-1" ]
then

	if [ ! -z "${INITDC}" ]
	then
		PARAM="${PARAM}|${INITDC}"
	fi

	PFLIST=`findPF "etc/protop.pf"`
	PFLIST="$PFLIST`findPF "etc/${CUSTID}.pf"`"
#	PFLIST="$PFLIST`findPF "etc/${SERVER}.pf"`"		# SERVER variable is not currently implemented
	PFLIST="$PFLIST`findPF "etc/${RESRCTYPE}.pf"`"		# RESRCTYPE variable is implemented in getDBPATH
	PFLIST="$PFLIST`findPF "etc/${FRNAME}.pf"`"

	${DLC}/bin/_progres -p ${MNAME}.p ${DBPATH} ${PFLIST} ${PARAM} -T ${TMPDIR} -debugalert -errorstack -clientlog ${TMPDIR}/pt3.${FRNAME}.debug

else

	errmsg "${DBPATH}.db does not exist."

fi
