#!/sbin/sh
#
# Copyright 2002-2003 by Sun Microsystems, Inc.
# All rights reserved.
#
# @(#)sstrrun  @(#)sstrrun	1.21 03/03/21
#
# Functions used when running NSM. 
#
###############################################################################

#
# Includes

#
# Set SUNWnsm basedir
BASEDIR=`/usr/bin/pkginfo -r SUNWnsmbj 2>&1`
if [ $? -ne 0 ]; then
   BASEDIR=/opt
fi

#
# Defines
SSTR_HOME=$BASEDIR/SUNWnsm
SSTR_BIN=$SSTR_HOME/bin
SSTR_ETC=$SSTR_HOME/etc
SSTR_SBIN=$SSTR_HOME/sbin
SSTR_UTIL=$SSTR_HOME/util
SSTR_VARHOME=/var/$SSTR_HOME
SSTR_ETCHOME=/etc/$SSTR_HOME
SSTR_APACHE=$SSTR_SBIN/sstr.apache
SSTR_CRE=$SSTR_SBIN/sstr.cre
SSTR_CRE_DEFAULT=$SSTR_SBIN/sstr.cre.default
SSTR_PGSQL=$SSTR_SBIN/sstr.postgresql
SSTR_TOMCAT=$SSTR_SBIN/sstr.tomcat
SSTR_TOMCAT_DEFAULT=$SSTR_SBIN/sstr.tomcat.default
SSTR_CONFIGURE=$SSTR_SBIN/configure
SSTR_PROPERTIES=$SSTR_ETC/sstr.properties
JAVA_HOME=$SSTR_HOME/jre
SLP=/etc/init.d/slpd
JMQ=/etc/init.d/jmq
SYS_START=$SSTR_SBIN/sstr_sys

#
# Set package lists

MGMT_PACKAGES="SUNWnsmr SUNWnsmu SUNWnsmut SUNWnsmdb"
AGENT_PACKAGES="SUNWnsmar SUNWnsmau SUNWnsmat SUNWfcprv"
BOTH_PACKAGES="$MGMT_PACKAGES $AGENT_PACKAGES"

#
# Error
# error "description"
error () {
  echo $0: $* 2>&1
  exit 1
}

#
# Check for root
checkuserroot() {
user=`/bin/id | grep root | wc -l`
if [ $user -eq 0 ]; then
        echo "You must be root to execute this script."
        exit 1
fi
}

#
# Set station type

setstationtype() {
	# CHECK PACKAGES FOR STATION_TYPE

        for PACKAGE in $MGMT_PACKAGES
        do
            /bin/pkginfo -q $PACKAGE
            if [ $? -ne 0 ]; then
                MGMT=false
		break
	    else 
		MGMT=true
            fi
        done

        for PACKAGE in $AGENT_PACKAGES
        do
            /bin/pkginfo -q $PACKAGE
            if [ $? -ne 0 ]; then
                AGENT=false
                break
            else 
                AGENT=true
            fi
        done

        for PACKAGE in $BOTH_PACKAGES
        do
            /bin/pkginfo -q $PACKAGE
            if [ $? -ne 0 ]; then
                BOTH=false
                break
            else 
                BOTH=true
            fi
        done

	# SET STATION_TYPE
	if [ "$MGMT" = "false" -a "$AGENT" = "false" -a "$BOTH" = "false" ]; then
	error "Sun StorEdge(TM) Topology Reporter is not installed on this system."

	elif [ "$MGMT" = "true" -a "$AGENT" = "false" -a "$BOTH" = "false" ]; then
	STATION_TYPE="Management"

	elif [ "$MGMT" = "false" -a "$AGENT" = "true" -a "$BOTH" = "false" ]; then
        STATION_TYPE="Agent"

        elif [ "$MGMT" = "true" -a "$AGENT" = "true" -a "$BOTH" = "true" ]; then
        STATION_TYPE="Both"

	else
	error "Unable to set station type."
        fi
}

#
# Sanity check inits

# sanity check init(s)
checkinitslp() {
if  [ ! -x "$SLP" ]; then
        error "Cannot find required executable script: $SLP"
fi
}

