#!/bin/sh

# configure the pkg installer as desired

PKGPATH=''

# mac options: bcdfghklorstvz
# win options: bcdeghijkmnpqrstuvwxyz

show_usage() {
	echo "usage: k2clientconfig options K2Client.pkg"
	echo "  options:"
	echo "  -d             display current settings (other options are ignored)"
	echo ""
	echo "  -h <host>      set IP address or DNS name of KeyServer to <host>"
	echo ""
	echo "  -s {0|1|2|3}   interface level displayed when the mpkg installer is run"
	echo "      0: user can change settings"
	echo "      1: user can see settings but cannot change them"
	echo "      2: user cannot see any settings besides standard pkg interface"
	echo "         use this option if you are distributing the pkg with Remote Desktop"
	echo "         to prevent anything from appearing on the client computer"
	echo "      3: same as 2"
	echo ""
	echo "  -g {yes|no}    override current ka address with address specified by pkg"
	echo "      yes: address specified by pkg will be used"
	echo "      no: current ka address will be used if present"
	echo ""
	echo "  -c {yes|no|maybe}    install KeyCheckout"
	echo "      yes: KeyCheckout will be installed unless user chooses not to"
	echo "      no: user cannot install KeyCheckout"
	echo "      maybe: KeyCheckout will not be installed unless user chooses to"
	echo ""
	echo "  -t {yes|no}    allow installation of KeyVerify"
	echo "      yes: KeyVerify will be installed unless user chooses not to"
	echo "      no: user cannot install KeyVerify"
	echo ""
	echo "  -p {yes|no}    allow installation of TeamDynamix Client Preference Pane"
	echo "      yes: Pane will be installed unless user chooses not to"
	echo "      no: user cannot install TeamDynamix Client Preference Pane"
	echo ""
	echo "  -k {yes|no}    kill TeamDynamix Client before install"
	echo "      yes: TeamDynamix Client will be killed (quit) before the install begins"
	echo "      no: TeamDynamix Client will not be killed (quit) before the install begins"
	echo ""
	echo "  -r {yes|no}    run TeamDynamix Client after install"
	echo "      yes: TeamDynamix Client will be started after install completes"
	echo "      no: TeamDynamix Client will not be started after install completes"
	echo ""
	echo "  -b {yes|no}    reboot after install"
	echo "      yes: prompt for a reboot after install"
	echo "      no: do not prompt for a reboot after install"
	echo ""
	echo "  -v name[=value]"
	echo "      set a specific named plist preference to a custom initial value"
	echo "      name is the preference name (e.g., 'minimize')"
	echo "      value is the preference value ('1' if omitted)"
	echo ""
	echo "  -l {yes|no}    lock TeamDynamix Client settings"
	echo "      yes: after install, TeamDynamix Client settings will be locked to users"
	echo "      no: after install, TeamDynamix Client settings will not be locked to users"
	echo ""
	echo "  -f {0|1|2|3}   value used for computer name"
	echo "      0: computer name defined by user (in Sharing system preference panel)"
	echo "      1: local computer host name as returned by gethostname"
	echo "      2: canonical host name, as retrieved from DNS"
	echo "      3: first component of canonical host name (i.e. 'myhost' instead of 'myhost.domain.org')"
	echo ""
	echo "  -z {user|short|comp} source of value used as login name"
	echo "      user: TeamDynamix Client will use user name as login name"
	echo "      short: TeamDynamix Client will use short user name as login name"
	echo "      comp: TeamDynamix Client will use computer name as login name"
	echo ""
	echo "  -x {yes|no}    by default, quarantine is removed from the mpkg when any other change is made"
	echo "      yes: remove quarantine attribute even if no other options are specified"
	echo "      no: do not remove quarantine attribute"
	echo ""
	echo "example configuration for use with Remote Desktop:"
	echo "  k2clientconfig -h 192.168.0.16 -s 2 -g yes"
}

get_plist_string() {
	defaults read "$1/${2%.plist}" $3
#	FOUND=`grep -A 1 "<key>$2</key>" "$1" | sed -e "1d" -e "s/\(.*\<string\>\)\(.*\)\(\<\/string\>.*\)/\2/"`
#	echo $FOUND
}

