#!/bin/sh

### BEGIN INIT INFO
# Provides:		tcsd
# Required-Start:	$local_fs
# Required-Stop:	$local_fs
# Should-Start:
# Should-Stop:
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	starts tcsd
# Description:		tcsd belongs to the TrouSerS TCG Software Stack
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/tcsd
NAME=tcsd
DESC="Trusted Computing daemon"

test -x $DAEMON || exit 0

set -e

case "${1}" in
	start)
		echo -n "Starting $DESC: "
		if [ ! -e /dev/tpm* ]
		then
			echo "device driver not loaded, aborting."
			exit 0
		fi

		start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
		echo "$NAME."
		;;

	stop)
		echo -n "Stopping $DESC: "
		start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid --oknodo --exec $DAEMON
		echo "$NAME."
		;;

	restart|force-reload)
		echo -n "Restarting $DESC: "
		start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid --oknodo --exec $DAEMON
		sleep 1
		if [ ! -e /dev/tpm* ]
		then
			echo "device driver not loaded, aborting."
			exit 0
		fi
		start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
		echo "$NAME."
		;;

	status)
		if [ -f /var/run/$NAME.pid ]
		then
			echo "$NAME is running."
		else
			echo "$NAME is not running."
			exit 1
		fi
		;;

	*)
		N=/etc/init.d/$NAME
		echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
		exit 1
		;;
esac

exit 0
