#!/usr/bin/ksh
#
# Copyright (c) 2002-2003 Sun Microsystems, Inc. All rights reserved
#
# @(#)esmcontrol	1.25 03/10/20
#
#  This is the main control script for ESM installer
#
PROGNAME=$0
#
# Resolve lib path from the installed system only.  
#

ESM_BASE=`/bin/pkgparam SUNWstm ESM_BASE 2> /dev/null`
if [ $? -ne 0 ]; then
    echo "$PROGNAME: ERROR: SUNWstm is not found. Please make"
    echo "           sure ESM has been installed correctly."
    exit 1 
fi
ESM_LIB=$ESM_BASE/lib

# source the libs
. $ESM_LIB/esm_globals.ksh
. $ESM_LIB/esm_lib.ksh

#
# globals/enviornment variables
#

LANG=C
export LANG

ESM_CONTEXT=control
export ESM_CONTEXT

_OVERALLSTATUS=$EL_PASS

_action=""
_comment=""
_management=0
_agent=0

#---------------------------------------
# Display the usage
# Arguments:
#    none
#---------------------------------------
l_usage ()
{
    echo "
INFO: usage: $PROGNAME [options]

where options are:

\tstart              start processes
\trestart            restart processes
\tstop               stop processes
\t-m|--management    management station
\t-a|--agent         agent station
\t-?|-h|--help       show this usage

NOTE: without -m or -a option, $PROGNAME will attempt 
      to start/stop/restart all installed station(s)

"
}

#---------------------------------------
# Parse the single character command line arguments
# Arguments:
#  $1 set of compound arguments
#---------------------------------------
l_getopts () {
    action=""
    while getopts :amh gopt
    do
        case $gopt in
            a)
            action=agent
        ;;
            m)
            action=management
        ;;
            *)
            action=help
        ;;
        esac
        l_optaction $action
    done
}

#---------------------------------------
# Set the action associated with a given option
# Arguments:
#  $1 action
#---------------------------------------
l_optaction ()
{
    case $1 in
        agent)
	    _agent=1
    ;;
        management)
	    _management=1
    ;;
        start)
            _action="start"
	    _comment="Starting"
    ;;
        stop)
            _action="stop"
	    _comment="Stopping"
    ;;
        restart)
            _action="restart"
    ;;
        *) l_usage
            exit 1
    ;;
    esac
}

#---------------------------------------
# Parse the esmcontrol CLI options
# Arguments:
#  $# all command line arguments
#---------------------------------------
l_cli_opts ()
{
    while [ $# -ne 0 ];
    do
        opt=$1
        # handle -- long command
        echo $opt | /bin/grep '\-\-' > /dev/null
        if [ $? -eq 0 ]; then
            l_optaction `echo $opt | tr -d '-'` 
        else
            # handle option without -
            echo $opt | /bin/grep '\-' > /dev/null
            if [ $? -eq 0 ]; then
                l_getopts $opt
            else
                l_optaction "$opt"
            fi
        fi
        shift
    done

    if [ -z "$_action" ]; then
        l_usage
        exit 1
    fi
}

#---------------------------------------
# Print a message block before exiting esmcontrol
# Arguments:
#    none
#---------------------------------------
l_exit_control ()
{
    el_esmecho 1 n ""

    if [ "${_OVERALLSTATUS}" = "$EL_PASS" ]; then
        msg="Successfully Completed"
        status=0
    else
        msg="Failed"
        status=1
    fi

    el_log 1 "Enterprise Storage Manager Version $ESM_VERSION esmcontrol $msg"
    el_print_logs
    el_log_title "Enterprise Storage Manager" $ESM_VERSION "End of esmcontrol"

    exit $status
}

#---------------------------------------
# Start or Stop the components
# Arguments:
#    $1 action (start, stop)
#    $2 comment used in log output
#---------------------------------------
l_start_components ()
{
    action=$1
    comment=$2

    _components=""
    [ ${_agent} -eq 1 ] && _components=$ESM_A_COMPS
    [ ${_management} -eq 1 ] && _components="$_components $ESM_M_COMPS"
    [ -z "$_components" ] && _components=$ESM_MA_COMPS

    tmplist=${_components}
    # reverse component list for stopping 
    [ "${action}" = "stop" ] && tmplist=`el_reverse_list "${_components}"`

    el_esmecho 1 n ""
    el_log 1 "Begin: $comment processes"

    for comp in ${tmplist}
    do
	# check for component existence
	tmpcmd=$ESM_BASE/component/$comp/bin/check
	# skip control if the component check is not found
	[ ! -x $tmpcmd ] && continue
	$tmpcmd --install
	ret=$?
	if [ $ret -ne $EL_PK_SOME -a $ret -ne $EL_PK_ALL ]; then
	    el_secho 3 $ESM_LOG n "$comp is not found."
	    continue
	fi

	tmpcmd=$ESM_BASE/component/$comp/bin/control
	if [ -x $tmpcmd ]; then
	    el_secho 1 $ESM_LOG n "${comment} $comp"
	    el_secho 3 $ESM_LOG n "$tmpcmd ${action}"
	    $tmpcmd ${action}
	    if [ $? -ne 0 ]; then
		_OVERALLSTATUS="$EL_FAIL"
		el_log 1 "ERROR: ${action} $comp failed."
	    fi
	else
	    el_secho 3 $ESM_LOG n "INFO: $tmpcmd is not executable."
	fi
    done
    el_log 1 "End:   $comment processes"
}

#---------------------------------------
# Main
#---------------------------------------

el_user_root
[ $? -ne 0 ] && exit 1

el_set_java_home

# Always attempt to set vars for EE CD (skip the ones that are not found)
el_setCDComps "EE"

#
# Handle cli options first 
#
l_cli_opts $@

el_log_title "Enterprise Storage Manager" $ESM_VERSION "Start of Control"

if [ "$_action" = "restart" ]; then
    l_start_components "stop" "Stopping"
    l_start_components "start" "Starting"
else
    l_start_components "$_action" "$_comment"
fi

l_exit_control
