#! /bin/sh
#
# ident	"@(#)es-stop	1.42	03/03/03 SMI"
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

print_usage() {
    echo ""
    eval echo `/usr/bin/gettext 'Usage : $PROGNAME -aefghmprstxAS [-y instanceName]'`
    /usr/bin/gettext "     a : stops Sun Management Center Agent\n"
    /usr/bin/gettext "     e : stops Sun Management Center Event manager\n"
    /usr/bin/gettext "     f : stops Sun Management Center Configuration manager\n"
    /usr/bin/gettext "     g : stops Sun Management Center Services\n"
    /usr/bin/gettext "     h : prints this usage report\n"
    /usr/bin/gettext "     l : stops Platform Agent for SSP\n"
    /usr/bin/gettext "     m : stops Sun Management Center Metadata Agent\n"
    /usr/bin/gettext "     p : stops Sun Management Center Topology manager\n"
    /usr/bin/gettext "     r : stops Hardware Diagnostic Suite Service\n"
    /usr/bin/gettext "     s : stops Sun Management Center Server\n"
    /usr/bin/gettext "     t : stops Sun Management Center Trap Handler\n"
    /usr/bin/gettext "     x : stops CST Service\n"
    /usr/bin/gettext "     y : stops instance of platform agent\n"
    /usr/bin/gettext "     w : stops web server\n"
    /usr/bin/gettext "     A : stops all Sun Management Center components listed above\n"
    fmt=`/usr/bin/gettext "equivalent to %s -aefgmprstxw"`
    /usr/bin/printf "             $fmt\n" $PROGNAME
    /usr/bin/gettext "     S : stops all Sun Management Center components listed above except Sun Management Center Agent\n"
    fmt=`/usr/bin/gettext "equivalent to %s -efgmprstxw"`
    /usr/bin/printf "             $fmt\n" $PROGNAME
    echo ""

}
stop_all_platform_instances () 
{
    if [ ! -f "${VARDIR}/platform-instances.dat" ] ; then
        return 0
    fi
    echologverbose "Stopping all platform instances"
    list_of_instances=""
    list_of_instances=`$XGET ${VARDIR}/platform-instances.dat ControlBlock.instances`
    echologverbose 'Instances are $2' "$list_of_instances"
    if [ -z "$list_of_instances" ] ; then
        return 0
    fi
    list_of_instances=`echo $list_of_instances | /usr/bin/sed -e "s/,/ /g"`

    for each_instance in $list_of_instances
    do
	es-stop.sh $each_instance 

    done
}

component_missing() {
    echo ""
    eval echo `/usr/bin/gettext '$1 Component not installed'`
    echo ""
    exit 1
}

stop_web_server ()
{
    webserver_stop_command=$BASEDIR/netscape/https-localhost/stop
    if [ ! -f $webserver_stop_command ] ; then
        return 0
    fi

    $webserver_stop_command
    if [ $? -eq 0 ] ; then
        return 0
    fi
    
    kill_processes "$BASEDIR/netscape/https-localhost/config" "KILL"
    return 0
}

stop_hwds_service()
{
    EDS_A=""
    EDS_A1=""
    EDS_A=`ps -ef | fgrep edagent | fgrep -v fgrep | awk '{ print $2 }'`

    # Stop the HWDS Agent
    /usr/bin/pkginfo -qi SUNWedag
    if [ $? -eq 0 -a ! -z "$EDS_A" ]; then
        HWDiag_dir=`/usr/bin/pkginfo -r SUNWedag`/SUNWsymon/addons/AdvancedMonitoring
        if [ 1 -lt $EDS_A ]; then
            if [ -x "$HWDiag_dir/lib/agent/ed_cmd" ]; then
                $HWDiag_dir/lib/agent/ed_cmd stop > /dev/null
                sleep 1
                EDS_A1=`$HWDiag_dir/lib/agent/ed_cmd quit`
                if [ "$EDS_A1" != "QUIT DONE" ] ; then
                    # almost everything else may hang the script at this point.
                    /usr/bin/kill -9 $EDS_A
                fi
            fi
        fi
    fi

    # Stop The HWDS Server
    /usr/bin/pkginfo -qi SUNWed
    if [ $? -eq 0 ]; then
        HWDS_CMD=`/usr/bin/pkginfo -r SUNWed`/SUNWsymon/addons/AdvancedMonitoring/sbin/stop-diag
        if [ -x "$HWDS_CMD" ]; then    
            $HWDS_CMD
        fi
    fi

}


###############################################
#
#   Main entry point
#
#
prog_base=`/usr/bin/basename $0`
common_cmd=`echo $0 | /usr/bin/sed s/$prog_base/es-common.sh/`
. $common_cmd

set_basedir
set_db_env

/usr/bin/pkginfo -qi SUNWesagt
if [ $? -eq 0 ]; then
    set_xput
fi

PROGNAME="$0"; export PROGNAME

ESROOT=$BASEDIR; export ESROOT

