#!/bin/sh
#
# (C) COPYRIGHT TIVOLI Systems, Inc. 1994
# Unpublished Work
# All Rights Reserved
# Licensed Material - Property of TIVOLI Systems, Inc.
#
# Component Name: TME 10 LCF
#
# $Date: 1997/02/25 16:09:47 $
#
# $Revision: 1.17 $
#
# Primary Author: John M. Adams

# This script is for starting lcfd. Its primary job is to set the
# proper variable for locating shared libraries at load time. There
# are no relative directory dependencies.
#
# Required inputs are LCFROOT, INTERP
#
# This script is designed for 3 different invocation contexts:
# 1) LCF has just been installed but lcfd has not yet run. In this
#    context, lcfd.sh will run in $LCFROOT.
# 2) At system boot time, invoked from a startup file. In this
#    context, this script will run in $LCFROOT/dat/<x>, where x is
#    a TMR unique identifier assigned when lcfd runs for the
#    first time. This naming convention is employed to ensure
#    cache directory uniqueness when multiple hosts share LCFROOT
#    via NFS or another network file system.
# 3) Used to start lcfd by hand.
#
# An edited version of this script is written to the run directory for each
# lcf instance. This hardcodes an appropriate default environment. The resulting
# script is suitable for users wishing to start the lcfd by hand.

PATH=/bin:/usr/bin:/usr/ucb:/usr/sbin:$PATH

# At install time the first two variables come in through the environment.
# When installation is complete, substitutions have been made on @var@'s
# so that environment is not required from then on.

if [ -n "$DEBUG" ]; then
  set -x
fi

INTERP=${INTERP:-solaris2}
LCFROOT=${LCFROOT:-/dnb/prosess/Tivoli/lcf}
LCF_DATDIR=${LCF_DATDIR:-/dnb/prosess/Tivoli/lcf/dat/1}
LD_TIVOLI=$LCFROOT/lib/$INTERP
export PATH INTERP LCFROOT
LCFD=lcfd

DEVNULL=/dev/null
if [ $INTERP = "w32-ix86" -o $INTERP = "w32-axp" ]; then
  LCFD=$LCFD.exe
  DEVNULL=nul
fi
if [ $INTERP = "os2-ix86" ]; then
  LCFD=$LCFD.exe
  DEVNULL=/dev/nul
fi

case "$INTERP" in
hpux9|hpux10)
    SHLIB_PATH=$PWD:$LD_TIVOLI:/usr/lib:/usr/lib/X11R5${SHLIB_PATH:+":$SHLIB_PATH"}
    export SHLIB_PATH;;
aix3-r2|aix4-r1)
    LIBPATH=$PWD:$LD_TIVOLI:/usr/lib${LIBPATH:+":$LIBPATH"}
    export LIBPATH;;
solaris2)
    LD_LIBRARY_PATH=$PWD:$LD_TIVOLI:/usr/openwin/lib:/usr/lib:/usr/ucblib${LD_LIBRARY_PATH:+":$LD_LIBRARY_PATH"}
    export LD_LIBRARY_PATH;;
sunos4)
    LD_LIBRARY_PATH=$PWD:$LD_TIVOLI:/usr/lib:/usr/ucblib${LD_LIBRARY_PATH:+":$LD_LIBRARY_PATH"}
    export LD_LIBRARY_PATH;;
w32-ix86|w32-axp)
    PATH="$LD_TIVOLI;$LCFROOT/bin/mrt;$PATH"
    export PATH;;
os2-ix86)
    PATH="$LD_TIVOLI;$LCFROOT/bin/mrt;$PATH"
    export PATH;;
mips-irix5)
    LD_LIBRARY_PATH=$PWD:$LD_TIVOLI:/usr/lib${LD_LIBRARY_PATH:+":$LD_LIBRARY_PATH"}
    export LD_LIBRARY_PATH;;
