#!/bin/sh
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

while getopts x:P:p:b:m: opt; do
     case $opt in
	   m)   mode=$OPTARG;;
           x)   DISPLAY=$OPTARG;;
           b)   BASEDIR=$OPTARG;;
	   P)   CONFIG=$OPTARG;;
	   p)   INSTPID=$OPTARG;;
          \?)   logger -t pcscd-SunRay -p user.err \
                   "Bad argument passed to launch script"
                exit 1;;
     esac
done
shift `expr $OPTIND - 1`


if [ -z "$DISPLAY" ]; then
        logger -t pcscd-SunRay -p user.err \
            "Missing DISPLAY (-x parameter ) in launch script"
        exit 1
fi

UTBASE=/etc/opt/SUNWut/basedir
SUNWUT=`${UTBASE}/lib/utprodinfo -r SUNWuto 2>/dev/null`/SUNWut
UTACTION=$SUNWUT/bin/utaction
PCSCD=/usr/lib/smartcard/pcscd
CHKCMD="$UTACTION -x $PCSCD -k -x :$DISPLAY"  

# Clean up utaction script on exit
if [ "$mode" = "STOP" ]; then
	pkill -9 -f "$CHKCMD"
	exit 0
fi

if [ -z "$BASEDIR" ]; then
        logger -t pcscd-SunRay -p user.err \
            "Missing BASEDIR (-b parameter) in launch script"
        exit 1
fi

# Create base directory with secure permissions
chmod -R 700 $BASEDIR

# We never want to have more than one utaction running for a display
pkill -9 -f "$CHKCMD"

# Launch new utaction.  Make sure that the default directory
# belongs to the pcscd instance in question
cd /tmp/SUNWut/pcscd/$DISPLAY
$UTACTION -x "$PCSCD -k -x :$DISPLAY -P $INSTPID" &

exit 0
