#! /bin/sh

### BEGIN INIT INFO
# Provides:       likewise-perfd
# Required-Start: $local-fs $network $syslog
# Required-Stop:  $local-fs $network $syslog
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    performance data updater for samba
### END INIT INFO

# Copyright (c) 2005,2006 Centeris Corporation
# All rights reserved.
#
# Author: Marcin Porwit <mporwit@centeris.com>
#
# /etc/init.d/likewise-perfd
#
#  location stuff

LIKEWISEDIR=/opt/likewise
LIKEWISEBINDIR=$LIKEWISEDIR/bin
LIKEWISELIBDIR=$LIKEWISEDIR/lib

# set a default return value

RETVAL=0

#
# execute platform specific pieces
#

# some dists (ie: SuSE) contain the UN LSB rc.status file, so we use that one
# otherwise, we will use the one we brought with us.

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

# other dists (ie: RHEL4) contain the functions file
# NB: Note that this is in addition to the rc.status stuff, on machines that
# have this file, we need it for some daemon management functions.
# BUGBUG: this is not so great, and may not hold true for other dists that we
# may wish to work with in the future.

if [ -e /etc/init.d/functions ]; then . /etc/init.d/functions; fi

PERFD_BIN="$LIKEWISEBINDIR/perf_writer"
PID_FILE="/var/run/samba/perfd.pid"

rc_reset

# Check for missing binary
if [ ! -x ${PERFD_BIN} ]; then
	echo -n >&2 "Perf daemon ${PERFD_BIN} is not installed. "
	rc_status -s
	exit 5
fi

# be extra careful because connection fail if TMPDIR is not writeable
export TMPDIR="/var/tmp"

case "$1" in
	start)
		echo -n "Starting Perf SMB daemon "
		if [ -e /sbin/checkproc ]; then
		    checkproc -p ${PID_FILE} ${PERFD_BIN}

		    case $? in
			0) echo -n "- Warning: daemon already running. " ;;
			1) echo -n "- Warning: ${PID_FILE} exists. " ;;
		    esac
		    startproc -p ${PID_FILE} ${PERFD_BIN} -d
		    rc_status -v
		else
		    status $PERFD_BIN 
		    case $? in
			0) echo -n "- Warning: daemon already running. not starting"; exit 0 ;;
			1) echo -n "- Warning: ${PID_FILE} exists. " ;;
		    esac
		    daemon ${PERFD_BIN} -d
		fi
		;;
	stop)
		echo -n "Shutting down Perf daemon "
		if [ -e /sbin/checkproc ]; then
		    checkproc -p ${PID_FILE} ${PERFD_BIN} || \
			echo -n " Warning: daemon not running. "
		    killproc -p ${PID_FILE} -t 10 ${PERFD_BIN}
		    rc_status -v
		else
		    killproc ${PERFD_BIN} 15
		fi
		;;
	try-restart|condrestart)
		if test "$1" = "condrestart"; then
			echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
		fi
		$0 status
		if test $? = 0; then
			$0 restart
		else 
			rc_reset
		fi
		rc_status
		;;
	restart)
		$0 stop
		$0 start
		rc_status
		;;
	force-reload|reload)
		echo -n "Reloading Perf daemon "
		if [ -e /sbin/checkproc ]; then
		    checkproc -p ${PID_FILE} ${PERFD_BIN} && \
			touch ${PID_FILE} || \
			echo -n >&2 " Warning: daemon not running. "
		    killproc -p ${PID_FILE} -HUP ${PERFD_BIN}
		    rc_status -v
		else
	            # killproc exists in 'functions'
		    killproc $DISCOVERD_BIN 1
		fi

		;;
	status)
		echo -n "Checking for Perf daemon "
		if [ -e /sbin/checkproc ]; then
		    checkproc -p ${PID_FILE} ${PERFD_BIN}
		    rc_status -v
		    RETVAL=$?
		else
		    status ${PERFD_BIN}
		    RETVAL=$?
		fi
		;;
	probe)
		test ${SMB_CONF} -nt ${PID_FILE} && echo reload
		;;
	*)
		echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
		exit 1
		;;
esac
exit $RETVAL
