#! /bin/sh
#
# Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
# Start or Stop the Performance Co-Pilot Inference Engine
#
# $Id: etc_init.d_pmie,v 1.2 2000/12/28 09:17:25 max Exp $
#
# chkconfig: 2345 95 05
# description: pmie is a performance monitoring inference engine

# source the PCP configuration environment variables
. /etc/pcp.env

# Get the portable PCP rc script functions
if [ -f $PCP_SHARE_DIR/lib/rc-proc.sh ] ; then
    . $PCP_SHARE_DIR/lib/rc-proc.sh
fi

PMIE=pmie
PMIECTRL=$PCP_VAR_DIR/config/pmie/control
prog=$PCP_RC_DIR/`basename $0`
MAIL=Mail

tmp=/var/tmp/$$
status=1
trap "rm -f $tmp.* ; exit \$status" 0 1 2 3 15

if [ "$PCP_PLATFORM" = "linux" ]
then
    ECHO=echo			# Linux does not have verbose flags
    IS_ON=is_chkconfig_on
    RUNDIR=$PCP_LOG_DIR/pmie
else
    IS_ON=/sbin/chkconfig
    RUNDIR=${PCP_LOGDIR:-/var/adm/pmielog}
    if $IS_ON verbose
    then			# For a verbose startup and shutdown
	ECHO=echo
    else			# For a quiet startup and shutdown
	ECHO=:
    fi
fi

# determine real name for localhost
#
LOCALHOSTNAME=`hostname`
if [ -z "$LOCALHOSTNAME" ] ; then
    LOCALHOSTNAME="localhost"
    [ -x /usr/bsd/hostname ] && LOCALHOSTNAME=`/usr/bsd/hostname`
fi
LOGDIR=$RUNDIR/$LOCALHOSTNAME

id=`id | sed -e "s/(.*//" -e "s/.*=//"`
if [ "$id" != 0 ]
then
    echo "$prog:"'
Error: You must be root (uid 0) to start or stop pmie.'
    exit
fi

_reboot_setup()
{
    # base directories and house-keeping for pmie instances
    #
    if [ ! -d $PCP_TMP_DIR/pmie ]
    then
	mkdir -p $PCP_TMP_DIR/pmie
    else
	rm -rf $tmp.ent $tmp.pid
	here=`pwd`
	cd $PCP_TMP_DIR/pmie
	ps -ef | grep pmie | grep -v grep | $PCP_AWK_PROG '{ print $2 }' \
	    | sed -e 's/[ 	]*//g' | sort >$tmp.pid
	ls [0-9]* 2>&1 | sed -e '/\[0-9]\*/d' \
	    | sed -e 's/[ 	][ 	]*//g' | sort >$tmp.ent
	# remove entries without a pmie process
	rm -f `comm -23 $tmp.ent $tmp.pid`
	rm -f $tmp.ent $tmp.pid
	cd $here
    fi
    chmod 1777 $PCP_TMP_DIR/pmie

    [ ! -d $LOGDIR ] && mkdir -p $LOGDIR
}

_start_pmie()
{
    wait_option=''
    [ ! -z "$PMCD_WAIT_TIMEOUT" ] && wait_option="-t $PMCD_WAIT_TIMEOUT" 

    if pmcd_wait $wait_option
    then
	pmie_check >$tmp.pmie 2>&1
	if [ -s $tmp.pmie ]
	then
	    pmpost "pmie_check start failed in $prog, mailing output to root"
	    $MAIL -s "pmie_check start failed in $prog" root <$tmp.pmie >/dev/null 2>&1
	fi
	rm -f $tmp.pmie
    else
	status=$?
	pmpost "pmcd_wait failed in $prog: exit status: $status" 
	echo "pmcd_wait: exit status: $status" | $MAIL -s "pmcd_wait failed in $prog" root
    fi
}

_stop_pmie()
{
    $ECHO "Waiting for pmie process(es) to terminate ..."

    pmie_check -s >$tmp.pmie 2>&1
    if [ -s $tmp.pmie ]
    then
	pmpost "pmie_check stop failed in $prog, mailing output to root"
	$MAIL -s "pmie_check stop failed in $prog" root <$tmp.pmie
    fi
    rm -fr $tmp.pmie $PCP_TMP_DIR/pmie
}


case "$1" in
  'start')
	ps -ef | grep "$PMIE" | grep -v grep >$tmp.pmies 2>&1
	[ -s $tmp.pmies ] && _stop_pmie

	# messages should go to stderr, not the GUI notifiers
	#
	unset PCP_STDERR

	_reboot_setup

	if which $PMIE >/dev/null 2>&1
	then
	    if $IS_ON pmie
	    then
		[ ! -d "$RUNDIR" ] && mkdir -p "$RUNDIR"
		cd $RUNDIR
		if which pmie_check >/dev/null 2>&1
		then
		    if [ ! -f $PMIECTRL ]
		    then
			echo "$prog:"'
Error: PCP inference engine control file '$PMIECTRL'
       is missing!  Cannot start any Performance Co-Pilot inference engine(s).'
		    else
			$ECHO "Performance Co-Pilot starting inference engine(s) ..."
			_start_pmie &
		    fi
		elif [ $PCP_PLATFORM = "irix" ]
		then
		    PMIECHECK=/usr/pcp/bin/pmie_check
		    echo "$prog:"'
Warning: Performance Co-Pilot installation is incomplete (at least the
	 script '$PMIECHECK' is missing) and the PCP Inference
	 Engine(s) cannot be started.
	 You should re-install the pcp_eoe.sw.eoe subsystem from the IRIX
	 distribution and consult pmie_check(1), else
		# /sbin/chkconfig pmie off'
		fi
	    else
		if [ "$0" = "$PCP_RC_DIR/pmie" ]
		then
		    if [ "$PCP_PLATFORM" = "irix" ]
		    then
			echo "$prog:"'
Warning: Performance Co-Pilot Inference Engine (pmie) is disabled.  To enable:
	 # /sbin/chkconfig pmie on'
		    else
			echo "$prog: Warning: Performance Co-Pilot Inference Engine (pmie) is disabled."
			chkconfig_on_msg pmie
		    fi
		fi
	    fi
	fi
	status=0
        ;;

  'stop')
	_stop_pmie
	status=0
        ;;

  *)
        echo "Usage: $PCP_RC_DIR/pmie {start|stop}"
        ;;
esac