change_plist_string() {
	defaults write "$1/${2%.plist}" $3 "$4"
	chmod 664 "$1/$2"
#	if [ -f "$1/$2" ]; then
#		sed -e "/\<key\>$3/,/$/s/\(\<string\>\)\(.*\)\(\<\/string\>\)/\1$4\3/" "$1/$2" > "$1/$2.new"
#
#		if [ "$?" == "0" ] && [ -f "$1/$2.new" ]; then
#			rm -f "$1/$2"
#			mv -f "$1/$2.new" "$1/$2"
#			chmod 555 "$1/$2"
#			echo "Changed $3 in $1/$2 to $4"
#		else
#			echo "Couldn't change $3 in $1/$2 !"
#		fi
#	else
#		echo "$1/$2 doesn't exist. couldn't change $3!"
#	fi
}


get_script_var() {
	FOUND=`grep "$3=" "$1/$2" | sed -e "s/\($3=\)\(.*\)/\2/"`
	echo $FOUND
}

change_script_var() {
	if [ -f "$1/$2" ]; then
		sed -e "s/\($3=\)\(.*\)/\1$4/" "$1/$2" > "$1/$2.new"

		if [ "$?" == "0" ] && [ -f "$1/$2.new" ]; then
			rm -f "$1/$2"
			mv -f "$1/$2.new" "$1/$2"
			chmod 555 "$1/$2"
			# echo "Changed $3 in $2 to $4"
		else
			echo "Couldn't change $3 in $1/$2 !"
		fi
	else
		echo "$1/$2 doesn't exist. couldn't change $3!"
	fi
}

get_choice_var() {
	FOUND=`grep "choice id=\"$3\"" "$1/$2" | sed -e "s/\(^.*$4=\"\)\([^\"]*\)\".*$/\2/"`
	echo $FOUND
}

change_choice_var() {
	if [ -f "$1/$2" ]; then
		sed -e "/choice id=\"$3\"/ { /$4=/! { s/\(title=\"\)\([^\"]*\)\(\"\)/\1\2\3 $4=\"$5\"/; }; }" -e "/choice id=\"$3\"/ { s/\($4=\"\)\([^\"]*\)\(\"\)/\1$5\3/; }" "$1/$2" > "$1/$2.new"

		if [ "$?" == "0" ] && [ -f "$1/$2.new" ]; then
			rm -f "$1/$2"
			mv -f "$1/$2.new" "$1/$2"
			chmod 644 "$1/$2"
			# echo "Changed $4 (for $3) in $2 to $5"
		else
			echo "Couldn't change $4 (for $3) in $1/$2 !"
		fi
	else
		echo "$1/$2 doesn't exist. couldn't change $4!"
	fi
}

get_pkgref_var() {
	FOUND=`grep "pkg-ref id=\"com\.sassafras\.pkg\.$3\"" "$1/$2" | grep "$4" | sed -e "s/\(^.*$4=\"\)\([^\"]*\)\".*$/\2/"`
	echo $FOUND
}

change_pkgref_var() {
	if [ -f "$1/$2" ]; then
		sed -e "/pkg-ref id=\"com\.sassafras\.pkg\.$3\"/ { s/\($4=\"\)\([^\"]*\)\(\"\)/\1$5\3/; }" "$1/$2" > "$1/$2.new"

		if [ "$?" == "0" ] && [ -f "$1/$2.new" ]; then
			rm -f "$1/$2"
			mv -f "$1/$2.new" "$1/$2"
			chmod 644 "$1/$2"
			# echo "Changed $4 (for $3) in $2 to $5"
		else
			echo "Couldn't change $4 (for $3) in $1/$2 !"
		fi
	else
		echo "$1/$2 doesn't exist. couldn't change $4!"
	fi
}

get_js_prop() {
	PROPSTR=$(echo "PROP_$3" | tr 'a-z' 'A-Z')
	FOUND=`grep "$PROPSTR:" "$1/$2" | sed -e "s/^[[:space:]]*$PROPSTR:[[:space:]]*\([^,]*\),*$/\1/"`
	echo $FOUND
}

change_js_prop() {
	if [ -f "$1/$2" ]; then
		PROPSTR=$(echo "PROP_$3" | tr 'a-z' 'A-Z')
		sed -e "s/^\([[:space:]]*$PROPSTR:[[:space:]]*\)[^,]*\(,*\)/\1$4\2/" "$1/$2" > "$1/$2.new"

		if [ "$?" == "0" ] && [ -f "$1/$2.new" ]; then
			rm -f "$1/$2"
			mv -f "$1/$2.new" "$1/$2"
			chmod 644 "$1/$2"
			# echo "Changed $4 (for $3) in $2 to $5"
		else
			echo "Couldn't change $4 (for $3) in $1/$2 !"
		fi
	else
		echo "$1/$2 doesn't exist. couldn't change $4!"
	fi
}

