#!/bin/sh
#
#ident  "@(#)esm_em_mcboot 1.5    99/10/07 SMI"
#
# Copyright (c) 1999 by Sun Microsystems, Inc.
# All rights reserved.
#
# This script is Component Manager specific. It starts the loading process
# for management classes.
#
TEXTDOMAIN=SUNW_SMS_CM
PATH=/usr/bin:/usr/sbin:/sbin:/usr/ucb/bin
PKGINFO=pkginfo
cwd=`pwd`

progname=$0
OPTION=$1

LOCATION=`${PKGINFO} -r SUNWesm`
if [ "$LOCATION" != "" ]
then
    LOCATION=${LOCATION}/SUNWesm/sbin
else
    LOCATION=/opt/SUNWesm/sbin
fi

ESM_MCBOOT=$LOCATION/esm_mcboot

USAGE="Usage: $0 {start | status}"

do_check()
{
	${ESM_MCBOOT} status
	if [ $? -eq 1 ]; then
		echo "StorEdge MCStation is not running, start failed"
		exit 1
	fi
}

set_pid_location()
{
	PID_DIR=/tmp/SUNWencc
	PID_EM=_esm_em_mcboot.pid

	if [ ! -d $PID_DIR ]
	then
	    mkdir -p $PID_DIR > /dev/null 2>&1
	    if [ $? -ne 0 ]
		then
                echo "$progname: \c" >&2
                gettext $TEXTDOMAIN "Unable to locate Process ID directory" >&2
                echo " $PID_DIR" >&2
                echo >&2
                PID_LOCATION=/tmp/$PID_EM
            else
                PID_LOCATION=$PID_DIR/$PID_EM
            fi
        else
            PID_LOCATION=$PID_DIR/$PID_EM
        fi
}

set_lock_location()
{
        # Caution: changing the lock directory or the name will affect the
        #               functioning of _esm_em_mcboot. Change the lock name
        #               and directory in that script accordingly.
	LOCK_DIR=/tmp/SUNWencc
	LOCK=_esm_em_mcboot.lck

	if [ ! -d $LOCK_DIR ]
	then
	    mkdir -p $LOCK_DIR > /dev/null 2>&1
	    if [ $? -ne 0 ]
                then
                echo "$progname: \c" >&2
                gettext $TEXTDOMAIN "Unable to locate Process lock directory" >&2
		echo " $LOCK_DIR" >&2
		echo >&2
		LOCK_LOCATION=/tmp/$LOCK
	    else
		LOCK_LOCATION=$LOCK_DIR/$LOCK
	    fi
	else
	    LOCK_LOCATION=$LOCK_DIR/$LOCK
	fi
}

do_start()
{
	set_pid_location
	set_lock_location

	COMMAND="$LOCATION/_esm_em_mcboot start"

	eval "$COMMAND &"
	echo $! > $PID_LOCATION
	sleep 1
	
	# Wait for lock file to be cleared by _esm_em_mcboot
	wait_lock
}

wait_lock() {

    # How many times to try for lock clearing
    LOCK_TRIES=1200
 
    # Time to wait
    TRY_TIME=10
    
    # current try
    try=0
 
    while [ $try -le $LOCK_TRIES ]
    do
        if [ ! -f $LOCK_LOCATION ]
        then
            # success
            return
        fi
 
	echo ".\c"
        sleep $TRY_TIME
        try=`expr $try + 1`
 
    done
    
    # If you get here, the load and start process is just taking too long.
    # Report and let it run in the background.
    sleep 1
    echo "$progname: \c" >&2
    gettext $TEXTDOMAIN "Loading process is taking too long... put MCBoot process in background..." >&2
    echo "" >&2
    exit 4
}

killproc ()
{
    pid=`/usr/bin/ps -e -o pid -o args | /usr/bin/grep $1 |
         grep -v grep | cut -f1 -d' '`
    [ "$pid" != "" ] && kill $pid
}

do_stop ()
{
    if [ x$PID_LOCATION != x ]
    then
        if [ -f $PID_LOCATION ]
        then
            PID=`cat $PID_LOCATION`
            rm $PID_LOCATION > /dev/null 2>&1
            kill -0 $PID > /dev/null 2>&1
            if [ $? -ne 0 ]
            then
                echo "$progname: \c" >&2
                gettext $TEXTDOMAIN "Notice: Unable to locate process id" >&2
                echo " $PID" >&2
                rm $PID_LOCATION > /dev/null 2>&1
                return 1
            fi
 
            kill -TERM $PID -$PID > /dev/null 2>&1
	    sleep 1
	    kill -0 $PID > /dev/null 2>&1
            if [ $? -eq 0 ]
            then
                kill -HUP $PID -$PID > /dev/null 2>&1
		sleep 1
		kill -0 $PID > /dev/null 2>&1
                if [ $? -eq 0 ]
                then
                    kill -KILL $PID -$PID > /dev/null 2>&1
		    kill -0 $PID > /dev/null 2>&1
		    sleep 1
                    if [ $? -eq 0 ]
                    then
                        echo "$progname: \c" >&2
                        gettext $TEXTDOMAIN "Notice: Unable to kill process id"
>&2
                        echo " $PID" >&2
                    fi
                fi # -HUP
            fi # -TERM
 
        else
            killproc com.sun.esm.apps.AppBoot
        fi
    else
        killproc com.sun.esm.apps.AppBoot
    fi
    #$JAVA_HOME $JAVA_CLASSPATH com.sun.esm.apps.AppBoot -stop &
}

case x$OPTION in 
    'xstart') 
	      echo "Start loading Component Manager specific management classes..."
	      do_check;
	      do_start 
	      echo "Done loading Component Manager specific management classes..."
              ;;
    'xstatus') 
              [ -f $LOCATION/esm_moboot ] && $LOCATION/esm_mcboot status
              ;;
              
    *)        
	      echo "usage: $progname { start | status }"
	      ;;
esac

exit 0