if [ $# -eq 0 ] ; then
    echolog ""
    echolog "Please specify a component to be stopped."
    print_usage
    exit 1
fi

case $1 in
    -metadata | -console | -server | -agent  | -service | -cfgserver | -trap | \
    -topology | -event | -cstservice | -serverall | -all | -event | -help)
	echo ""
	eval echo `/usr/bin/gettext 'Invalid option\\\($1\\\).  Options have changed to single letters:'`
	/usr/bin/gettext "Use the following single letter arguments instead:\n"
	echo "   -agent         -a"
	echo "   -metadata      -m"
	echo "   -all           -A"
	echo "   -cfgserver     -f"
	echo "   -service       -g"
	echo "   -console       -c"
	echo "   -help          -h"
	echo "   -event         -e"
	echo "   -cstservice    -x"
	echo "   -server        -s"
	echo "   -serverall     -S"
	echo "   -topology      -p"
	echo "   -trap          -t"
	echo ""
	exit 1
	;;
esac

check_root

cd "$BASEDIR"

PWD="$BASEDIR"; export PWD
set_paths
set_inst_flags
VERBOSE=OFF
stop_db=0
while getopts y:cgsmatpfehxwASlvrY OPT
do
    case $OPT in
	s)
	    [ $no_server -eq 1 ] && component_missing Server
	    es-server-stop.sh
	    ;;
	m)
	    [ $no_metadata -eq 1 ] && component_missing Metadata
	    es-stop.sh metadata
	    ;;

	a)
	    [ $no_agent -eq 1 ] && component_missing Agent
	    es-stop.sh agent
	    ;;
	l)
	    [ $no_agent -eq 1 ] && component_missing Agent
	    es-stop.sh platform
	    ;;
	f)
	    [ $no_server -eq 1 ] && component_missing Server
	    es-stop.sh cfgserver
	    ;;
	g)
	    [ $no_service -eq 1 ] && component_missing Services
	    $BASEDIR/classes/base/service/bin/es-service-stop.sh
	    ;;
	h)
	    print_usage
	    exit 0
	    ;;
	t)
	    [ $no_server -eq 1 ] && component_missing Server
	    es-stop.sh trap
	    ;;
	p)
	    [ $no_server -eq 1 ] && component_missing Server
	    es-stop.sh topology
	    #
	    #Stop dicovery process
	    #
	    kill_all_processes "discover" "KILL"
	    ;;
	e)
	    [ $no_server -eq 1 ] && component_missing Server
	    es-stop.sh event
	    ;;
	x)
	    [ $no_server -eq 1 ] && component_missing Server
	    es-stop.sh cstservice
	    ;;
	w)
	    stop_web_server
	    ;;
	r)
            stop_hwds_service
            ;;
	    
	S | A)
	    if [ $no_server -eq 0 ] ; then
		es-stop.sh metadata
		es-stop.sh cfgserver
		es-stop.sh topology
		es-stop.sh event
		if [ -f "$BASEDIR/classes/base/service/bin/es-service-stop.sh" ] ; then
		    $BASEDIR/classes/base/service/bin/es-service-stop.sh
		fi
		/usr/bin/pkginfo -qi SUNWcstv
		[ $? -eq 0 ] && es-stop.sh cstservice
		/usr/bin/pkginfo -qi SUNWcstve
		[ $? -eq 0 ] && es-stop.sh cstservice
		es-stop.sh trap
		es-server-stop.sh
        	stop_hwds_service
                #
                #Stop dicovery process
                #
                kill_all_processes "discover" "KILL"
                #
		# Shutdown the database engine
		#
		# In the future, we should check all the database
		# dependent components and shutdown the engine 
		# accordingly.
		#

		stop_db=1

		#
		# Stop the web server
		#

		stop_web_server

	    fi
	    if [ $OPT = "A" ] ; then
		if [ $no_agent -eq 0 ] ; then
		    es-stop.sh agent
		    es-stop.sh platform
                    stop_all_platform_instances
		fi
	    fi

	    ;;
        v)
	    VERBOSE=ON
	    ;;
	y)
	    [ $no_agent -eq 1 ] && component_missing Agent
	    es-stop.sh "$OPTARG"
            ;;
	Y)
		stop_all_platform_instances
		;;
	\?)
	    print_usage
	    exit 1
	    ;;
    esac
done
if [ $stop_db -eq 1 ] ; then
    if [ -f /tmp/db-stop.log ] ; then
	rm -rf /tmp/db-stop.log
    fi

    if [ -f /tmp/db-stop-sql.log ] ; then
	rm -rf /tmp/db-stop-sql.log
    fi


    prstring=`$ESROOT/base/bin/sparc-sun-solaris${OSVERS}/smcpwchk $SMCORAU 2>/dev/null`

    retcode=$?

    if [ $retcode = 2 ]
    then
       echolog 'the user $2 was not found by a getpwnam system call ,not in /etc/passwd, nis, nis+, etc..' "$SMCORAU"
       echolog 'exiting.............................'
       exit 1
    fi

    grstring=`$ESROOT/base/bin/sparc-sun-solaris${OSVERS}/smcgrchk $SMCORAG 2>/dev/null`

    retcode=$?

    if [ $retcode = 2 ]
    then
       echolog 'the group $2 was not found by a getgrnam system call ,not in /etc/group, nis, nis+, etc..' "$SMCORAG"
       echolog 'exiting.............................'
       exit 1
    fi



    if [ "$VERBOSE" = "ON" ] ; then
	su - $SMCORAU -c $BASEDIR/sbin/db-stop -v
    else
	su - $SMCORAU -c $BASEDIR/sbin/db-stop
    fi
fi