change_choice() {
	if [ "$1" != "" ]; then
		if [ $KA_NEWPKG -ne 0 ]; then
			if [ "$1" == "no" ]; then
				change_js_prop "$EXTRACTDIR/pkg/" "Distribution" "$2" "0"
			elif [ "$1" == "yes" ]; then
				change_js_prop "$EXTRACTDIR/pkg/" "Distribution" "$2" "1"
			elif [ "$1" == "maybe" ]; then
				change_js_prop "$EXTRACTDIR/pkg/" "Distribution" "$2" "2"
			fi
		else
			if [ "$1" == "no" ]; then
				change_choice_var "$EXTRACTDIR/pkg/" "Distribution" "$2" "start_enabled" "false"
				change_choice_var "$EXTRACTDIR/pkg/" "Distribution" "$2" "start_selected" "false"
				change_choice_var "$EXTRACTDIR/pkg/" "Distribution" "$2" "start_visible" "false"
			elif [ "$1" == "yes" ]; then
				change_choice_var "$EXTRACTDIR/pkg/" "Distribution" "$2" "start_enabled" "true"
				change_choice_var "$EXTRACTDIR/pkg/" "Distribution" "$2" "start_selected" "true"
				change_choice_var "$EXTRACTDIR/pkg/" "Distribution" "$2" "start_visible" "true"
			elif [ "$1" == "maybe" ]; then
				change_choice_var "$EXTRACTDIR/pkg/" "Distribution" "$2" "start_enabled" "true"
				change_choice_var "$EXTRACTDIR/pkg/" "Distribution" "$2" "start_selected" "false"
				change_choice_var "$EXTRACTDIR/pkg/" "Distribution" "$2" "start_visible" "true"
			fi
		fi
	fi
}

get_choice() {
	if [ $KA_NEWPKG -ne 0 ]; then
		CHOICE_OLD=`get_js_prop "$EXTRACTDIR/pkg/" "Distribution" "$1"`
		if [ "$CHOICE_OLD" == "0" ]; then
			echo "no"
		elif [ "$CHOICE_OLD" == "1" ]; then
			echo "yes"
		else
			echo "maybe"
		fi
	else
		CHOICE_OLD=`get_choice_var "$EXTRACTDIR/pkg/" "Distribution" "$1" "start_enabled"`
		if [ "$CHOICE_OLD" == "false" ]; then
			echo "no"
		else
			CHOICE_OLD=`get_choice_var "$EXTRACTDIR/pkg/" "Distribution" "$1" "start_selected"`
			if [ "$CHOICE_OLD" == "false" ]; then
				echo "maybe"
			else
				echo "yes"
			fi
		fi
	fi
}

change_restart() {
	if [ "$1" != "" ]; then
		if [ $KA_NEWPKG -ne 0 ]; then
			O_BOOT="1"
			if [ "$1" == "no" ]; then
				O_BOOT="0"
			fi
			change_js_prop "$EXTRACTDIR/pkg/" "Distribution" "RESTART" "$O_BOOT"
		else
			O_BOOT="RequireRestart"
			if [ "$1" == "no" ]; then
				O_BOOT="None"
			fi
			change_pkgref_var "$EXTRACTDIR/pkg/" "Distribution" "KeyAccess" "onConclusion" "$O_BOOT"
		fi
	fi
}

get_restart() {
	if [ $KA_NEWPKG -ne 0 ]; then
		O_BOOT=`get_js_prop "$EXTRACTDIR/pkg/" "Distribution" "RESTART"`
		if [ "$O_BOOT" == "0" ]; then
			echo "no"
		else
			echo "yes"
		fi
	else
		O_BOOT=`get_pkgref_var "$EXTRACTDIR/pkg/" "Distribution" "KeyAccess" "onConclusion"`
		if [ "$O_BOOT" == "RequireRestart" ]; then
			echo "yes"
		else
			echo "no"
		fi
	fi
}


pkg_extract() {
	xar -x -C $2 -f $1
}

pkg_compress() {
	pushd $2 > /dev/null
	xar --compression none -c -f $3/tmp.pkg .
	popd > /dev/null
	mv -f $3/tmp.pkg $1
}

scripts_extract() {
	pushd $2 > /dev/null
	cp $1 ./Scripts.gz
	gunzip ./Scripts.gz
	cat Scripts | cpio -i 2> /dev/null
	rm Scripts
	popd > /dev/null
}

