#!/bin/bash

# postinstall and postupgrade script need to be identical.
# If you modify this script, please synchronize with the other one.

# Description:
# This script generates a single driver bundle with all the supported VID PID
# for Mac 10.5.x OSes and later.
# For Mac 10.4.x OS and before, for each VID PID a separate driver bundle
# is generated.
# This script assumes that the driver binary (libscmccid) will be copied by the
# PackageMaker at /usr/local/scm/ini location.
# This script temporarily generates Bundle.txt, Header.txt, Vid.txt, Pid.txt,
# Name.txt and Footer.txt files at /usr/local/scm/ini location for generating
# driver bundle(s) and later removes them.

# For every new version of the driver,
# Change the CFBundleVersion value in the CreateHeaderFile ().

# To add support for a new reader,
# 1. Add the bundle name at the end of CreateBundleFile ().
# 2. Add the Vid at the end of CreateVidFile ().
# 3. Add the Pid at the end of CreatePidFile ().
# 4. Add the FriendlyName at the end of CreateNameFile ().

# Global variables 
BundleFile="/usr/local/scm/ini/Bundle.txt";
HeaderFile="/usr/local/scm/ini/Header.txt";
VidFile="/usr/local/scm/ini/Vid.txt";
PidFile="/usr/local/scm/ini/Pid.txt";
NameFile="/usr/local/scm/ini/Name.txt";
FooterFile="/usr/local/scm/ini/Footer.txt";
BinaryFile="/usr/local/scm/ini/libscmccid";
declare -i MacMinorVersion=`sw_vers -productVersion | awk -F. '{print $2}'`;

function CreateBundleFile ()
{
	echo "scmccid_5111.bundle" > $BundleFile;
	echo "scmccid_5114.bundle" >> $BundleFile;
	echo "scmccid_5120.bundle" >> $BundleFile;
	echo "scmccid_5121.bundle" >> $BundleFile;
	echo "scmccid_5124.bundle" >> $BundleFile;
	echo "scmccid_e001.bundle" >> $BundleFile;
	echo "scmccid_e002.bundle" >> $BundleFile;
	echo "scmccid_e003.bundle" >> $BundleFile;
	echo "scmccid_5113.bundle" >> $BundleFile;
	echo "scmccid_5115.bundle" >> $BundleFile;
	echo "scmccid_5116.bundle" >> $BundleFile;
	echo "scmccid_5117.bundle" >> $BundleFile;
	echo "scmccid_5118.bundle" >> $BundleFile;
	echo "scmccid_5119.bundle" >> $BundleFile;
	echo "scmccid_511d.bundle" >> $BundleFile;
	echo "scmccid_5410.bundle" >> $BundleFile;
	echo "scmccid_5151.bundle" >> $BundleFile;
	echo "scmccid_0824.bundle" >> $BundleFile;
	echo "scmccid_0924.bundle" >> $BundleFile;
	echo "scmccid_1024.bundle" >> $BundleFile;
	echo "scmccid_1524.bundle" >> $BundleFile;
	echo "scmccid_1624.bundle" >> $BundleFile;
	echo "scmccid_003e.bundle" >> $BundleFile;
	echo "scmccid_512a.bundle" >> $BundleFile;
	echo "scmccid_512c.bundle" >> $BundleFile;
}

function CreateHeaderFile ()
{
	echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" > $HeaderFile;
	echo "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" >> $HeaderFile;
	echo "<plist version=\"1.0\">" >> $HeaderFile;
	echo "<dict>" >> $HeaderFile;
	echo "<key>CFBundleDevelopmentRegion</key>" >> $HeaderFile;
	echo "<string>English</string>" >> $HeaderFile;
	echo "<key>CFBundleExecutable</key>" >> $HeaderFile;
	echo "<string>libscmccid</string>" >> $HeaderFile;
	echo "<key>CFBundleIdentifier</key>" >> $HeaderFile;
	echo "<string>com.scmmicro.drivers.scmccid</string>" >> $HeaderFile;
	echo "<key>CFBundleInfoDictionaryVersion</key>" >> $HeaderFile;
	echo "<string>6.0</string>" >> $HeaderFile;
	echo "<key>CFBundlePackageType</key>" >> $HeaderFile;
	echo "<string>BNDL</string>" >> $HeaderFile;
	echo "<key>CFBundleSignature</key>" >> $HeaderFile;
	echo "<string>????</string>" >> $HeaderFile;
	echo "<key>CFBundleVersion</key>" >> $HeaderFile;
	echo "<string>5.0.21</string>" >> $HeaderFile;
	echo "<key>ifdCapabilities</key>" >> $HeaderFile;
	echo "<string>0x00000000</string>" >> $HeaderFile;
	echo "<key>ifdManufacturerString</key>" >> $HeaderFile;
	echo "<string>SCM Microsystems Inc.</string>" >> $HeaderFile;
	echo "<key>ifdManufacturerURL</key>" >> $HeaderFile;
	echo "<string>http://www.scmmicro.com</string>" >> $HeaderFile;
	echo "<key>ifdProtocolSupport</key>" >> $HeaderFile;
	echo "<string>0x00000001</string>" >> $HeaderFile;
	echo "<key>ifdVersionNumber</key>" >> $HeaderFile;
	echo "<string>0x00000001</string>" >> $HeaderFile;
	echo "<key>ifdProductString</key>" >> $HeaderFile;
	echo "<string>Generic CCID driver</string>" >> $HeaderFile;
}

