#! /bin/sh

### BEGIN INIT INFO
# Provides:          openct
# Required-Start:    $syslog $remote_fs
# Required-Stop:     $syslog $remote_fs
# Should-Start:      $local_fs
# Should-Stop:       $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: smart card terminal framework
# Description:       Initialize the OpenCT smart card terminal
#                    framework.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/openct-control
STATUS_DIR="/var/run/openct"
STATUS_FILE="$STATUS_DIR/status"
NAME=OpenCT
DESC="smart card terminal framework"

. /etc/rc.status

rc_reset

test -x $DAEMON || exit 5

# create the directory for our status and socket files,
# if it does not exist.
if ! test -e "$STATUS_DIR"
then
	mkdir "$STATUS_DIR"

	# maybe you also want to set owner ship and permissions here.
	# this example would assign the directory to a group "scard"
	# and set permissions so only users in that group can access
	# smart card readers via openct.
	chown scard:scard "$STATUS_DIR"
	chmod 0755 "$STATUS_DIR"
fi

case "$1" in
  start)
	echo -n "Starting $DESC: $NAME"
	$DAEMON init
	rc_status -v
	;;
  stop)
	echo -n "Stopping $DESC: $NAME"
	if [ -f $STATUS_FILE ]; then
		$DAEMON shutdown
		rm -f $STATUS_FILE
	fi
	rc_status -v
	;;
  reload)
	;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	echo -n "Restarting $DESC: $NAME"
	if [ -f $STATUS_FILE ]; then
		$DAEMON shutdown
		rm -f $STATUS_FILE
	fi
	sleep 0.1
	$DAEMON init
	rc_status -v
	;;
  try-restart)
	$0 status >/dev/null &&  $0 restart
	rc_status
	;;
  status)
	echo -n "Checking for $DESC: $NAME"
	if openct-tool list >/dev/null 2>&1; then
		rc_failed 0
	else
		rc_failed 3
	fi
	rc_status -v
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|status|try-restart|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