scripts_compress() {
	pushd $2 > /dev/null
	find . | cpio -o -R root:wheel 2> /dev/null | gzip -c -n > $1
	popd > /dev/null
}

OPT_D=''
OPT_H=''
OPT_I=''
OPT_S=''
OPT_G=''
OPT_C=''
OPT_T=''
OPT_P=''
OPT_K=''
OPT_R=''
OPT_B=''
OPT_L=''
OPT_F=''
OPT_V=''
OPT_Z=''
OPT_X=''

while getopts 'dh:s:g:c:t:p:k:r:b:l:f:v:z:x:i:' OPTLETTER
do
#	echo "option '$OPTLETTER' is '$OPTARG'"
	case "$OPTLETTER" in
	"d")	OPT_D="1"
				;;
	"h")	OPT_H="$OPTARG"
				;;
	"s")	OPT_S="$OPTARG"
				;;
	"g")	OPT_G="$OPTARG"
				;;
	"c")	OPT_C="$OPTARG"
				;;
	"t")	OPT_T="$OPTARG"
				;;
	"p")	OPT_P="$OPTARG"
				;;
	"k")	OPT_K="$OPTARG"
				;;
	"r")	OPT_R="$OPTARG"
				;;
	"b")	OPT_B="$OPTARG"
				;;
	"l")	OPT_L="$OPTARG"
				;;
	"f")	OPT_F="$OPTARG"
				;;
	"i")	OPT_I="$OPTARG"
				;;
	"v")
		if [ "x$OPT_V" == "x" ]; then
			OPT_V="$OPTARG"
		else
			OPT_V="$OPT_V ~~ $OPTARG"
		fi
				;;
	"z")	OPT_Z="$OPTARG"
				;;
	"x")	OPT_X="$OPTARG"
				;;
	?)		echo "unrecognized option: '$OPTLETTER'"
				show_usage
				echo "quitting"
				exit 1
				;;
	esac
done

opts_count=`expr $# - $OPTIND`
# echo "# $# OPTIND $OPTIND opts_count $opts_count"
if [ $opts_count -ne 0 ]
then
	echo "missing file name or too many options"
	show_usage
	exit 1
fi
shift `expr $OPTIND - 1`
# echo "remaining args: '$*'"
PKGPATH=$*
if [ ! -f $PKGPATH ]; then
	echo "file $PKGPATH doesn't exist"
	show_usage
	exit 1
fi

if [ "$OPT_X" != "no" ]; then
	xattr -r -d com.apple.quarantine "$PKGPATH"
fi

EXTRACTDIR=`mktemp -d -t k2cc`
if [ $(ls -A $EXTRACTDIR | wc -l) -ne 0 ]; then
	echo "error - temp directory $EXTRACTDIR is not empty! aborting!"
	exit 1
fi

mkdir $EXTRACTDIR/pkg
mkdir $EXTRACTDIR/s_ka
pkg_extract $PKGPATH $EXTRACTDIR/pkg
KA_VERSION=`grep "com.sassafras.KeyAccess\".*CFBundleVersion" $EXTRACTDIR/pkg/KeyAccess.pkg/PackageInfo | sed -e "s/\(^.*CFBundleVersion=\"\)\([^\"]*\)\(\".*$\)/\2/"`
if [[ "$KA_VERSION" > "7.4.1.0" ]]; then
	KA_NEWPKG=1
else
	KA_CUSTOM=`get_js_prop "$EXTRACTDIR/pkg/" "Distribution" "CUSTOM"`
	if [ "$KA_CUSTOM" != "" ]; then
		KA_NEWPKG=1
	else
		KA_NEWPKG=0
	fi
fi

echo "Installer for TeamDynamix Client $KA_VERSION"
scripts_extract $EXTRACTDIR/pkg/KeyAccess.pkg/Scripts $EXTRACTDIR/s_ka

