#! /bin/sh
#
# Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
# 
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
# 
# The following is for chkconfig on RedHat based systems
# chkconfig: 2345 95 05
# description: pmie is a performance monitoring inference engine
#
# The following is for insserv(1) based systems,
# e.g. SuSE, where chkconfig is a perl script.
### BEGIN INIT INFO
# Provides:       pmie
# Required-Start: $network $remote_fs $syslog
# Required-Stop:  $remote_fs $syslog
# Default-Start:  2 3 4 5
# Default-Stop:   0 1 6
# Short-Description:   Control PCP inference engines
# Description:   Configures the performance monitoring inference engine
### END INIT INFO

. $PCP_DIR/etc/pcp.env
. $PCP_SHARE_DIR/lib/rc-proc.sh

PMIECTRL=$PCP_PMIECONTROL_PATH
RUNDIR=$PCP_LOG_DIR/pmie
pmprog=$PCP_RC_DIR/pmie
prog=$PCP_RC_DIR/`basename $0`

# search for your mail agent of choice ...
#
MAIL=''
for try in Mail mail email
do
    if which $try >/dev/null 2>&1
    then
	MAIL=$try
	break
    fi
done

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

if [ $pmprog = $prog ]
then
    VERBOSE_CTL=on
else
    VERBOSE_CTL=off
fi

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 -a "$1" != "status" ]
then
    echo "$prog:"'
Error: You must be root (uid 0) to start or stop pmie.'
    exit
fi

_usage()
{
    echo "Usage: $pmprog [-v] {start|restart|stop|status|reload|force-reload}"
}

_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
	_get_pids_by_name pmie | 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"
	    if [ ! -z "$MAIL" ]
	    then
		$MAIL -s "pmie_check start failed in $prog" root <$tmp.pmie >/dev/null 2>&1
	    else
		echo "$prog: pmie_check start failed ..."
		cat $tmp.pmie
	    fi
	fi
	rm -f $tmp.pmie
    else
	status=$?
	pmpost "pmcd_wait failed in $prog: exit status: $status" 
	if [ ! -z "$MAIL" ]
	then
	    echo "pmcd_wait: exit status: $status" | $MAIL -s "pmcd_wait failed in $prog" root
	else
	    echo "$prog: pmcd_wait failed ..."
	    echo "pmcd_wait: exit status: $status"
	fi
    fi
}

_shutdown()
{
    _get_pids_by_name pmie >$tmp.pmies 2>&1
    if [ ! -s $tmp.pmies ]
    then
	[ "$1" = verbose ] && echo "$pmprog: PMIE not running"
	return 0
    fi

    $ECHO $PCP_ECHO_N "Waiting for PMIE process(es) to terminate ..." "$PCP_ECHO_C"

    pmie_check -s >$tmp.pmie 2>&1
    if [ -s $tmp.pmie ]
    then
	pmpost "pmie_check stop failed in $prog, mailing output to root"
	if [ ! -z "$MAIL" ]
	then
	    $MAIL -s "pmie_check stop failed in $prog" root <$tmp.pmie
	else
	    echo "$prog: pmie_check stop failed ..."
	    cat $tmp.pmie
	fi
    fi

    true
    $RC_STATUS -v
    rm -fr $tmp.pmie $PCP_TMP_DIR/pmie
    pmpost "stop pmie from $pmprog"
}

while getopts v c
do
    case $c
    in
	v)  # force verbose
	    VERBOSE_CTL=on
	    ;;
	*)
	    _usage
	    exit 1
	    ;;
    esac
done
shift `expr $OPTIND - 1`

if [ $VERBOSE_CTL = on ]
then                            # For a verbose startup and shutdown
    ECHO=$PCP_ECHO_PROG
else                            # For a quiet startup and shutdown
    ECHO=:
fi

case "$1" in
  'start'|'restart'|'reload'|'force-reload')
	_shutdown quietly

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

	_reboot_setup

	if which pmie >/dev/null 2>&1
	then
	    if is_chkconfig_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 $PCP_ECHO_N "Performance Co-Pilot starting inference engine(s) ..." "$PCP_ECHO_C"
			_start_pmie &
			$RC_STATUS -v
		    fi
		fi
	    elif [ "$0" = "$PCP_RC_DIR/pmie" ]
	    then
		echo "$prog: Warning: Performance Co-Pilot Inference Engine (pmie) is disabled."
		chkconfig_on_msg pmie
	    fi
	fi
	status=0
        ;;

  'stop')
	_shutdown verbose
	status=0
        ;;

  'status')
        # NOTE: $RC_CHECKPROC returns LSB compliant status values.
	$ECHO $PCP_ECHO_N "Checking for pmie:" "$PCP_ECHO_C"
        if [ -r /etc/rc.status ]
        then
            # SuSE
            $RC_CHECKPROC pmie
            $RC_STATUS -v
            status=$?
        else
            # not SuSE
            $RC_CHECKPROC pmie
            status=$?
            if [ $status -eq 0 ]
            then
                $ECHO running
            else
                $ECHO stopped
            fi
        fi
        ;;
  *)
	_usage
        ;;
esac

