[Main.Init]

if IsAdmin()
{
	# Initialize Variables
	ChangeWindow(SelectPrinterUninstallDialog,MainSplitter.top);
	ChangeWindow(Navigation,MainSplitter.bottom);
	
	PopulateUninstallTree(UninstallTree,refresh);
	TreeNodeCount(UninstallTree,Printers,cgfVar_PrinterObjectCount);	

	# Add dependencies, hiding, and reparenting here.
	# User must manually click ENetwork to select it fully. 
	# This allows its kids can be uninstalled independently of it.
	SetTreeItemAttrib(UninstallTree,ENetwork,ManualFullCheck);
	
	# Legacy components : Handle components that are available in past products. 
	SetTreeItemAttrib(UninstallTree,Bidi,ManualFullCheck);

	ChangeWindow(BusyAppsDialog,MainSplitter.top);
	PrinterBusy(%OEM% %MODELNAME%,cgfVar_PrinterBusy);
	# Confirmation dialog
	ChangeWindow(ConfirmationDialog,MainSplitter.top);	
	RunSection(ShowYesNoButtons);

	# SkipConfirm flag is used to determine if we want to ignore the Uninstall confirmation dialog.
	if CheckVariable(SkipConfirm,eq,true)
	{
		RunSection(Navigation.BtnYes.ButtonClick);
	}
} else
{
	ShowMessage(%Uninstall.NoAdminRightsMessage%,%Uninstall.DriverUninstallTitle%,error);
}

[SelectPrinterUninstallDialog.Init]
RunSection(ShowSelectCancelButtons);

[RestartDialog.Init]
RunSection(ShowOkButton);
SetFocus(BtnRebootYes);
#Set RunOnce Registry Value
if CheckVariable(Unst_Warsaw,ne,true)
{
	RunUnInstallAction(cgfVar_RegActionReturn,RegistryEntry,Add,HKLM,SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce,%OEM% %ModelName%,STR,);
}

[ConfirmationDialog.Init]
RunSection(ShowYesNoButtons);

[BusyAppsDialog.Init]
RunSection(ShowOkButton);

[Navigation.BtnOK.ButtonClick]
if IsWindowActive(BusyAppsDialog,MainSplitter.top)
{
	ExitApp(noPrompt);
} else if IsWindowActive(RestartDialog,MainSplitter.top)
{
	if CheckValue(BtnRebootYes,eq,true)
	{
		ExitApp(noPrompt,reboot);
	} else
	{
		RunUnInstallAction(cgfVar_Return,RegistryEntry,Add,HKLM,Software\Microsoft\Windows\CurrentVersion\RunOnce,lvdcUninstallRan,STR,);
		if CheckVariable(ReturnCDFE,eq,true)
		{
			RunSection(ReturnToCDFE);
		} else
		{
			ExitApp(noPrompt);
		}
	}
} 

[Navigation.BtnSelectPrinter.ButtonClick]
if TreeNotChecked(UninstallTree)
{
	ShowMessage(%Uninstall.NothingSelected%,%Uninstall.DriverUninstallTitle%,Error);
	Break();		
} else 
{
	if IsTreeItemFullChecked(UninstallTree,Printers)
	{
		SetVariable(cgfVar_UninstallAll,true);
	} else 
	{
		SetVariable(cgfVar_UninstallAll,false);	
	}
	RunSection(ProceedUninstall);
}

