#!/bin/bash
#

# $1: full path to the installation package
# $2: full path to the installation destination
# $3: mountpoint of the destination volume
# $4: root directory / for the current System folder

#echo "Start postinstall script"

# if the script to enable debug output in pcscd is available, run it
ENABLEDEBUGOUTPUTSCRIPT=`dirname "$0"`/EnableDebugPCSCD.sh
if [ -f "${ENABLEDEBUGOUTPUTSCRIPT}" ]; then
  "${ENABLEDEBUGOUTPUTSCRIPT}"
fi

# fix permissions
# /usr/sbin/chown root:wheel "/"
# /usr/sbin/chown root:wheel "/usr"
# /usr/sbin/chown root:wheel "/usr/libexec"
# /usr/sbin/chown root:wheel "/usr/libexec/SmartCardServices"
# /usr/sbin/chown root:wheel "/usr/libexec/SmartCardServices/drivers"
# /usr/sbin/chown root "/Library"

#/usr/sbin/chown -R root:wheel "/usr/libexec/SmartCardServices/drivers/libifd-cyberjack.bundle"
#/usr/sbin/chown -R root:wheel "/usr/local/libexec/SmartCardServices/drivers/libifd-cyberjack.bundle"

#cp /usr/libexec/SmartCardServices/drivers/libifd-cyberjack.bundle/Contents/Resources/cyberjack.conf.default /etc

## check if the ifd-ccid.bundle is there, in this case remove the cyberjack info
#if [ -d "/usr/libexec/SmartCardServices/drivers/ifd-ccid.bundle" ]; then
#	"$1/Contents/Resources/RemoveFromCCID"
#fi


#if [ $(/usr/bin/sw_vers -productVersion | awk -F '.' '{ print $1 }') -ge 10  -a  $(/usr/bin/sw_vers -productVersion | awk -F '.' '{ print $2 }') -ge 5 ]; then 
#	# install LaunchAgent
#	mkdir -p /Library/LaunchAgents &&
#	cp "${1}/Contents/Resources/de.reiner-sct.cyberjack.gui.plist" /Library/LaunchAgents/ &&
#	chmod 444 /Library/LaunchAgents/de.reiner-sct.cyberjack.gui.plist &&
#	chown root:wheel /Library/LaunchAgents/de.reiner-sct.cyberjack.gui.plist &&
#	(
#	# make sure LaunchAgents are running
#	LAUNCHAGENTSFAILED=0
#	LOGGEDINUSERS=$(/bin/ps -x -a -o user,pid,gid,command | grep loginwindow | grep -v grep | awk '{print $1":"$3":"$2;}')
#	for u in ${LOGGEDINUSERS}; do
#		USERNAME=$(echo "$u" | awk -F ':' '{print $1}')
#		USERGROUP=$(echo "$u" | awk -F ':' '{print $2}')
#		BSPID=$(echo "$u" | awk -F ':' '{print $3}')
#		if [ "${USERNAME}" != "root"  -a  "${USERNAME}" != "loginwindow" ]; then
#			echo "launching CyberJack launch agent for user: ${USERNAME}, group ${USERGROUP}, bs_pid ${BSPID}"
#			/bin/launchctl bsexec ${BSPID} /usr/sbin/chroot -u ${USERNAME} -g ${USERGROUP} / /bin/launchctl load -S Aqua /Library/LaunchAgents/de.reiner-sct.cyberjack.gui.plist || ((++LAUNCHAGENTSFAILED))
#		fi
#	done
#	if [ ${LAUNCHAGENTSFAILED} -gt 0 ]; then
#		echo "failed to launch some launch agents - PIN dialogs may not work until user logs out and in again" >&2
#		# FIXME: should we display an alert to the user?
#	fi
#	)
#	
#	# Smartcard Services installed as launchd daemon
#	# check if the Smartcard Services are running. If so then restart them so our ifd-handler is recognized
##	if [ -d "/tmp/pcsc" ]; then
##	  echo "Restarting Smartcard Services"
##	  SystemStarter restart SmartcardServices
##	fi
#fi

exit 0
