#!/bin/sh
# @(#)S97osimcs	1.18 11/24/99 SMI
#
# Copyright 11/24/99 Sun Microsystems, Inc. All Rights Reserved
#
# Start/Stop the CMIP 8.2 osimcsd process
#
EXE=osimcsd                   # Application to be started/stopped 
ECHO=/bin/echo
PROG=`basename $0`
#

trap "$ECHO Interrupted ; exit 1" INT QUIT

killproc() {        # kill the named process(es)
    pid=`/usr/bin/ps -ef |
         /usr/bin/grep $1 |
	/usr/bin/grep -v grep |
	/usr/bin/awk '{print $2}'`
    [ "$pid" != "" ] && kill $pid
}

#	Check whether file system(s) is/ are available 

if [ ! -d /usr/bin ]
then			
	echo "$0: /usr not mounted ?"
	exit 1
fi

#	mkdir /tmp/mbx, in case osilogd hasn't done it already

if [ ! -d /tmp/mbx ]
then
	mkdir /tmp/mbx
	chgrp sys /tmp/mbx
fi
chmod 777 /tmp/mbx

# NOTICE: IF YOU HAVE ONE OF THE FOLLOWING MESSAGES
#		Can't find <osistart> 
#		Can't find <$EXE>      
#         IT MEANS THAT THE EXECUTABLES ARE NOT IN THE 
#	  PATH.  
#	  IN THIS CASE SET THE VARIABLE <OSIPATH> TO THE
#	  APPROPRIATE DIRECTORY.
#	  IF you leave OSIPATH undefined (null string),
#	  the script will use the default value.
OSIPATH=""

#	Set PATH 

EXEPKG=SUNWomgtb	# pkg that install the executables
if [ -z "$OSIPATH" ]
then
	# if the pkg is installed, ask it for the executables
	BASEDIR=`/bin/pkgparam $EXEPKG BASEDIR 2>/dev/null`
	if [ -z "$BASEDIR" ]	# pkg not installed
	then
		BASEDIR=/opt	# set defaul. Maybe the dir is nfs mounted
	fi
	OSIPATH=$BASEDIR/SUNWconn/lib
fi
PATH="/usr/bin:/bin:/usr/sbin:$OSIPATH:" ; export PATH 

if [ ! -d $OSIPATH ]
then			
	$ECHO "$PROG: $OSIPATH not mounted ?"
	exit 1
fi

USAGE="Usage: $PROG {start | stop }"

#lets initialize some variable used inside osimcs for specifics customers

OSIVAR=`find /etc/opt/SUNWconn/cmip -user root -perm 0744 -name cmip_var`

if [ x$OSIVAR != x ]
then
  . $OSIVAR
fi

case "$1" in
'start')
    if [ x$DUMP_OSIMCS_INTERNAL = xYES ]
    then
	#DEBUG / INTERNAL TRACES ACTIVATED ===> change the osimcsd to be used
	OSIPATH=$OSIPATH/../cmip/lib
	EXE=osimcsd.dbg
    fi

    if [ ! -f $OSIPATH/$EXE ]
    then
    	$ECHO "$PROG: Can't find <$OSIPATH/$EXE>" 
    	exit 1
    fi

    if [ ! -x $OSIPATH/$EXE ]
    then
    	$ECHO "$PROG: Can't execute <$OSIPATH/$EXE>" 
    	exit 1
    fi
    $OSIPATH/$EXE &
    sleep 10		# We wait some time to be sure osimcs is ready to 
    			# receive bind before giving back hand

    ;;   

'stop')
	killproc $EXE
	;;  

*)
	$ECHO $USAGE
	exit 1
	;;
esac


exit 0
