function InitializeCustomInfo()
	STRING	svUninstParam;
begin
	gCustomInfo.szTwainDir = WINDIR ^ "TWAIN_32" ^ IFX_PRODUCT_KEY;
	gCustomInfo.szDriverPackageDir = COMMONFILES ^ IFX_PRODUCT_KEY;
    StrSub(svUninstParam, UNINSTALL_STRING, 9, StrLength(UNINSTALL_STRING));
	gCustomInfo.szUninstParam = svUninstParam;
	   
    gCustomInfo.szSoftRootKey = "System\\Sonix";
    gCustomInfo.szProductType = "PCCAM";
    
    gCustomInfo.szDriverServiceOld = "snpstd";
    gCustomInfo.szDriverService = IFX_PRODUCT_KEY;
    gCustomInfo.szHardwareId = "USB\\VID_0c45&PID_6030";
    gCustomInfo.szDefInfPath = WINDIR ^ "INF" ^ IFX_PRODUCT_KEY + ".inf";
    
	if (SYSINFO.WINNT.bWinNT) then
 		gCustomInfo.szUninstToolPath = WINDIR ^ "u" + IFX_PRODUCT_KEY + ".exe";
 	else
 		gCustomInfo.szUninstToolPath = "";
 	endif;
	
	gCustomInfo.bInitialized = TRUE;
end;

function InitializeCustomTarget()
begin
	if (!gCustomInfo.bInitialized) then
		InitializeCustomInfo();
	endif;
	
    ComponentSetTarget (MEDIA, "<TWAIN_DS_DIR>", gCustomInfo.szTwainDir);
    ComponentSetTarget (MEDIA, "<DRIVER_PACKAGE_DIR>", gCustomInfo.szDriverPackageDir);
    ComponentSetTarget (MEDIA, "<UNINST_PARAM>", gCustomInfo.szUninstParam);
end;

function UninstallDriver()
	STRING szSoftProductTypeKey;
	NUMBER nKeyCounts, nNameCounts;
begin
	if (!gCustomInfo.bInitialized) then
		InitializeCustomInfo();
	endif;

    // remove driver service and hardware key    
    if (SYSINFO.WINNT.bWinNT) then
    	ServiceStopService ( "StiSvc" );
    	ServiceRemoveService ( gCustomInfo.szDriverServiceOld );
    	ServiceRemoveService ( gCustomInfo.szDriverService );
    	LaunchAppAndWait ( gCustomInfo.szUninstToolPath , "NOREBOOT" , LAAW_OPTION_WAIT );
    	ServiceStartService ( "StiSvc" , "" );
    else
    	RegDBDeleteKey ( "Enum\\USB\\VID_0c45&PID_603f"); //CISVF10
    	RegDBDeleteKey ( "Enum\\USB\\VID_0c45&PID_602a"); //HY7131D/E
    	RegDBDeleteKey ( "Enum\\USB\\VID_0c45&PID_602d"); //HY7131R
    	RegDBDeleteKey ( "Enum\\USB\\VID_0c45&PID_6028"); //PAS202
    	RegDBDeleteKey ( "Enum\\USB\\VID_0c45&PID_6029"); //PAS106
    	RegDBDeleteKey ( "Enum\\USB\\VID_0c45&PID_6009"); //PAS106+SN9C101  
    	RegDBDeleteKey ( "Enum\\USB\\VID_0c45&PID_6030"); //MI0343 MI0360
    //	RegDBDeleteKey ( "Enum\\USB\\VID_0c45&PID_602b"); //MI0360 for Genius
    	RegDBDeleteKey ( "Enum\\USB\\VID_0c45&PID_6024"); //TAS5130
    	RegDBDeleteKey ( "Enum\\USB\\VID_0c45&PID_6025"); //TAS5130
    	RegDBDeleteKey ( "Enum\\USB\\VID_0c45&PID_6005"); //TAS5110+SN9C101  
    	RegDBDeleteKey ( "Enum\\USB\\VID_0c45&PID_602c"); //OV7630
    	
    	RegDBDeleteKey ( "Enum\\Root\\Unknown" );
    	DeleteFile ( WINDIR ^ "INF" ^ "drvidx.bin");
    	DeleteFile ( WINDIR ^ "INF" ^ "drvdata.bin");
    endif;

	// remove software key
	if (MAINTENANCE) then
	    szSoftProductTypeKey = gCustomInfo.szSoftRootKey + "\\" + gCustomInfo.szProductType;
        RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );
        RegDBDeleteKey ( szSoftProductTypeKey + "\\" + IFX_PRODUCT_KEY );
        RegDBDeleteKey ( gCustomInfo.szSoftRootKey + "\\" + IFX_PRODUCT_NAME );
        nKeyCounts = RegDBQueryKeyCount ( szSoftProductTypeKey , REGDB_KEYS );
        nNameCounts = RegDBQueryKeyCount ( szSoftProductTypeKey , REGDB_NAMES );
        if (nKeyCounts == 0 && nNameCounts == 0) then
         	RegDBDeleteKey ( szSoftProductTypeKey );
        endif;
        nKeyCounts = RegDBQueryKeyCount ( gCustomInfo.szSoftRootKey , REGDB_KEYS );
        nNameCounts = RegDBQueryKeyCount ( gCustomInfo.szSoftRootKey , REGDB_NAMES );
        if (nKeyCounts == 0 && nNameCounts == 0) then
        	RegDBDeleteKey ( gCustomInfo.szSoftRootKey );
        endif;  
        
        RegDBDeleteValue ( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run" , IFX_PRODUCT_KEY );      //2004-04-08
  endif;
end;

function UninstallDriverFiles()
	STRING szUninstKeyParent;
	STRING svInfPath, svPnfPath;
	NUMBER nResult, nvType, nvSize;
	BOOL bResult;    //2004-04-08
begin
	if (!gCustomInfo.bInitialized) then
		InitializeCustomInfo();
	endif;   
	
	// remove uninstallation key
	szUninstKeyParent = "Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\";
    RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );
    RegDBDeleteKey ( szUninstKeyParent + IFX_PRODUCT_KEY );
    RegDBDeleteKey ( szUninstKeyParent + IFX_PRODUCT_NAME );
    
	// delete .inf & .pnf             
   	RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );           
    nResult = RegDBGetKeyValueEx ( szUninstKeyParent + PRODUCT_GUID , "InfPath" , nvType , svInfPath , nvSize );
    if (nResult != 0) then
    	svInfPath = gCustomInfo.szDefInfPath;
    endif;    
	svPnfPath = svInfPath;
	nResult = StrReplace(svPnfPath, ".inf", ".pnf", 0);
	if (nResult > 0) then                        
		nResult = DeleteFile( svPnfPath);       //2004-04-08
		if ((nResult != 0) && SYSINFO.WINNT.bWinNT) then
			bResult = MoveFileExA(svPnfPath, NULL, 0x00000004); //0x00000004: MOVEFILE_DELAY_UNTIL_REBOOT
			if (bResult) then   
				BATCH_INSTALL = TRUE;
			endif;
		endif;
	endif;
	DeleteFile( svInfPath);
	
	// delete unused directory
	DeleteDir ( gCustomInfo.szTwainDir, ALLCONTENTS );
	DeleteDir ( gCustomInfo.szDriverPackageDir, ALLCONTENTS );
