#!/bin/ksh -p
#
# $(ID)
#
# Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
#

set -u
########################################################################
#
# Enable/Disable Smart Card Services
#
########################################################################

PATH=/usr/bin:/bin:/usr/sbin:/sbin
export PATH

########################################################################
#
# Operation entry points, one for each keyword.  This is where the
# specific work for this feature happens.
#
########################################################################


#
# disable()
#
disable() {

case $OS in
    SunOS*)
	svcadm disable svc:/application/security/pcscd:default
	if [ "`uname -r`" = 5.10 ]; then
	    svccfg delete -f pcscd
	fi
      
	pkill -9 -f /usr/lib/smartcard/pcscd

	rm -f /var/run/pcscd/fifo/* 
	rm -f /var/run/pcscd/run/* 
    	;;
    Linux*)
	/bin/sh /etc/init.d/pcscd stop

	rm -f /etc/rc.d/rc0.d/K88pcscd
	rm -f /etc/rc.d/rc1.d/K88pcscd
	rm -f /etc/rc.d/rc2.d/S25pcscd
	rm -f /etc/rc.d/rc3.d/S25pcscd
	rm -f /etc/rc.d/rc4.d/S25pcscd
	rm -f /etc/rc.d/rc5.d/S25pcscd
	rm -f /etc/rc.d/rc6.d/K88pcscd
      
	pkill -9 -f /usr/lib/smartcard/pcscd

	rm -f /var/run/pcscd/fifo/* 
	rm -f /var/run/pcscd/run/* 
    	;;
    *)
	print -u2 "unknown OS \"$OS\""
    	;;
esac
}

#
# enable()
#
enable() {
   
case $OS in
    SunOS*)
	typeset SERVICE=pcscd 
	if [ "`uname -r`" = 5.10 ]; then
	    cd /var/svc/manifest/application/security
	    svccfg import pcscd.xml
	    svcadm enable svc:/application/security/pcscd:default
	else
	    #Delete if it is in MASKED state in case
	    /usr/sbin/svccfg -v -s $SERVICE delcust -M
	    #After the manifest file available just restarting manifest-import service
	    #enables the service on S11
	    /usr/sbin/svcadm -v restart manifest-import
	fi
    	;;
    Linux*)
	/bin/sh /etc/init.d/pcscd start

	rm -f /etc/rc.d/rc0.d/K88pcscd
	ln -s ../init.d/pcscd /etc/rc.d/rc0.d/K88pcscd
	rm -f /etc/rc.d/rc1.d/K88pcscd
	ln -s ../init.d/pcscd /etc/rc.d/rc1.d/K88pcscd
	rm -f /etc/rc.d/rc2.d/S25pcscd
	ln -s ../init.d/pcscd /etc/rc.d/rc2.d/S25pcscd
	rm -f /etc/rc.d/rc3.d/S25pcscd
	ln -s ../init.d/pcscd /etc/rc.d/rc3.d/S25pcscd
	rm -f /etc/rc.d/rc4.d/S25pcscd
	ln -s ../init.d/pcscd /etc/rc.d/rc4.d/S25pcscd
	rm -f /etc/rc.d/rc5.d/S25pcscd
	ln -s ../init.d/pcscd /etc/rc.d/rc5.d/S25pcscd
	rm -f /etc/rc.d/rc6.d/K88pcscd
	ln -s ../init.d/pcscd /etc/rc.d/rc6.d/K88pcscd
    	;;
    *)
	print -u2 "unknown OS \"$OS\""
    	;;
esac

}


#########################################
# LOCAL VARIABLES
# They must defined here to resolve the some of parameter resolution
# problem
#########################################
local_variables() {

    return
}


#########################################
#
# Configurable parameter
#
#########################################
# NONE

#########################################
#
# variables used in the shared script utctl-shlib
#
#########################################

DESCR="Enable/Disable Smart Card Services"

########################################################################
#
# Execution starts here
########################################################################

PROGPATH=`dirname $0`
. $PROGPATH/utctl-shlib
