#!/bin/sh
#
# protop friendlyName [dcName1 [dcName2 ... ]]
#

. #ENVPT3#/bin/protopenv

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

cd ${PROTOP}

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

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

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

# save ${1} and shift the command args so that we can easily pass along $2 etc
#
# P1 is either:
#
#   - a "friendly name" in etc/dblist.cfg
#   - the path to a database
#
# the old "$1 is a pf file pointing to a database" will no longer work
#

P1=${1}

if [ -z "${P1}" ]
then
	errmsg "Please specify a friendly name in etc/dblist.cfg or a full database pathname."
	errmsg "${0} friendlyName"
        errmsg "${0} /path/to/db/dbname"
	exit
fi

shift
XDB=
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

# chain togther .pf files if they exist
#
# 	etc/protop.pf
# 	etc/custId.pf
# 	etc/serverName.pf
# 	etc/type.pf
# 	etc/friendlyName.pf
#

if [ -f etc/protop.pf ]
then
  PFLIST="${PFLIST} -pf etc/protop.pf"
fi

read CUSTID < etc/custid.cfg
if [ -f etc/${CUSTID}.pf ]
then
	PFLIST="${PFLIST} -pf etc/${CUSTID}.pf"
fi

if [ -z "${PTSERVER}" ]
then
	PTSERVERNAME=`uname -a | awk '{print $2}'`
else
	PTSERVERNAME=${PTSERVER}
fi

if [ -f etc/${PTSERVERNAME}.pf ]
then
	PFLIST="${PFLIST} -pf etc/${PTSERVERNAME}.pf"
fi

if [ -f ${PROTOP}/etc/dblist.cfg ]
then
	XTYPE=`grep "^${P1}|" ${PROTOP}/etc/dblist.cfg | awk -F"|" '{print $6}' 2>/dev/null`
	if [ ! -z "${XTYPE}" ]
	then
		if [ -f etc/${XTYPE}.pf ]
		then
			PFLIST="${PFLIST} -pf etc/${XTYPE}.pf"
		fi
	fi
fi

if [ -f ${PROTOP}/etc/${P1}.pf ]
then
	PFLIST="${PFLIST} -pf etc/${P1}.pf"
fi


###


if [ -f ${PROTOP}/etc/dblist.cfg ]	# search using "pipe" as the delimiter
then
	XDB=`grep "^${P1}|" ${PROTOP}/etc/dblist.cfg | awk -F"|" '{print $2}' 2>/dev/null`
fi

PX=`basename ${P1} .db`

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

if [ ! -z "${XDB}" ]					# is there a friendly name?
then

	PARAM='-param '"${P1}"				# pass the friendly name into protop.p

else

	XDB=${P1}					# take ${P1} as the path name
	PARAM='-param '"${PX}"				# pass the db base name as the friendly name into protop.p

fi

# printf '\033[8;72;160t'				# set terminal window to 72 lines, 160 columns


if [ -f ${XDB}.db ] || [ "${XDB}" = "-1" ]
then
	if [ ! -z "${INITDC}" ]
	then
		PARAM="${PARAM}|${INITDC}"
	fi

	## echo ${DLC}/bin/_progres -p protop.p ${XDB} ${PARAM} ${PFLIST} -T ${TMPDIR} $*
	## read xyzzy

	${DLC}/bin/_progres -p protop.p ${XDB} ${PARAM} ${PFLIST} -T ${TMPDIR} -errorstack -clientlog ${TMPDIR}/protop.${PX}.debug

else
	errmsg "${XDB}.db does not exist."
fi
