#!/bin/sh
# *****************************************************************
# *                                                               *
# *    Copyright (c) Digital Equipment Corporation, 1999          *
# *                                                               *
# *   All Rights Reserved.  Unpublished rights  reserved  under   *
# *   the copyright laws of the United States.                    *
# *                                                               *
# *   The software contained on this media  is  proprietary  to   *
# *   and  embodies  the  confidential  technology  of  Digital   *
# *   Equipment Corporation.  Possession, use,  duplication  or   *
# *   dissemination of the software and media is authorized only  *
# *   pursuant to a valid written license from Digital Equipment  *
# *   Corporation.                                                *
# *                                                               *
# *   RESTRICTED RIGHTS LEGEND   Use, duplication, or disclosure  *
# *   by the U.S. Government is subject to restrictions  as  set  *
# *   forth in Subparagraph (c)(1)(ii)  of  DFARS  252.227-7013,  *
# *   or  in  FAR 52.227-19, as applicable.                       *
# *                                                               *
# *****************************************************************
#
# chkconfig: 2345 99 99
# description: The CMF console logging Daemon
#

GetCmfdPid()
{
	if [ -n "$1" ]
	then
		GETMYPID=$1
		GETMYPPID="1"
		shift
		/bin/ps -e -o pid,ppid,command $* | while read mypid myppid command args
		do
			if [ "$command" = "$GETMYPID" -a "$myppid" = "$GETMYPPID" ]
			then
				echo "$mypid"	
				exit
			fi
		done
	fi
}

CMFD=/usr/opt/ams/bin/cmfd
if [ ! -x $CMFD ]; then
    echo "CMF: console logging daemon '$CMFD' not found!"
    exit 1
fi

CMFHOME=/usr/opt/ams
CMFPORT=6500

CMFSECURITY="-S1"
AMSMGR=/usr/opt/ams/bin/amsmgr
SYSMAP=/usr/opt/ams/config/SystemMap.xml

case "$1" in
'start')
        #
        # Start the cmf daemon
        security=`$AMSMGR -view security`
        if [ $security = "false" ]; then
            CMFSECURITY="-S0"
        fi
        pid=`GetCmfdPid $CMFD`
        if [ ! -z "$pid" ]; then
            echo "CMF: daemon is already running"
            exit 1
        fi
        if [ -f $SYSMAP ]; then
            echo "Writing cmf.conf from SystemMap.xml"
            $AMSMGR -write cmfconf
            echo "CMF: starting console logging daemon"
            $CMFD $CMFSECURITY
 	    /bin/sleep 5
        else
            echo "AMS: not configured - no '$SYSMAP' found"
        fi
        ;;
        
'stop')
        pid=`GetCmfdPid $CMFD`
        if [ ! -z "$pid" ]; then
            echo "CMF: stopping cmf console logging daemon"
            /usr/opt/ams/bin/cmfdkill $pid
            /bin/sleep 10
            pid=`GetCmfdPid $CMFD`
            if [ ! -z "$pid" ]; then
                echo "could not kill cmfd"
                echo "  -- kill by hand then do a start"
                exit 1
            fi
        else
            echo "CMF: could not find a running cmfd"
        fi
        ;;
'restart')
        $0 stop
        $0 start
        ;;
'update')
        pid=`GetCmfdPid $CMFD`
        if [ ! -z "$pid" ]; then
            echo "CMF: re-read configuration file"
            /usr/opt/ams/bin/cmfdkill -HUP $pid
        else
            echo "CMF: could not find a running cmfd"
        fi
        ;;
'status')
	pid=`GetCmfdPid $CMFD`
	if [ ! -z "$pid" ]; then
		echo "cmfd is running"
		exit 0
	else
		echo "cmfd is NOT running"
		exit 1
	fi
	;;
*)
        echo "Usage: $0 {start|stop|restart|update|status}"
        ;;
esac