[Navigation.BtnYes.ButtonClick]
if CheckSize(BusyAppsList,ne,0)
{	
	ChangeWindow(BusyAppsDialog,MainSplitter.top);
	RunSection(ShowOkButton);		
} else if CheckVariable(cgfVar_PrinterBusy,eq,true)
{
	ShowMessage(%Uninstall.PrinterBusyMessage%,%Uninstall.PrinterBusyTitle%,Error);
	ExitApp(noPrompt);
} else
{	
	# Determine wether to display the multiple printer objects dialog	
	CheckTreeItem(UninstallTree,UsersGd,true);
	CheckTreeItem(UninstallTree,Misc,true);
	CheckTreeItem(UninstallTree,OCR,true);
	CheckTreeItem(UninstallTree,AppInst,true);
	CheckTreeItem(UninstallTree,LeadTool,true);
	CheckTreeItem(UninstallTree,warsaw,true);
	CheckTreeItem(UninstallTree,AppToFEL,true);
	CheckTreeItem(UninstallTree,App4R,true);
	CheckTreeItem(UninstallTree,Fax4,true);
	CheckTreeItem(UninstallTree,Printers,true);
	if CheckVariable(cgfVar_PrinterObjectCount,lt,2)
	{				 
		WriteTreeToScript(UninstallTree);
		SetVariable(cgfVar_UninstallAll,true);
		RunSection(ProceedUninstall);
	} else
	{
		WriteTreeToScript(UninstallTree);	
		CheckTreeItem(UninstallTree,Printers,false);	
		HideTreeItem(UninstallTree,UsersGd,false);
		HideTreeItem(UninstallTree,Misc,false);
		HideTreeItem(UninstallTree,OCR,false);
		HideTreeItem(UninstallTree,AppInst,false);
		HideTreeItem(UninstallTree,LeadTool,false);
		HideTreeItem(UninstallTree,warsaw,false);
		HideTreeItem(UninstallTree,AppToFEL,false);	
		HideTreeItem(UninstallTree,App4R,false);
		CheckTreeItem(UninstallTree,Fax4,false);
		ChangeWindow(SelectPrinterUninstallDialog,MainSplitter.top);
		RunSection(ShowSelectCancelButtons);
	}
}

[Navigation.BtnCancel.ButtonClick]
if CheckVariable(ReturnCDFE,eq,true)
{
	RunSection(ReturnToCDFE);
} else
{
	ExitApp(noPrompt);
}

[Navigation.BtnNo.ButtonClick]
if CheckVariable(ReturnCDFE,eq,true)
{
	RunSection(ReturnToCDFE);
} else
{
	ExitApp(noPrompt);
}

[ShowOkButton]
ShowWindow(BtnCancel,false);
ShowWindow(BtnNo,false);
ShowWindow(BtnOK,true);
ShowWindow(BtnSelectPrinter,false);
ShowWindow(BtnYes,false);
SetFocus(BtnOK);

[ShowYesNoButtons]
ShowWindow(BtnCancel,false);
ShowWindow(BtnNo,true);
ShowWindow(BtnOK,false);
ShowWindow(BtnSelectPrinter,false);
ShowWindow(BtnYes,true);
SetFocus(BtnYes);

[ShowSelectCancelButtons]
ShowWindow(BtnCancel,true);
ShowWindow(BtnNo,false);
ShowWindow(BtnOK,false);
ShowWindow(BtnSelectPrinter,true);
ShowWindow(BtnYes,false);

[ProceedUninstall]
HideMainFrame();

RunProgram($DRIVER_MULTI_OS_DIR$\%ModelCode%serv.exe,NOWAIT,/unregister);

if CheckVariable(cgfVar_UninstallAll,eq,true)
{
	WriteToScript(Uninstall.Devices.Device0,2);
	WriteToScript(Uninstall.Devices.Device1,2);
	WriteToScript(Uninstall.Devices.Device2,2);
	WriteToScript(Uninstall.Devices.Device3,2);
	WriteToScript(Uninstall.Devices.Device4,2);
	WriteToScript(Uninstall.Devices.Device5,2);
	WriteToScript(Device4.Model,%OEM% %ModelName%);
	WriteToScript(Device1.Model,%OEM% %ModelName% Scanner);
	RunSection(BeginUninstallProgressDialog);

	RunSection(DeleteShortcuts);
	RunSection(DeleteExtraFiles);
	RunSection(DeleteWavFiles);

	RunUnInstallAction(cgfVar_Return,File,DeleteDir,$DRIVER_DIR$\%DEVICEID%\);
	
	if CheckVariable(Unst_Warsaw,eq,true)
	{
		ExitApp(noPrompt);
	} else
	{
		ShowMainFrame();
		ChangeWindow(RestartDialog,MainSplitter.top);
	}		
} else 
{	
	WriteTreeToScript(UninstallTree);
	RunSection(BeginUninstallProgressDialog);
	ShowMessage(%Uninstall.Complete_Title%,%Uninstall.MsgBox_Complete_Title%,ok);
	ExitApp(noPrompt);
}