function CreateVidFile ()
{
	echo "<string>0x04E6</string>" > $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x03F0</string>" >> $VidFile;
	echo "<string>0x03F0</string>" >> $VidFile;
	echo "<string>0x03F0</string>" >> $VidFile;
	echo "<string>0x03F0</string>" >> $VidFile;
	echo "<string>0x03F0</string>" >> $VidFile;
	echo "<string>0x046A</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
	echo "<string>0x04E6</string>" >> $VidFile;
}

function CreatePidFile ()
{
	echo "<string>0x5111</string>" > $PidFile;
	echo "<string>0x5114</string>" >> $PidFile;
	echo "<string>0x5120</string>" >> $PidFile;
	echo "<string>0x5121</string>" >> $PidFile;
	echo "<string>0x5124</string>" >> $PidFile;
	echo "<string>0xE001</string>" >> $PidFile;
	echo "<string>0xE002</string>" >> $PidFile;
	echo "<string>0xE003</string>" >> $PidFile;
	echo "<string>0x5113</string>" >> $PidFile;
	echo "<string>0x5115</string>" >> $PidFile;
	echo "<string>0x5116</string>" >> $PidFile;
	echo "<string>0x5117</string>" >> $PidFile;
	echo "<string>0x5118</string>" >> $PidFile;
	echo "<string>0x5119</string>" >> $PidFile;
	echo "<string>0x511D</string>" >> $PidFile;
	echo "<string>0x5410</string>" >> $PidFile;
	echo "<string>0x5151</string>" >> $PidFile;
	echo "<string>0x0824</string>" >> $PidFile;
	echo "<string>0x0924</string>" >> $PidFile;
	echo "<string>0x1024</string>" >> $PidFile;
	echo "<string>0x1524</string>" >> $PidFile;
	echo "<string>0x1624</string>" >> $PidFile;
	echo "<string>0x003E</string>" >> $PidFile;
	echo "<string>0x512A</string>" >> $PidFile;
	echo "<string>0x512C</string>" >> $PidFile;
}

function CreateNameFile ()
{
	echo "<string>SCR331 DI Smart Card Reader</string>" > $NameFile;
	echo "<string>SCR331 DI Smart Card Reader</string>" >> $NameFile;
	echo "<string>SDI010 USB Smart Card Reader</string>" >> $NameFile;
	echo "<string>SDI010 USB Smart Card Reader</string>" >> $NameFile;
	echo "<string>SCR331 DI Smart Card Reader</string>" >> $NameFile;
	echo "<string>SCR331 USB Smart Card Reader</string>" >> $NameFile;
	echo "<string>SCR332 USB Smart Card Reader</string>" >> $NameFile;
	echo "<string>SPR532 USB Smart Card Reader</string>" >> $NameFile;
	echo "<string>SCR333 USB Smart Card Reader</string>" >> $NameFile;
	echo "<string>SCR335 USB Smart Card Reader</string>" >> $NameFile;
	echo "<string>SCR3310 Smart Card Reader</string>" >> $NameFile;
	echo "<string>USB SIM Card Reader</string>" >> $NameFile;
	echo "<string>Expresscard SIM Card Reader</string>" >> $NameFile;
	echo "<string>Expresscard SmartCard Reader</string>" >> $NameFile;
	echo "<string>SCR3311 Smart Card Reader</string>" >> $NameFile;
	echo "<string>SCR35xx Smart Card Reader</string>" >> $NameFile;
	echo "<string>SCR338 Smart Card Reader</string>" >> $NameFile;
	echo "<string>HP USB Smart Card Reader</string>" >> $NameFile;
	echo "<string>HP USB Smartcard Keyboard</string>" >> $NameFile;
	echo "<string>HP USB Smart Card Reader</string>" >> $NameFile;
	echo "<string>HP USB Smart Card Reader KR</string>" >> $NameFile;
	echo "<string>HP USB Smart Card Reader JP</string>" >> $NameFile;
	echo "<string>SmartTerminal ST-2xxx</string>" >> $NameFile;
	echo "<string>SDI010 USB Smart Card Reader</string>" >> $NameFile;
	echo "<string>SDI011 USB Smart Card Reader</string>" >> $NameFile;
}

function CreateFooterFile ()
{
	echo "</dict>" > $FooterFile;
	echo "</plist>" >> $FooterFile;
}

function CreateFiles ()
{
	CreateBundleFile;
	CreateHeaderFile;
	CreateVidFile;
	CreatePidFile;
	CreateNameFile;
	CreateFooterFile;
}

