#!/bin/sh
#
# ident "@(#)$Id$ Oracle"
#
# Portions Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
#

#
# pcscd        Starts the pcscd Daemon
#
# chkconfig:   2345 25 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
#
### BEGIN INIT INFO
# Provides: pcscd
# Should-Start: $syslog openct
# Should-Stop: $syslog openct
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: PC/SC smart card daemon
# 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.
### END INIT INFO
#
# Note!  pcscd should be started after pcmcia, and shut down before it
# for smooth experience with PCMCIA readers.

. /etc/init.d/functions

umask 077

PCSCD=/usr/lib/smartcard/pcscd
prog=$(basename $PCSCD)
lockfile=/var/lock/subsys/$prog
PIDFILE=/var/run/pcscd/pcscd.pid

# Source config
if [ -f /etc/sysconfig/pcscd ] ; then
    . /etc/sysconfig/pcscd
fi

start() {
    retval=-1
    if [ -f "$PIDFILE" ]; then
	    killproc -p $PIDFILE $prog
	    rm -f $PIDFILE
    fi
    if [ -f "$PCSCD" ]; then 
	    echo -n $"Starting PC/SC smart card daemon: "
	    daemon --pidfile $PIDFILE $PCSCD -L
	    retval=$?
	    if [ $retval -eq 0 ]
	    then
		touch $lockfile
		echo_success
	    else
		echo_failure
	    fi
    else
	    exit -1
    fi
    return $retval
}
stop() {
    echo -n $"Stopping PC/SC smart card daemon: "
    killproc -p $PIDFILE $prog
    retval=$?
    pkill -f "$PCSCD -L"
    pkill -f "$PCSCD -I"
    pkill -f "utaction -x $PCSCD"
    if [ $retval -eq 0 ]
    then
	rm -f $lockfile
	echo_success
    else
	echo_failure
    fi
    return $retval
}


case "$1" in
    start|stop)
        $1
	exit $?
        ;;
    restart|reload|force-reload)
	stop
	start
	exit $?
        ;;
    status)
	pgrep -l -f "$PCSCD "
        status $prog
        ;;
    debug)  
	rm -f ${PIDFILE}
	$PCSCD -f -d debug -L
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
        exit 2
esac
exit 0