[ReturnToCDFE]
ExitApp(noPrompt);
if CheckVariable(MediaType,eq,BBS)
{
	if CheckOS(x64)
	{
		RunProgram($CD_ROOT_DIR$\Install\x64\instgui.exe,NOWAIT,MediaType=BBS);
	} else
	{
		RunProgram($CD_ROOT_DIR$\Install\x86\instgui.exe,NOWAIT,MediaType=BBS);
	}
} else
{
	if CheckOS(x64)
	{
		RunProgram($CD_ROOT_DIR$\Install\x64\instgui.exe,NOWAIT,);
	} else
	{
		RunProgram($CD_ROOT_DIR$\Install\x86\instgui.exe,NOWAIT,);
	}
}

[BeginUninstallProgressDialog]
if CheckVariable(Unst_Warsaw,eq,true)
{	
	StartUninstall(,,,);
} else
{
	StartUninstall(ProgressScreen,ProgressBar,ProgressText);
}

[DeleteShortcuts]
if CheckOS(NT_VISTA|X64)
{
	if RegExists(HKLM,Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders,Common Programs,cgfVar_StartMenuPath)
	{
	}
} 
if CheckVariable(cgfVar_StartMenuPath,ne,empty)
{
	FormatString(startMenu\%OEM% %ModelName%\,startMenu,!cgfVar_StartMenuPath,cgfVar_StartMenuPath);
	DeleteDir(!cgfVar_StartMenuPath);
} else
{
	DeleteDir($ALLUSERS_DESKTOP_DIR$\..\Startm~1\Programs\%OEM% %ModelName%\);
}

[DeleteExtraFiles]
if CheckOS(x64)
{
	RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\spool\prtprocs\x64\%ModelCode%pp6c.dll);
	RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\spool\prtprocs\x64\%ModelCode%drpp.dll);
} else
{
	RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\spool\prtprocs\w32x86\%ModelCode%pp5c.dll);
	RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\spool\prtprocs\w32x86\%ModelCode%drpp.dll);
}
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%caps.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%cnv4.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%coin.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%drs.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%iesc.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%inpa.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%lmpm.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%vs.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%cfg.exe);

# Need to manually delete these files in case Warsaw component was not installed
# after declining EULA from PNP install.
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%.loc);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%cfg.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%comc.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%comm.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%coms.exe);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%cu.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%cub.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%cur.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%gf.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%grd.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%help.chm);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%hbn3.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%ih.exe);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%ins.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%insb.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%insr.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%jswr.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%pmui.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%pplc.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%prox.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%serv.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%usb1.dll);
RunUnInstallAction(cgfVar_Return,File,Delete,$SYSTEM_DIR$\%ModelCode%util.dll);

RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%wavs.exe);
RunUnInstallAction(cgfVar_Return,File,DeleteDir,$WINDOWS_DIR$\Twain_32\%OEM%\%ModelName%\);

# New files installed from cab file.
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%Cfg.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%ConfigDeviceSchema.xsd);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%DefaultPrintTicket.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%DeviceNamespace.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%DevicePrivateNamespaces.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%JobCollateAllDocuments.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%JobCopiesAllDocuments.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%JobDuplexAllDocumentsContiguously.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%JobDuplexInstructionSheet.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%JobInputBin.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%JobNUpAllDocumentsContiguously.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%JobNUpPageBorder.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PageBorderless.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PageImageableSize.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PageMediaSize.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PageMediaSizeCustom.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PageMediaSizeCustomCategories.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PageMediaSizeCustomUnits.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PageMediaType.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PageOrientation.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PageOutputColor.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PageOutputQuality.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PageResolution.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PresentationDirection.xml);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PrintCapabilitiesSchema.xsd);
RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\%ModelCode%PrintSchema.xsd);

[DeleteWavFiles]
if DoesFileExists($DRIVER_MULTI_OS_DIR$\*wavs.exe)
{
} else
{
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\wavs.exe);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_bllo.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_blpr.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_canc.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_cllo.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_cmpb.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_covr.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_crm1.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_crmg.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_ivcb.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_lpaf.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_lpmf.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_ntmg.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_pajm.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_phlo.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_prcp.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_prpb.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_prst.wav);
	RunUnInstallAction(cgfVar_Return,File,Delete,$DRIVER_MULTI_OS_DIR$\lxk_tkyu.wav);
}