#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          opsipxeconfd
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: opsi pxe config service
# Description:       Opsi PXE Configuration Service
### END INIT INFO
# chkconfig: 2345 80 20

DAEMON=/usr/bin/opsipxeconfd
USER=root
LOGFILE=/var/log/opsi/opsipxeconfd.log
PIDDIR=/var/run/opsipxeconfd
PIDFILE=$PIDDIR/opsipxeconfd.pid


# See if the binary is there
if [ ! -x $DAEMON ]; then
	echo "$DAEMON not installed"
	[ "$1" = "stop" ] && exit 0
	exit 5
fi

start() {
	echo -n "Starting opsi pxe configuration service.."
		
	# Make sure we have our PIDDIR, even if it's on a tmpfs
	install -o $USER -g root -m 755 -d $PIDDIR
	
	# Make sure logfile is writable
	test -e $LOGFILE && chown -R $USER $LOGFILE
	
	if [ -f $PIDFILE ] && ps h  $(cat $PIDFILE) >/dev/null 2>/dev/null; then
		echo ".   (already running)."
		exit 0
	else
		[ -f $PIDFILE ] && rm $PIDFILE
		
		su - $USER -c "$DAEMON start"
		
		pidfileseen=0
		running=false
		i=1
		while [ $i -le 10 ]; do
			echo -n "."
			if ([ -f $PIDFILE ] && ps h $(cat $PIDFILE) >/dev/null 2>/dev/null); then
				pidfileseen=$(($pidfileseen+1))
				if [ $pidfileseen -ge 3 ]; then
					running=true
					break
				fi
			else
				if [ $pidfileseen -ge 1 ]; then
					running=false
					break
				fi
			fi
			sleep 1
			i=$(($i+1))
		done
		
		if [ "$running" = "true" ]; then
			echo "   (done)."
		else
			echo "   (failed)."
			exit 1
		fi
	fi
}

stop() {
	echo -n "Stopping opsi pxe configuration service...   "
		
	if [ -f $PIDFILE ] && ps h $(cat $PIDFILE 2>/dev/null) >/dev/null 2>/dev/null; then
		
		kill -9 $(cat $PIDFILE 2>/dev/null) >/dev/null 2>/dev/null || true
		running=true
		i=1
		while [ "$running" = "true" -a $i -le 10 ]; do
			echo -n "."
			if ([ -f $PIDFILE ] && ps h $(cat $PIDFILE 2>/dev/null) >/dev/null 2>/dev/null); then
				sleep 1
				i=$(($i+1))
			else
				running=false
			fi
		done
		[ -f $PIDFILE ] && kill -9 $(cat $PIDFILE 2>/dev/null) >/dev/null 2>/dev/null || true
		echo "(done)."
	else
		opsipxeconfd_pids=""
		for pid in $(ps -A | grep opsipxeconfd | sed s'/^\s*//' | cut -d' ' -f1); do
			[ -d "/proc/$pid" -a "$pid" != "$$" ] && opsipxeconfd_pids="$opsipxeconfd_pids $pid"
		done
		if [ "$opsipxeconfd_pids" = "" ]; then
			echo ".   (not running)."
		else
			kill -9 $opsipxeconfd_pids >/dev/null 2>/dev/null || true
			echo "   (done)."
		fi
	fi
	
	[ -f $PIDFILE ] && rm $PIDFILE >/dev/null 2>/dev/null || true
	
}

case "$1" in
	start)
		start
	;;
	
	stop)
		stop
	;;
	
	reload)
		echo -n "Reloading opsi pxe configuration service...   "
		
		if [ -f $PIDFILE ] && ps h  $(cat $PIDFILE) > /dev/null; then
			kill -1 $(cat $PIDFILE) >/dev/null 2>/dev/null
			echo "(done)."
		else
			echo "(not running)."
		fi
	;;
	
	restart|force-reload)
		stop
		sleep 1
		start
	;;
	
	status)
		echo -n "Checking opsi pxe configuration service...   "
		
		if [ -f $PIDFILE ] && ps h  $(cat $PIDFILE) > /dev/null; then
			echo "(running)."
			exit 0
		fi
		echo "(not running)."
		exit 1
	;;
	
	*)
		echo "Usage: /etc/init.d/opsipxeconfd {start|stop|status|reload|restart|force-reload}"
		exit 1
	;;
esac

exit 0