solaris2-ix86)
    LD_LIBRARY_PATH=$PWD:$LD_TIVOLI:/usr/openwin/lib:/usr/lib:/usr/ucblib${LD_LIBRARY_PATH:+":$LD_LIBRARY_PATH"}
    export LD_LIBRARY_PATH;;
osf-axp)
    LD_LIBRARY_PATH=$PWD:$LD_TIVOLI:/usr/lib${LD_LIBRARY_PATH:+":$LD_LIBRARY_PATH"}
    export LD_LIBRARY_PATH;;
openstep4-ix86)
    DYLD_LIBRARY_PATH=$PWD:$LD_TIVOLI:/usr/lib${DYLD_LIBRARY_PATH:+":$DYLD_LIBRARY_PATH"}
    export DYLD_LIBRARY_PATH;;
linux-ix86)
    LD_LIBRARY_PATH=$PWD:$LD_TIVOLI:/usr/lib${LD_LIBRARY_PATH:+":$LD_LIBRARY_PATH"}
    export LD_LIBRARY_PATH;;
linux-s390)
    LD_LIBRARY_PATH=$PWD:$LD_TIVOLI:/usr/lib${LD_LIBRARY_PATH:+":$LD_LIBRARY_PATH"}
    export LD_LIBRARY_PATH;;
*)
    LD_LIBRARY_PATH=$PWD:$LD_TIVOLI:/usr/lib:/usr/ucblib${LD_LIBRARY_PATH:+":$LD_LIBRARY_PATH"}
    export LD_LIBRARY_PATH;;
esac

[ -d "$LCF_DATDIR" ] && cd $LCF_DATDIR

case "$1" in
	install)
		shift
		case "$LCFOPTS" in *lib_dir*)
			;;
		*)
			LCFOPTS="$LCFOPTS -Dlib_dir=$LCFROOT/lib/$INTERP"
			;;
		esac
		case "$LCFOPTS" in *load_dir*)
			;;
		*)
			LCFOPTS="$LCFOPTS -Dload_dir=$LCFROOT/bin/$INTERP/mrt"
			;;
		esac

                if [ $INTERP != "os2-ix86" ]; then
		   exec $LCFROOT/bin/$INTERP/mrt/$LCFD $LCFOPTS $@
                else