# sanity check init(s)
checkinitjmq() {
if  [ ! -x "$JMQ" ]; then
        error "Cannot find required executable script: $JMQ"
fi
}

# sanity check init(s)
checkinitpgsql() {
if  [ ! -x "$SSTR_PGSQL" ]; then
        error "Cannot find required executable script: $SSTR_PGSQL"
fi
}

# sanity check init(s)
checkinitapache() {
if  [ ! -x "$SSTR_APACHE" ]; then
        error "Cannot find required executable script: $SSTR_APACHE"
fi
}

# sanity check init(s)
checkinitcre() {
if  [ ! -x "$SSTR_CRE" ]; then
        error "Cannot find required executable script: $SSTR_CRE"
fi
}

# sanity check init(s)
checkinitcredefault() {
if  [ ! -x "$SSTR_CRE_DEFAULT" ]; then
        error "Cannot find required executable script: $SSTR_CRE_DEFAULT"
fi
}

# sanity check init(s)
checkinittomcat() {
if  [ ! -x "$SSTR_TOMCAT" ]; then
        error "Cannot find required executable script: $SSTR_TOMCAT"
fi
}

# sanity check init(s)
checkinittomcatdefault() {
if  [ ! -x "$SSTR_TOMCAT_DEFAULT" ]; then
        error "Cannot find required executable script: $SSTR_TOMCAT_DEFAULT"
fi
}

# sanity check init(s)
checkinitconfigure() {
if  [ ! -x "$SSTR_CONFIGURE" ]; then
        error "Cannot find required executable script: $SSTR_CONFIGURE"
fi
}

#
# Find named process(es)

# find the named process(es)
findprocpgsql() {
  /usr/bin/pgrep -f "$SSTR_HOME.*postmaster"
}

# find the named process(es)
findprocapache() {
  /usr/bin/pgrep -f "$SSTR_HOME.*httpd"
}

# find the named process(es)
findproccre() {
  /usr/bin/pgrep -f "$SSTR_HOME.*-DCRE[= ]"
}

# find the named process(es)
findproctomcat() {
  /usr/bin/pgrep -f "$SSTR_HOME.*-DTOMCAT[= ]"
}

# find the named process(es)
findprocjmq() {
  /usr/bin/pgrep -f jmqbroker
}

# find the named process(es)
findprocslp() {
  /usr/bin/pgrep -u 0 -P 1 |
  while read PID; do
    if 
      /usr/bin/pfiles $PID 2> /dev/null |
      /usr/bin/grep "sockname.*AF_INET.*port.*427" > /dev/null 2>&1 
    then
      echo $PID
    fi
  done
}

#
# Get SSTR version
getsstrversion() {
  SSTR_VERSION=`/usr/bin/pkgparam SUNWnsmbj VERSION`
}

#
# Get SSTR architecture
getsstrarch() {
  SSTR_ARCH=`/usr/bin/pkgparam SUNWnsmbj ARCH`
}

