#!/bin/sh
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"@(#)postinstall.tmpl	1.6	04/09/13 SMI"
#

#
# Function: getKeyValue
#
# Try to get key value pair from the given line.
# The line to be parsed should conform to the format loadable
# by the Java Properties object.  
# 
# The variables "key" and "value" will be set if successful.
# 0 will be returned if a key/value pair is found. 
# 1 will be returned otherwise.
#
getKeyValue() {

	key=""
	value=""

	if [ $# != 1 ] ; then
		#no argument to be parsed
		return 1
	fi

	if [ "XX$1" = "XX" ] ; then
		#empty line, nothing to be parsed
		return 1
	fi

	#first, remove any leading and trailing blank spaces in the line
	line=`echo "$1"|sed 's/^[ 	]*//'`

	#Check to see if it is a comment line.  Lines starting
	#with '#' or '!' are comment lines
	echo "$line"|egrep "^[#!]" > /dev/null 2>& 1

	if [ "XX$?" = "XX0" ] ; then
		#It's a comment line
		return 1
	fi

	#Check to make sure the line have some kind of delimiter
	echo "$line"|egrep "[ 	=:]" > /dev/null 2>& 1
	if [ "XX$?" != "XX0" ] ; then
		#No delimiter in line, return error
		return 1
	fi

	key=`echo "$line" | sed \
		's/^\([^ 	=:]*\)[ 	=:].*$/\1/'`

	if [ "XX$key" != "XX$line" ] ; then
		value=`echo "$line" | sed \
			's/^[^ 	=:]*[ 	]*[=:]\{0,1\}[ 	]*\(.*\)$/\1/'`
		return 0
	fi

	#Should never get here, but just in case.
	return 1
}

#
# If desktop properties are found in opencard.properties
# because this is an upgrade, move the properties from
# /etc/smartcard/opencard.properties to /etc/smartcard/desktop.properties.
#
ETCSCDIR=$PKG_INSTALL_ROOT/etc/smartcard
OP=$ETCSCDIR/opencard.properties
DP=$ETCSCDIR/desktop.properties

if [ ! -f $OP ] ; then
	exit 1
fi

if [ ! -f $DP ] ; then
	exit 1
fi

if [ ! -d /tmp ] ; then
	exit 1
fi

TMP_OP=/tmp/op.$$
TMP_OP2=/tmp/op2.$$
RESULT_OP=/tmp/op3.$$
RESULT_DP=/tmp/dp.$$
TMP_DP=/tmp/dp2.$$

#
# First, join all the lines that have the '\' continuation character
#
nawk '{ \
	while (match($0, /^([^\\]|\\.)*\\$/ )) { \
		getline line \
		sub(/.$/, line, $0) \
	} \
	print $0 \
}' ${OP} > ${TMP_OP2}

#
# Get list of desktop properties to be processed
#
egrep "^[ 	]*ocf\.client\.dtsession\.root" ${TMP_OP2} > ${TMP_OP}

cp ${DP} ${RESULT_DP}
if [ "XX$?" != "XX0" ] ; then
	#can't continue if failed to copy original desktop.properties file.
	rm -f ${TMP_OP} ${TMP_OP2}
	exit 1
fi

cp ${TMP_OP2} ${RESULT_OP}
if [ "XX$?" != "XX0" ] ; then
	#can't continue if failed to copy original opencard.properties file.
	rm -f ${TMP_OP} ${TMP_OP2} ${RESULT_DP}
	exit 1
fi

cat ${TMP_OP} | \
while read opval ; do

	getKeyValue "$opval"

	if [ "XX$?" = "XX1" ] ; then
		#Something is wrong with line.  Don't put it in new file
		continue
	fi

	oldKey="$key"

	#Successful in finding key-value pair.
	#construct the new key.  Replace "ocf.client.dtsession.root" with the
	#word "desktop"
	key=`echo $key|sed 's/^ocf\.client\.dtsession\.root/desktop/'`

	#check if key is in desktop.properties, if so, remove it before adding
	egrep "^[ 	]*$key" ${RESULT_DP} > /dev/null 2>& 1
	if [ "XX$?" = "XX0" ] ; then
		#line exists in file, need to remove
		egrep -v "^[ 	]*$key" ${RESULT_DP} > ${TMP_DP}
		if [ "XX$?" = "XX0" ] ; then
			cp ${TMP_DP} ${RESULT_DP}
		fi
	fi
	echo "$key=$value" >> ${RESULT_DP}

	# Remove opencard.properties entry that are 
	# moved to desktop.properties file
	egrep -v "^[ 	]*$oldKey" ${RESULT_OP} > ${TMP_OP2}
	if [ "XX$?" = "XX0" ] ; then
		cp ${TMP_OP2} ${RESULT_OP}
	fi
done

#Only copy the files if they have been modified.  Having the temp
#desktop.properties file means something has been modified.
if [ -f ${TMP_DP} ] ; then
	#Remove any dtlogin entries from opencard.properties
	egrep -v "^[ 	]*ocf\.client\.dtlogin\.root" \
		${RESULT_OP} > ${TMP_OP2}
	if [ "XX$?" = "XX0" ] ; then
		cp ${TMP_OP2} ${RESULT_OP}
	fi

	cp ${RESULT_DP} ${DP}
	cp ${RESULT_OP} ${OP}

	if [ "${PKG_INSTALL_ROOT:-/}" = "/" ] ; then
	    # Note "svcadm restart" cannot be used due to 5079309.
	    /usr/sbin/svcadm disable network/rpc/ocfserv
	    /usr/sbin/svcadm enable network/rpc/ocfserv
	fi
fi

# cleanup
rm -f ${TMP_OP} ${TMP_OP2} ${RESULT_DP} ${RESULT_OP} ${TMP_DP}


##
## Update opencard.properties with IFD handler for internal reader
##

/bin/cp -p $OP $OP.noifd

if [ $? -ne 0 ]; then
    ## Failed to create a temporary file.
    /bin/rm -f $OP.noifd
    exit 1
fi


# Join the lines that have the '\' continuation character

nawk '{ \
	while (match($0, /^([^\\]|\\.)*\\$/ )) { \
		getline line \
		sub(/.$/, line, $0) \
	} \
	print $0 \
}' $OP.noifd > $OP.noifd2

if [ $? -ne 0 ]; then
    /bin/rm -f $OP.noifd $OP.noifd2
    exit 1
fi



# Check if there is an entry to be updated.

OLD_TERMINAL=com.sun.opencard.terminal.scm.SCMI2c.SCMI2cCardTerminalFactory

/bin/egrep -s "$OLD_TERMINAL" $OP.noifd2

if [ $? -ne 0 ]; then
    # Entry does not exist.  Do nothing.
    /bin/rm -f $OP.noifd $OP.noifd2

    exit 0
fi



# Copy to create a new file with the same file attributes as original.

/bin/cp -p $OP $OP.new

if [ $? -ne 0 ]; then
    /bin/rm -f $OP.noifd $OP.noifd2 $OP.new
    exit 1
fi


# Replace the card terminal factory and 
# append the IFD handler name to the entry

NEW_TERMINAL=com.sun.opencard.terminal.IFDCardTerminal.IFDCardTerminalFactory
IFD_HANDLER=/usr/lib/smartcard/ifdh_scmi2c.so

/bin/sed -e "s+$OLD_TERMINAL\([^ ]*\)+$NEW_TERMINAL\1|$IFD_HANDLER+" \
	$OP.noifd2 > $OP.new

if [ $? -ne 0 ]; then
    /bin/rm -f $OP.noifd $OP.noifd2 $OP.new
    exit 1
fi


# We are done with this temporary file.

/bin/rm -f $OP.noifd2


# Replace the configuration file with the new one.

/bin/mv -f $OP.new $OP

if [ $? -ne 0 ]; then
    /bin/rm -f $OP.noifd $OP.new
    exit 1
fi

if [ "${PKG_INSTALL_ROOT:-/}" = "/" ]; then
    # Note "svcadm restart" cannot be used due to 5079309.
    /usr/sbin/svcadm disable network/rpc/ocfserv
    /usr/sbin/svcadm enable network/rpc/ocfserv
fi

exit 0