# For OS2, start lcfd in another session so this shell script doesn't hang rexecd 
# The /i option on START enables lcfd to inherit the environment from config.sys
#      so stdin and stdout is piped to the new session and not the rexecd session.
#                   echo "OS2 debug: comment out start for testing ..."
 		   cmd /c start /i `echo $LCFROOT/bin/$INTERP/mrt/$LCFD | sed "s/\//\\\\\\/g"` -C$LCF_DATDIR $LCFOPTS $@
                fi 
		;;
	stop_msg)
		shift
		echo "Stopping the Tivoli LCF daemon"
		;;
	stop)
		shift
		if [ "$INTERP" = "linux-ix86" -o "$INTERP" = "linux-s390" ]; then
			instance=`basename $LCF_DATDIR`
			if [ -d /var/lock/subsys ]; then
				rm -f /var/lock/subsys/lcfd$instance.rc
			fi
		fi
		if [ -f $LCF_DATDIR/lcfd.pid ] ; then
			pid=`sed -n 1p $LCF_DATDIR/lcfd.pid`
			if [ -n "$pid" ] ; then
				if [ $INTERP = "sunos4" -o $INTERP = "openstep4-ix86" ]; then
					pidlist=`ps -xc | grep lcfd | grep -v grep | awk '{print $1}'`
                                # On DGUX, PID is the 3rd field.
                                # Tier2 Change...Please do not Remove.
                                # Barry Sheehan 6/10/99
                                elif [ $INTERP = "dgux5-ix86" ] ; then
					pidlist=`ps -el | grep lcfd | grep -v grep | awk '{print $3}'`
                                # On DEC Unix, ps -el may return variable
                                # number of fields.  Using ps -ef instead.
                                # Tier2 Change...Please do not Remove.
                                # Barry Sheehan 6/10/99
                                elif [ $INTERP = "osf-axp" ] ; then
                                        pidlist=`ps -ef | grep lcfd | grep -v grep | awk '{print $2}'`
				else
					pidlist=`ps -el | grep lcfd | grep -v grep | awk '{print $4}'`
				fi

				for temppid in $pidlist
				do
					if [ $temppid -eq $pid ] ; then
						rm -f $LCF_DATDIR/lcfd.pid
						kill $pid
						exit $?
					fi
				done
				echo "The process id in $LCF_DATDIR/lcfd.pid is not an lcfd process id."
				echo "Unable to get process id of the Tivoli LCF daemon (pid)."
			else
				echo "Unable to get process id of the Tivoli LCF daemon (pid)."
				exit 1
			fi
		else
			echo "Unable to get process id of the Tivoli LCF daemon (lcfd.pid)."
			exit 1
		fi
		;;
	start_msg)
		shift
		echo "Starting the Tivoli LCF daemon"
		;;
	start|*)
		if [ "$1" = "start" ] ; then
			shift
		fi

		if [ "$INTERP" = "linux-ix86" -o "$INTERP" = "linux-s390" ]; then
			instance=`basename $LCF_DATDIR`
			if [ -d /var/lock/subsys ]; then
				touch /var/lock/subsys/lcfd$instance.rc
			fi
		fi

		if [ $INTERP != "os2-ix86" -a -f $LCF_DATDIR/lcfd.pid ] ; then
			pid=`sed -n 1p $LCF_DATDIR/lcfd.pid`
			if [ -n "$pid" ] ; then
				if [ $INTERP = "sunos4" -o $INTERP = "openstep4-ix86" ]; then
					pidlist=`ps -xc | grep lcfd | grep -v grep | awk '{print $1}'`
                                # On DGUX, PID is the 3rd field.
                                # Tier2 Change...Please do not Remove.
                                # Barry Sheehan 6/10/99
                                elif [ $INTERP = "dgux5-ix86" ] ; then
					pidlist=`ps -el | grep lcfd | grep -v grep | awk '{print $3}'`
                                # On DEC Unix, ps -el may return variable
                                # number of fields.  Using ps -ef instead.
                                # Tier2 Change...Please do not Remove.
                                # Barry Sheehan 6/10/99
                                elif [ $INTERP = "osf-axp" ] ; then
                                        pidlist=`ps -ef | grep lcfd | grep -v grep | awk '{print $2}'`
				else
					pidlist=`ps -el | grep lcfd | grep -v grep | awk '{print $4}'`
				fi

				for temppid in $pidlist
				do
					if [ $temppid -eq $pid ] ; then
						echo "lcfd for this environment already running under pid $pid"
						exit 1
					fi
				done
			fi
		fi

		if [ -f $LCF_DATDIR/cache/bin/$LCFD ] && 
		   [ -x $LCF_DATDIR/cache/bin/$LCFD ]; then
                   if [ $INTERP = "os2-ix86" ]; then
             	        cmd /c start /i `echo $LCF_DATDIR/cache/bin/$LCFD | sed "s/\//\\\\\\/g"` -C$LCF_DATDIR $LCFOPTS $@
                   else
			exec $LCF_DATDIR/cache/bin/$LCFD $LCFOPTS $@
                   fi
		else
                   if [ $INTERP = "os2-ix86" ]; then
 		        cmd /c start /i `echo $LCFROOT/bin/$INTERP/mrt/$LCFD | sed "s/\//\\\\\\/g"` -C$LCF_DATDIR $LCFOPTS $@
                   else
			exec $LCFROOT/bin/$INTERP/mrt/$LCFD $LCFOPTS $@
                   fi
		fi;;
esac
