#!/bin/sh
#
# deal with sessions that have been previously disconnected but which are stuck with _connect-disconnect set
#
# $1 = pdbname
# $2 = usr#
# $3 = PID (if local)

. #ENVPT3#/bin/protopenv

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

PROTRACE=${PROTRACE-/home}						# where to look for protrace files
									# define in bin/localenv if necessary
XPID=${3-remote}
SCRATCH=${TMPDIR}/disconnectx.tmp

echo "" >>  ${SCRATCH}
echo "Disconnecting user ${2} from ${1}" >> ${SCRATCH}

if [ ! -z "${3}" ]
then
	echo "\$DLC/bin/proGetStack ${3}" >> ${SCRATCH}
	proGetStack ${XPID} >> ${SCRATCH}
fi

proshut ${1} -C disconnect ${2} >> ${SCRATCH}
sleep 5

if [ -z "${3}" ]
then
	echo "Not a local session, no stack trace available" >> ${SCRATCH}
else

	echo "Process ID ${XPID} appears to be stuck.  Please check manually."

	find ${PROTRACE} -name "protrace.${XPID}" -exec cat {} \; >> ${SCRATCH}

fi

# local extensions go in this script -- if disconnx.local exists, run it
#
# $1 = pdbname
# $2 = usr#
# $3 = PID (if local)

if [ -f ${PROTOP}/bin/disconnx.local ]
then
        ${PROTOP}/bin/disconnx.local ${1} ${2} ${3}
fi