function RemoveFiles ()
{
	rm -f $BundleFile;
	rm -f $HeaderFile;
	rm -f $VidFile;
	rm -f $PidFile;
	rm -f $NameFile;
	rm -f $FooterFile;
	rm -f $BinaryFile;
}

function CreateHeader ()
{
	cat $HeaderFile > $1;
}

function AppendVidKey ()
{
	echo "<key>ifdVendorID</key>" >> $1;
}

function AppendPidKey ()
{
	echo "<key>ifdProductID</key>" >> $1;
}

function AppendNameKey ()
{
	echo "<key>ifdFriendlyName</key>" >> $1;
}

function AppendArrayStartTag ()
{
	echo "<array>" >> $1;
}

function AppendArrayEndTag ()
{
	echo "</array>" >> $1;
}

function AppendFooter ()
{
	cat $FooterFile >> $1;
	chmod 744 $1;
}

function AppendVidValue_Tiger ()
{
	cat $VidFile | sed -n ''"$2"' s/.*/&/p' >> $1;
}

function AppendPidValue_Tiger ()
{
	cat $PidFile | sed -n ''"$2"' s/.*/&/p' >> $1;
}

function AppendNameValue_Tiger ()
{
	cat $NameFile | sed -n ''"$2"' s/.*/&/p' >> $1;
}

function CreateBundles_Tiger ()
{
	declare -i Counter=1;
	declare -i Max=`cat $BundleFile | wc -l`;

	while [ $Counter -le $Max ]
	do
		Bundle=`cat $BundleFile | sed -n ''"$Counter"' s/.*/&/p'`;
		BundlePath="/usr/libexec/SmartCardServices/drivers/$Bundle";
		LinkPath="/usr/local/pcsc/drivers/$Bundle";
		LinkPath1="/usr/local/lib/pcsc/drivers/$Bundle";
		DriverPath="$BundlePath/Contents/MacOS";
		Infoplist="$BundlePath/Contents/Info.plist";
		
		rm -rf $LinkPath;
		rm -rf $LinkPath1;
		rm -rf $BundlePath;
		mkdir -p $DriverPath;
		cp -f $BinaryFile $DriverPath;

		CreateHeader $Infoplist;

		AppendVidKey $Infoplist;
		AppendVidValue_Tiger $Infoplist $Counter;

		AppendPidKey $Infoplist;
		AppendPidValue_Tiger $Infoplist $Counter;

		AppendNameKey $Infoplist;
		AppendNameValue_Tiger $Infoplist $Counter;

		AppendFooter $Infoplist;

		ln -sf $BundlePath $LinkPath;
		ln -sf $BundlePath $LinkPath1;

		Counter=Counter+1;
	done
}

function ModifyIni_Tiger ()
{
	sed 's/\(MultiSlotSupportForNativeMacPcscd=\)0/\11/' /usr/local/scm/ini/scmccid.ini > /usr/local/scm/ini/newscmccid.ini;
	mv -f /usr/local/scm/ini/newscmccid.ini /usr/local/scm/ini/scmccid.ini;
}

function AppendVidValue_Leopard ()
{
	cat $VidFile >> $1;
}

function AppendPidValue_Leopard ()
{
	cat $PidFile >> $1;
}

function AppendNameValue_Leopard ()
{
	cat $NameFile >> $1;
}

function CreateBundle_Leopard ()
{
	Bundle="scmccid.bundle";
	BundlePath="/usr/libexec/SmartCardServices/drivers/$Bundle";
	LinkPath="/usr/local/pcsc/drivers/$Bundle";
	LinkPath1="/usr/local/lib/pcsc/drivers/$Bundle";
	DriverPath="$BundlePath/Contents/MacOS";
	Infoplist="$BundlePath/Contents/Info.plist";
	
	rm -rf $LinkPath;
	rm -rf $BundlePath;
	rm -rf $LinkPath1;
	mkdir -p $DriverPath;
	cp -f $BinaryFile $DriverPath;

	CreateHeader $Infoplist;

	AppendVidKey $Infoplist;
	AppendArrayStartTag $Infoplist;
	AppendVidValue_Leopard $Infoplist;
	AppendArrayEndTag $Infoplist;

	AppendPidKey $Infoplist;
	AppendArrayStartTag $Infoplist;
	AppendPidValue_Leopard $Infoplist;
	AppendArrayEndTag $Infoplist;

	AppendNameKey $Infoplist;
	AppendArrayStartTag $Infoplist;
	AppendNameValue_Leopard $Infoplist;
	AppendArrayEndTag $Infoplist;

	AppendFooter $Infoplist;

	ln -sf $BundlePath $LinkPath;
	ln -sf $BundlePath $LinkPath1;
}

CreateFiles;

if [ $MacMinorVersion -le 4 ]
then
	CreateBundles_Tiger;
	ModifyIni_Tiger;
else
	CreateBundle_Leopard;
fi

RemoveFiles;