end;

function NUMBER SetDriverSigningStatus(nStatus)
	NUMBER nvPrevStatus, nvType, nvSize;
	STRING szKey, szName, svValue;
begin
	nvPrevStatus = 0;
	
    if (SYSINFO.WINNT.bWin2000) then
    	szKey = "SOFTWARE\\Microsoft\\Driver Signing";
    	szName = "Policy";
    	 
	    RegDBSetDefaultRoot ( HKEY_CURRENT_USER );
	    RegDBGetKeyValueEx ( szKey , szName , nvType , svValue , nvSize );
	    StrToNum(nvPrevStatus, svValue);
	    
	    NumToStr(svValue, nStatus);
	    RegDBSetKeyValueEx ( szKey , szName , REGDB_NUMBER , svValue , -1 );
	    
    	RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );
    	Sprintf(svValue, "%c\0\0\0", nStatus);           
	    RegDBSetKeyValueEx ( szKey , szName , REGDB_BINARY , svValue , 4 );
    endif;
    
    return nvPrevStatus;
end;

function PostInstallation()
    STRING szInfPath, svOEMInfPath[MAX_PATH];
    STRING szUninstKey, szName;
    BOOL bResult; 
begin
	if (!gCustomInfo.bInitialized) then
		InitializeCustomInfo();
	endif;

    Disable(LOGGING);
    
    if (SYSINFO.WINNT.bWinNT) then
    	SetDriverSigningStatus(0);
    	
	    szInfPath = gCustomInfo.szDriverPackageDir ^ IFX_PRODUCT_KEY + ".inf";
	    UseDLL ( "Setupapi.dll" );
	    bResult = SetupCopyOEMInfA( szInfPath, NULL, 1, 0, svOEMInfPath, MAX_PATH, NULL, NULL );
	    UnUseDLL ( "Setupapi.dll" );
	    
	    if (bResult) then
	    	szUninstKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\" + PRODUCT_GUID;        
	    	szName = "InfPath";
	    	RegDBSetDefaultRoot ( HKEY_LOCAL_MACHINE );
		    RegDBSetKeyValueEx ( szUninstKey, szName , REGDB_STRING , svOEMInfPath , -1 );
	    endif;
	else
		WriteProfString (  gCustomInfo.szDefInfPath, "SX.USBDCam" , "CopyFiles" , "SX.USBDCam.CopyMsWdmDrivers" );
	    LaunchAppAndWait ( "rundll32.exe" , "c" + IFX_PRODUCT_KEY + ".dll,SwitchHdDma ON" , LAAW_OPTION_WAIT );
    	BATCH_INSTALL = TRUE;
	endif;
	           
    if (SYSINFO.WINNT.bWinNT) then        //2004-04-08
    	LaunchAppAndWait ( WINDIR ^ "v" + IFX_PRODUCT_KEY + ".exe", "" , LAAW_OPTION_NOWAIT );
    endif;
    
    Enable(LOGGING);
end;