#!/bin/sh
#
# pcscd		PC/SC Smartcard daemon
#
# chkconfig:	2345 12 88
#
# description: The PC/SC smart card daemon is a resource manager for the \
#              PC/SC lite and Musclecard frameworks.  It coordinates \
#              communications with smart card readers, smart cards, and \
#              cryptographic tokens that are connected to the system.
#
# processname: pcscd
# config:      /etc/reader.conf.d
#
# Note!  pcscd should be started after pcmcia, and shut down before it
# for smooth experience with PCMCIA readers.
#
# $Id$

# Source function library
. /etc/rc.d/init.d/functions

umask 077

# Set defaults
LOGLEVEL=""
PCSCD_OPTIONS=""

# Get service config - may override defaults
[ -f /etc/sysconfig/pcscd ] && . /etc/sysconfig/pcscd

pidfile=/var/run/pcscd/pcscd.pid

start() {
	if [ -f /var/lock/subsys/pcscd ]; then
		msg_already_running "PC/SC smart card daemon"
		return
	fi

	local logopt
	case "$LOGLEVEL" in
	debug|info|error|critical)
		logopt=--$LOGLEVEL
	;;
	esac

	msg_starting "PC/SC smart card daemon"
	daemon /usr/sbin/pcscd $logopt $PCSCD_OPTIONS
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/pcscd
}

stop() {
	if [ ! -f /var/lock/subsys/pcscd ]; then
		msg_not_running "PC/SC smart card daemon"
		return
	fi

	msg_stopping "PC/SC smart card daemon"
	killproc --pidfile $pidfile pcscd
	rm -f /var/lock/subsys/pcscd
}

condrestart() {
	if [ ! -f /var/lock/subsys/pcscd ]; then
		msg_not_running "PC/SC smart card daemon"
		RETVAL=$1
		return
	fi

	stop
	start
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  force-reload)
	# pcscd doesn't support reloading on HUP
	condrestart 7
	;;
  status)
	status --pidfile $pidfile pcscd
	RETVAL=$?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