if [ "$OPT_D" != "1" ]; then
	# only make changes if -d isn't specified
	if [ "$OPT_H" != "" ]; then
		change_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" KSAddress "$OPT_H"
	fi

	if [ "$OPT_S" != "" ]; then
		O_PROMPT="1"
		O_NOCHANGE="0"
		if [ "$OPT_S" == "1" ]; then
			O_NOCHANGE="1"
		fi
		if [ "$OPT_S" == "2" ] || [ "$OPT_S" == "3" ]; then
			O_PROMPT="0"
			O_NOCHANGE="1"
		fi
		change_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" AddressPromptUser "$O_PROMPT"
		change_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" AddressDisableChange "$O_NOCHANGE"
	fi

	if [ "$OPT_G" != "" ]; then
		O_OVERA="1"
		if [ "$OPT_G" == "yes" ]; then
			O_OVERA="0"
		fi
		change_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" AddressDefaultCurrent "$O_OVERA"
	fi

	change_choice "$OPT_C" "KeyCheckout"
	change_choice "$OPT_T" "KeyVerify"
	change_choice "$OPT_P" "KeyAccessPref"

	if [ "$OPT_K" != "" ]; then
		O_KILL="0"
		if [ "$OPT_K" == "yes" ]; then
			O_KILL="1"
		fi
		change_script_var "$EXTRACTDIR/s_ka/" "preinstall" KILL_KA_FIRST "$O_KILL"
	fi

	if [ "$OPT_R" != "" ]; then
		O_START="0"
		if [ "$OPT_R" == "yes" ]; then
			O_START="1"
		fi
		change_script_var "$EXTRACTDIR/s_ka/" "postinstall" STARTUP_KA "$O_START"
	fi

	change_restart "$OPT_B"

	if [ "$OPT_L" != "" ]; then
		O_LOCK="0"
		if [ "$OPT_L" == "yes" ]; then
			O_LOCK="1"
		fi
		change_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" KASettingsLocked "$O_LOCK"
	fi

	if [ "$OPT_F" != "" ]; then
		change_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" UseDNSHostname "$OPT_F"
	fi

	if [ "$OPT_I" != "" ]; then
		change_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" SiteAuditTag "$OPT_I"
	fi

	if [ "$OPT_V" != "" ]; then
		change_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" CustomPrefs "$OPT_V"
	fi

	if [ "$OPT_Z" != "" ]; then
		O_USEC="0"
		if [ "$OPT_Z" == "comp" ]; then
			O_USEC="1"
		elif [ "$OPT_Z" == "short" ]; then
			O_USEC="3"
		fi
		change_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" UseComputerName "$O_USEC"
	fi
fi

NOPT_H=''
NOPT_I=''
NOPT_S=''
NOPT_G=''
NOPT_C=''
NOPT_T=''
NOPT_P=''
NOPT_K=''
NOPT_R=''
NOPT_B=''
NOPT_L=''
NOPT_F=''
NOPT_Z=''
NOPT_X=''

#	NOPT_H=`grep -A 1 "<key>KSAddress</key>" ../KeyAccess.pkg/Contents/Resources/k2clientconfig.plist | sed -e "1d" -e "s/\(.*\<string\>\)\(.*\)\(\<\/string\>.*\)/\2/"`
NOPT_H=`get_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" "KSAddress"`
NOPT_I=`get_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" "SiteAuditTag"`

O_PROMPT=`get_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" "AddressPromptUser"`
if [ "$O_PROMPT" == "0" ]; then
	NOPT_S="2"
	if [ "$OPT_S" == "3" ]; then
		NOPT_S="3"
	fi
else
	O_NOCHANGE=`get_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" "AddressDisableChange"`
	if [ "$O_NOCHANGE" == "1" ]; then
		NOPT_S="1"
	else
		NOPT_S="0"
	fi
fi

O_OVERA=`get_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" "AddressDefaultCurrent"`
if [ "$O_OVERA" == "0" ]; then
	NOPT_G="yes"
else
	NOPT_G="no"
fi

NOPT_C=`get_choice "KeyCheckout"`
NOPT_T=`get_choice "KeyVerify"`
NOPT_P=`get_choice "KeyAccessPref"`

O_KILL=`get_script_var "$EXTRACTDIR/s_ka/" "preinstall" "KILL_KA_FIRST"`
if [ "$O_KILL" == "1" ]; then
	NOPT_K="yes"
else
	NOPT_K="no"
fi

O_START=`get_script_var "$EXTRACTDIR/s_ka/" "postinstall" "STARTUP_KA"`
if [ "$O_START" == "1" ]; then
	NOPT_R="yes"
else
	NOPT_R="no"
fi


NOPT_B=`get_restart`

O_LOCK=`get_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" "KASettingsLocked"`
if [ "$O_LOCK" == "0" ]; then
	NOPT_L="no"
else
	NOPT_L="yes"
fi

NOPT_F=`get_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" "UseDNSHostname"`

NOPT_V=`get_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" "CustomPrefs" 2> /dev/null`

