#!/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 --background --make-pid --pidfile /var/run/${NAME}.pid --exec ${DAEMON} -- -f ${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)
		"${0}" stop
		sleep 1
		"${0}" start
		;;

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

	*)
		echo "Usage: ${NAME} {start|stop|restart|force-reload|status}" >&2
		exit 1
		;;
esac

exit 0
