#!/bin/sh

### BEGIN INIT INFO
# Provides:       likewise-open
# Required-Start: $local-fs $network $syslog
# Required-Stop:  $local-fs $network $syslog
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Start translation of syslog into eventlog entries
### END INIT INFO

#   The contents of this file are subject to the terms of the
#   Common Development and Distribution License, Version 1.0 only
#   (the "License").  You may not use this file except in compliance
#   with the License.
 
#   You can obtain a copy of the license at 
#   http://www.opensource.org/licenses/cddl1.php
 
#   When distributing Covered Code, include this CDDL HEADER in each
#   file and include the original License file at src/license.txt.
#   If applicable, add the following below this CDDL HEADER, with the
#   fields enclosed by brackets "[ ]" replaced with your own identifying
#   information: Portions Copyright [yyyy] [name of copyright owner]

#   Copyright (c) 2005 by Centeris, Inc.  All rights reserved.
#   Use is subject to license terms.
#   Initial Developer updates, news, and information may be found at 
#   http://sourceforge.net/projects/likewiseopenagt

if [ -e /etc/rc.status ] ; then 
    . /etc/rc.status 
fi

#  We need this for later...

if [ ! -f /opt/likewise/cesm-agent/bin/jrevar.sh ]; then
   /opt/likewise/cesm-agent/bin/findjre.sh
fi

. /opt/likewise/cesm-agent/bin/jrevar.sh


FILE_PID=/var/run/likewise-open.pid

# where is java? /usr/bin/java?
EXE_PERL=`which perl`
EXE_DIR=/opt/likewise/cesm-agent/bin

case "$1" in
    start)

	echo -n "Starting Likewise Open agent  "

	if [[ -e $FILE_PID && ! -z `ps -o state= -p \`/bin/cat $FILE_PID\`` ]] ; then
	    echo "is already running." 
        else
	    cd ${EXE_DIR}
	    nohup $EXE_PERL ${EXE_DIR}/likewise-open-agent.pl &
	    echo $!>$FILE_PID
	    sleep 1
	    if ! [[ -e $FILE_PID && ! -z `ps -o state= -p \`/bin/cat $FILE_PID\`` ]] ; then
		rc_failed
            fi
	fi
	rc_status -v
    ;;
    status)

	if [[ -e $FILE_PID && ! -z `ps -o state= -p \`/bin/cat $FILE_PID\`` ]] ; then
            echo "$0 is running"
        else
            echo "$0 is stopped"
        fi
    ;;
    stop)

	echo -n "Stopping likewise-open agent  "

	if [[ -e $FILE_PID && ! -z `ps -o state= -p \`/bin/cat $FILE_PID\`` ]] ; then	
	    kill `cat $FILE_PID`
	else
	    echo "no pidfile - already stopped?"
	fi
	rc_status -v
    ;;
    *)
        echo "Usage: $0 {start|stop|status}"
        exit 1
    ;;
esac