O_USEC=`get_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" "UseComputerName"`
if [ "$O_USEC" == "0" ]; then
	NOPT_Z="user"
elif [ "$O_USEC" == "3" ]; then
	NOPT_Z="short"
else
	NOPT_Z="comp"
fi

SUCCESS=1
if [ "$OPT_D" == "1" ]; then
	DISP="k2clientconfig -h $NOPT_H -s $NOPT_S -g $NOPT_G -c $NOPT_C -t $NOPT_T -p $NOPT_P -k $NOPT_K -r $NOPT_R -b $NOPT_B -l $NOPT_L -f $NOPT_F -z $NOPT_Z"
	if [ "$NOPT_I" != "" ]; then
		DISP="$DISP -i $NOPT_I"
	fi
	if [ "$NOPT_V" != "" ]; then
		DISP="$DISP -v \"$NOPT_V\""
	fi
	echo "$DISP"
else
	if [ "$OPT_H" != "" ] && [ "$OPT_H" != "$NOPT_H" ]; then
		echo "-h $OPT_H failed, still set to $NOPT_H"
		SUCCESS=0
	fi
	if [ "$OPT_I" != "" ] && [ "$OPT_I" != "$NOPT_I" ]; then
		echo "-h $OPT_I failed, still set to $NOPT_I"
		SUCCESS=0
	fi
	if [ "$OPT_S" != "" ] && [ "$OPT_S" != "$NOPT_S" ]; then
		echo "-s $OPT_S failed, still set to $NOPT_S"
		SUCCESS=0
	fi
	if [ "$OPT_G" != "" ] && [ "$OPT_G" != "$NOPT_G" ]; then
		echo "-g $OPT_G failed, still set to $NOPT_G"
		SUCCESS=0
	fi
	if [ "$OPT_C" != "" ] && [ "$OPT_C" != "$NOPT_C" ]; then
		echo "-c $OPT_C failed, still set to $NOPT_C"
		SUCCESS=0
	fi
	if [ "$OPT_T" != "" ] && [ "$OPT_T" != "$NOPT_T" ]; then
		echo "-t $OPT_T failed, still set to $NOPT_T"
		SUCCESS=0
	fi
	if [ "$OPT_P" != "" ] && [ "$OPT_P" != "$NOPT_P" ]; then
		echo "-p $OPT_P failed, still set to $NOPT_P"
		SUCCESS=0
	fi
	if [ "$OPT_K" != "" ] && [ "$OPT_K" != "$NOPT_K" ]; then
		echo "-k $OPT_K failed, still set to $NOPT_K"
		SUCCESS=0
	fi
	if [ "$OPT_R" != "" ] && [ "$OPT_R" != "$NOPT_R" ]; then
		echo "-r $OPT_R failed, still set to $NOPT_R"
		SUCCESS=0
	fi
	if [ "$OPT_B" != "" ] && [ "$OPT_B" != "$NOPT_B" ]; then
		echo "-b $OPT_B failed, still set to $NOPT_B"
		SUCCESS=0
	fi
	if [ "$OPT_L" != "" ] && [ "$OPT_L" != "$NOPT_L" ]; then
		echo "-l $OPT_L failed, still set to $NOPT_L"
		SUCCESS=0
	fi
	if [ "$OPT_F" != "" ] && [ "$OPT_F" != "$NOPT_F" ]; then
		echo "-f $OPT_F failed, still set to $NOPT_F"
		SUCCESS=0
	fi
	if [ "$OPT_Z" != "" ] && [ "$OPT_Z" != "$NOPT_Z" ]; then
		echo "-z $OPT_Z failed, still set to $NOPT_Z"
		SUCCESS=0
	fi

	if [ $SUCCESS -eq 1 ] ; then
		if [ $KA_NEWPKG -ne 0 ]; then
			change_js_prop "$EXTRACTDIR/pkg/" "Distribution" "CUSTOM" "1"
		fi
		change_plist_string "$EXTRACTDIR/s_ka/" "k2clientconfig.plist" "PROP_CUSTOM" "1"

		plutil -convert xml1 "$EXTRACTDIR/s_ka/k2clientconfig.plist"

		scripts_compress $EXTRACTDIR/pkg/KeyAccess.pkg/Scripts $EXTRACTDIR/s_ka
		pkg_compress $PKGPATH $EXTRACTDIR/pkg $EXTRACTDIR
	fi
fi

if [ -f $EXTRACTDIR/pkg/Distribution ]; then
	rm -rf $EXTRACTDIR
fi

exit 0
