#!/bin/sh

### BEGIN INIT INFO
# Provides:          ppds
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Starts Raspberry-pi high level peripherial driver
### END INIT INFO

# Defaults
RUN_MODE="daemons"

PIDDIR=/var/run/ppds
PPDSPID=$PIDDIR/ppds.pid

# clear conflicting settings from the environment
unset TMPDIR

# See if the daemons are there
test -x /usr/sbin/ppds || exit 0

. /lib/lsb/init-functions

case "$1" in
	start)
		log_daemon_msg "Starting ppds"
		# Make sure we have our PIDDIR, even if it's on a tmpfs
		install -o root -g root -m 755 -d $PIDDIR

		if [ "$RUN_MODE" != "inetd" ]; then
			log_progress_msg "ppds"
			if ! start-stop-daemon --start  --oknodo --pidfile $PPDSPID --exec /usr/sbin/ppds -- -D; then
				log_end_msg 1
				exit 1
			fi
		fi

		log_end_msg 0
		;;
	stop)
		log_daemon_msg "Stopping ppds"
		log_progress_msg "ppds"

		start-stop-daemon --stop --quiet --pidfile $PPDSPID
		# Wait a little and remove stale PID file
		sleep 1
		if [ -f $PPDSPID ] && ! ps h `cat $PPDSPID` > /dev/null
		then
			# Stale PID file (ppds was succesfully stopped)
			rm -f $PPDSPID
		fi

		log_end_msg 0

		;;
		
	restart|force-reload)
		$0 stop
		sleep 1
		$0 start
		;;
        status)
		status="0"
		if [ "$RUN_MODE" != "inetd" ]; then
			status_of_proc -p $PPDSPID /usr/sbin/ppds ppds || status=$?
		fi
		exit $status
		;;
	*)
		echo "Usage: /etc/init.d/ppds {start|stop|restart|force-reload|status}"
		exit 1
		;;
esac

exit 0