#
# Ensure full product shutdown
insureshutdown() {

	echo "Ensuring SSTR core components are fully shutdown before proceeding..."

		# Check for and kill everything

                if [ -n "`findproccre`" ]; then
                        if  [ -f "$SSTR_CRE" -a -x "$SSTR_CRE" ]; then
                            $SSTR_CRE                  stop
                        else
                           /usr/bin/pkill -9 -f "$SSTR_HOME.*-DCRE[= ]"
                        fi
			/usr/bin/sleep 2
                        if [ -n "`findproccre`" ]; then
                            echo ""
                            echo "Unable to shutdown the CRE for SSTR successfully"
                            exit 1
                        else
                            echo ""
                            echo "The CRE for SSTR successfully shut down"
                        fi
                else
                    echo ""
                    echo "The CRE for SSTR...............not running."
                fi

                if [ -n "`findproctomcat`" ]; then
                        if  [ -f "$SSTR_TOMCAT" -a -x "$SSTR_TOMCAT" ]; then
                            $SSTR_TOMCAT                stop
                        else
                           /usr/bin/pkill -9 -f "$SSTR_HOME.*-DTOMCAT[= ]"
                        fi
			/usr/bin/sleep 2
                        if [ -n "`findproctomcat`" ]; then
                            echo "Unable to shutdown Tomcat for SSTR successfully"
                            exit 1
                        else
                            echo "Tomcat for SSTR successfully shut down"
                        fi
                else
                    echo "Tomcat for SSTR................not running."
                fi

                if [ -n "`findprocapache`" ]; then
                    echo "Stopping Apache"
                        if  [ -f "$SSTR_APACHE" -a -x "$SSTR_APACHE" ]; then
                            $SSTR_APACHE               stop
                        else
                           /usr/bin/pkill -9 -f "$SSTR_HOME.*httpd"
                        fi
			/usr/bin/sleep 2
                        if [ -n "`findprocapache`" ]; then
                            echo "Unable to shutdown Apache for SSTR successfully"
                            exit 1
                        else
                            echo "Apache for SSTR successfully shut down"
                        fi
                else
                    echo "Apache for SSTR................not running."
                fi

                if [ -n "`findprocpgsql`" ]; then
                        if  [ -f "$SSTR_PGSQL" -a -x "$SSTR_PGSQL" ]; then
                            $SSTR_PGSQL                stop
                        else
                           /usr/bin/pkill -9 -f "$SSTR_HOME.*postmaster"
                        fi
			/usr/bin/sleep 2
                        if [ -n "`findprocpgsql`" ]; then
                            echo "Unable to shutdown PostgreSQL for SSTR successfully"
                            echo ""
                            exit 1
                        else
                            echo "PostgreSQL for SSTR successfully shut down"
                            echo ""
                        fi
                else
                    echo "PostgreSQL for SSTR............not running."
                    echo ""
                fi
	return 0
}

#
# Ensure slp is fully shutdown
insureshutdownslp() {

        echo "Ensuring SLP is fully shutdown before proceeding..."

                # Check for and kill slp

                if [ -n "`findprocslp`" ]; then
                        if  [ -f "$SLP" -a -x "$SLP" ]; then
                            $SLP                       stop
			    /usr/bin/sleep 3
			    if [ -n "`findprocslp`" ]; then
			        /usr/bin/kill -9 `findprocslp`
			    else
			        echo "SLP successfully shut down"
			    fi
                        else
                           /usr/bin/kill -9 `findprocslp`
                        fi

                        if [ -n "`findprocslp`" ]; then
                            error "Unable to shutdown SLP successfully"
                        else
                            echo "SLP successfully shut down"
                        fi
                else
                    echo ""
                    echo "SLP............................not running."
                fi
}


insureshutdownimq() {

        echo "Ensuring iMQ is fully shutdown before proceeding..."

                # Check for and kill slp

                if [ -n "`findprocjmq`" ]; then
                        if  [ -f "$JMQ" -a -x "$JMQ" ]; then
                            $JMQ                       stop
			    /usr/bin/sleep 3
			    if [ -n "`findprocjmq`" ]; then
			        /usr/bin/kill -9 `findprocjmq`
			    else
			        echo "iMQ successfully shut down"
			    fi
                        else
                           /usr/bin/kill -9 `findprocjmq`
                        fi

                        if [ -n "`findprocjmq`" ]; then
                            error "Unable to shutdown iMQ successfully"
                        else
                            echo "iMQ successfully shut down"
                        fi
                else
                    echo ""
                    echo "iMQ............................not running."
                fi
}


#
# print log footer/header
#
log_title ()
{
    product=$1
    prodvers=$2
    description=$3  #ie: start_install, end_uninstall, start_config
    echo ""
    echo "==================================="
    echo "Sun StorEdge(TM) $product $prodvers "
    echo " $description"
    echo " `date`"
    echo "==================================="
    echo ""
}

#
# print properties
#
print_properties ()
{
    title=$1
    echo "-----------------------"
    echo " $title "
    echo "-----------------------"

    if  [ -r "$SSTR_PROPERTIES" -a -f "$SSTR_PROPERTIES" ]; then
        # Print contents of sstr.properties to STDOUT
        /usr/bin/cat $SSTR_PROPERTIES | /usr/bin/grep -v "\#"
    fi
}

