// deinstall.inc - Handles deinstall mode.

[MiscDeleteFileList]
// Desc.               Section                Dest. Dir
// ---------------------------------------------------------------------------------
   "Generated Files"   = Files-Delete,        $(!STF_WINDOWSSYSPATH)

//
//	Removes existing files from hard drive by attempting to delete them.
//	If delete fails it renames them and adds them to the DeleteList.
//
//		$0 = File list
//
[RemoveFiles]
	set FileList = $($0)
	_Enter("RemoveFiles", _Show(FileList))

	ForListDo $(FileList)
		set CurrentPath = $($)
		_Trace("Removing", _Show(CurrentPath))
		_Call("CheckFileExistance", _Show(CurrentPath))
		LibraryProcedure Result $(!LIBHANDLE) CheckFileExistance $(CurrentPath)
		_Return("CheckFileExistance", _Show(Result))
		ifstr(i) $(Result) == "YES"
			_Trace("Exists", "")
			_Call("DelFile", _Show(CurrentPath))
			LibraryProcedure Result $(!LIBHANDLE) DelFile $(CurrentPath)
			_Return("DelFile", _Show(Result))
			ifstr(i) $(Result) != "SUCCESS"
				_Trace("Delete failed", "")
				_LocalShell DirectoryFromPath $(CurrentPath)
				set Directory = $($R0)
				_LocalShell BaseFromPath $(CurrentPath)
				set Base = $($R0)
				set BackupPath = $(Directory)$(Base)".old"

				_Call("CheckFileExistance", _Show(BackupPath))
				LibraryProcedure Result $(!LIBHANDLE) CheckFileExistance $(BackupPath) 
				_Return("CheckFileExistance", _Show(Result))
				ifstr(i) $(Result) == "YES"
					_Trace("Exists - Deleting", "")
					_Call("DelFile", _Show(BackupPath))
					LibraryProcedure Result $(!LIBHANDLE) DelFile $(BackupPath)
					_Return("DelFile", _Show(Result))
				endif

				_Call("RenFile", _Show(CurrentPath)_Show(BackupPath))
				LibraryProcedure Result $(!LIBHANDLE) RenFile $(CurrentPath) $(BackupPath)
				_Return("RenFile", _Show(Result))
				AddFileToDeleteList $(BackupPath)
		    else
        		LibraryProcedure Result $(!LIBHANDLE) CheckFileExistance $(CurrentPath)
		        _Return("CheckFileExistance", _Show(Result))
		        ifstr(i) $(Result) == "YES"
		        	AddFileToDeleteList $(CurrentPath)
		          	_Return("AddFileToDeleteList", _Show(CurrentPath))
		      	endif
		  	endif
		endif
	EndForListDo

	_Exit("RemoveFiles", "")
	Return

//
// AddSingleFileToRemoveList
//
// $0 = Path
//
[AddSingleFileToRemoveList]

   set Path = $($0)
	_Enter("AddSingleFileToRemoveList", _Show(Path))

   set !DIGI_RemoveFilesList = >($(!DIGI_RemoveFilesList), $(Path))

	_Exit("AddSingleFileToRemoveList", "")
	Return

//
// Like AddSectionFilesToCopyList
//
// $0 = Files Section
// $1 = Destination Dir
//
[AddSectionFilesToRemoveList]

   set Section = $($0)
   set Dir = $($1)
	_Enter("AddSectionFilesToRemoveList", _Show(Section)_Show(Dir))

	Ifcontains $(Section)$(Dir) not-in $(!DIGI_FileSectionsToRemoveCoalesceList)
      set !DIGI_FileSectionsToRemoveCoalesceList = >($(!DIGI_FileSectionsToRemoveCoalesceList), $(Section)$(Dir))
      set FileList = ^($(Section), 2)
      ForListDo $(FileList)
         _LocalShell AddSingleFileToRemoveList $(Dir)"\\"$($)
	   EndForListDo
   else
   	_Trace("Coalescing", _Show(Section)_Show(Dir))
   endif

	_Exit("AddSectionFilesToRemoveList", "")
	Return

//
// Like CopyFilesInCopyList
//
[RemoveFilesInRemoveList]

	_Enter("RemoveFilesInRemoveList", _Show(Section)_Show(Dir))

	_LocalShell RemoveFiles $(!DIGI_RemoveFilesList)
	_LocalShell ClearRemoveList

	_Exit("RemoveFilesInRemoveList", "")
	Return

//
// Like ClearCopyList
//
[ClearRemoveList]
   set !DIGI_RemoveFilesList = EMPTY_LIST;
   set !DIGI_FileSectionsToRemoveCoalesceList = EMPTY_LIST;
   Return

//
// Removes a protocol from an adapter.
//
//		$0 = Line number.
//		$1 = Adapter service name
//
[RemoveServicesFromLine]
	read-syms GlobalConstants

	set Line = $($0)
	set AdapterServiceName = $($1)
	_Enter("RemoveServicesFromLine", _Show(Line)_Show(AdapterServiceName))

	set KeyParameters = KEY_NULL
	set KeyLine = KEY_NULL

	_OpenServiceParamKey($(AdapterServiceName), KeyParameters)
	_IfNotValidKey($(KeyParameters))
		goto Exit
	endif

	_OpenRegKey($(KeyParameters), $(Line), KeyLine)
	_IfNotValidKey($(KeyLine))
		goto Exit
	endif

	set LineServices = EMPTY_LIST
	_GetRegMultiSzByKey $(KeyLine) DXB_REGVAL_LINE_SERVICES
   _If($($R0))
	   set LineServices = $($R1)
   endif

	ForListDo $(LineServices)
       set Service = $($)
	   _LocalShell "Remove-"$(Service) $(Line) $(KeyLine) $(AdapterServiceName)
	   _LocalShell "CleanUp-"$(Service) $(Line) $(KeyLine) $(AdapterServiceName) $(Result)
	EndForListDo

Exit = +
	_CloseRegKey($KeyParameters)
	_CloseRegKey($KeyLine)

	_Exit("RemoveServicesFromLine", "")
	Return

//
// Removes software components for all known services
//
[DeinstallAllServices]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms GlobalPathConstants
	read-syms DriverConstants
	read-syms Identification
	read-syms DriverPackageConstants

	_Enter("DeinstallAllServices", "")

   ForListDo $(ServicesToDeinstallList)
      set Service = $($)
      _Trace("Deinstalling", _Show(Service))
      read-syms DriverConstants-$(Service)
      _UtilityShell RemoveSoftwareComponent $(Manufacturer) $(ProductSoftwareName) "FALSE"
#if defined(_DEBUG)
      _ShellError(Dontcare)
      _UtilityShellError($($R0), DontCare)
#endif
   EndForListDo

	_Exit("DeinstallAllServices", "")
	Return

//
//	In the simplest case we only remove the adapter registry entries.
//	If services are running on the adapter we remove them.
//	Check bus type reference counts and remove firmware files if needed.
//	When removing the last adapter we remove DigiDxb, all files, and short-cuts.
//
//		$0 = Option
//		$1 = Source Dir
//		$2 = $(AddCopy)
//		$3 = $(DoCopy)
//		$4 = $(DoConfig)
//
//		$R0 = Status (STATUS_*)
//
[DeinstallMode]
	read-syms GlobalConstants
	read-syms GlobalRegistryConstants
	read-syms GlobalPathConstants
	read-syms DriverConstants
	read-syms Identification

	set Option = $($0)
	set SrcDir = $($1)
	set AddCopy = $($2)
	set DoCopy = $($3)
	set DoConfig = $($4)
	_Enter("DeinstallMode", _Show(Option)_Show(SrcDir)_Show(AddCopy)_Show(DoCopy)_Show(DoConfig))

	read-syms DriverPackageConstants
	read-syms DriverConstants-$(Option)
	read-syms InstallMessages-$(!STF_LANGUAGE)

	set Status = "STATUS_FAILED"

	set KeyAdapter = KEY_NULL
	set KeyNetRules = KEY_NULL
	set KeyProduct = KEY_NULL
	set KeyDigiRoot = KEY_NULL

   ifstr(i) $(Option) == "PAMA"
      // PAMA is special, it can NOT be removed by itself!
      // If this is PAMA, then throw up a message, and bail.
      _LocalShell StatusMessage $(CannotRemoveThisService)
   	set Status = "STATUS_USERCANCEL"
      goto Exit
   endif

	// Our drivers are hidden so this will never happen.
	ifstr(i) $(ProductKeyName) == $(!NTN_RegBase)
		_UtilityShell RemoveSoftwareComponent $(Manufacturer) $(ProductSoftwareName)
#if defined(_DEBUG)
      _ShellError(DontCare)
      _UtilityShellError($($R0), DontCare)
#endif
		goto CheckProductList
	endif

	_OpenRegKey_HKLM($(!NTN_RegBase), KeyAdapter)
	_IfNotValidKey($(KeyAdapter))
		goto Exit
	endif

	_GetRegSzByKey $(KeyAdapter) DXB_REGVAL_SERVICENAME
   _Assert($($R0))
	set ServiceName = $($R1)

	_OpenRegKey($(KeyAdapter), "NetRules", KeyNetRules)
	_IfNotValidKey($(KeyNetRules))
		goto Exit
	endif

	// Same option as passed in?
	_Trace("Passed in ", _Show1(Option))
	_GetRegSzByKey $(KeyNetRules) "InfOption"
   _Assert($($R0))

   _ToBoolean(ifstr(i) $($R1) == $(Option), OptionMatch)
   _Assert($(OptionMatch))

	set Option = $($R1)
	_Trace("Registry ", _Show1(Option))
	
	// Remove services running on adapter.
	// Drivers will be removed only if their reference count drop to zero.
	// Resources (TAPI keys, virtual adapters, etc.) are always removed.

	_OpenRegKey_HKLM($(!NTN_RegBase), KeyAdapter)
	_IfNotValidKey($(KeyAdapter))
		goto Exit
	endif

	_OpenServiceParamKey($(ServiceName), KeyParameters)
	_IfNotValidKey($(KeyParameters))
		goto Exit
	endif

   _GetRegDwordByKey $(KeyParameters) DXB_REGVAL_NUMBEROFLINES
   _Assert($($R0))
	set NumberOfLines = $($R1)

   set LineNumber = 0
	_Do(RemoveLineServices)
		_LocalShell NumberToName $(LineNumber)
      set LineName = $($R0)
	   _LocalShell RemoveServicesFromLine $(LineName) $(ServiceName)
		set-add LineNumber = $(LineNumber), 1
	_While(RemoveLineServices, ifint $(LineNumber) < $(NumberOfLines))

#if defined(SUPPORT_SYNC570)
	ifcontains(i) $(Option) in $(OptionsThatNeedStreams)
		// Remove the Adapter from the DigiSOH List
		_LocalShell RemoveAdapterFromMiniportAdapters $(ServiceName)
	endif
#endif

   // HKLM\SYSTEM\CurrentControlSet\Services\DigiDxb?
	_UtilityShell RemoveHardwareComponent $(Manufacturer) $(ProductHardwareName) $(!NTN_RegBase)
#if defined(_DEBUG)
   _ShellError(DontCare)
   _UtilityShellError($($R0), DontCare)
#endif

   // HKLM\SOFTWARE\Digi\DigiDxb
   _UtilityShell IsRefCountEqualZero $(ProductKeyName)
   _If($($R1))
	   _UtilityShell RemoveSoftwareComponent $(Manufacturer) $(ProductSoftwareName)
#if defined(_DEBUG)
      _ShellError(DontCare)
      _UtilityShellError($($R0), DontCare)
#endif
   endif

   // HKLM\SOFTWARE\Digi\WAN Links\InstalledOptions\xxxx	
   _UtilityShell DecrementRefCount $(PackageOptionInstalledKeyName)
   _UtilityShell IsRefCountEqualZero $(PackageOptionInstalledKeyName)
   _If($($R1))
	   DeleteRegTree $(!REG_H_LOCAL) $(PackageOptionInstalledKeyName)
   endif

   // HKLM\SOFTWARE\Digi\WAN Links
   _UtilityShell DecrementRefCount $(PackageKeyName)
   _UtilityShell IsRefCountEqualZero $(PackageKeyName)
   _If($($R1))
		_UtilityShell RemoveSoftwareComponent $(Manufacturer) $(PackageSoftwareName) "FALSE"
#if defined(_DEBUG)
      _ShellError(DontCare)
      _UtilityShellError($($R0), DontCare)
#endif

      _LocalShell DeinstallAllServices
	  
	  _LocalShell RemoveGroupFromServiceGroupOrder $(DStreamsDependOnGroup)

#if defined(OLD_PRODUCT_VERSION)
      // Delete hack
	   read-syms OldDriverConstants  
		_UtilityShell RemoveSoftwareComponent $(Manufacturer) $(OldProductSoftwareName) "FALSE"
#if defined(_DEBUG)
      _ShellError(Dontcare)
      _UtilityShellError($($R0), DontCare)
#endif
#endif

		_LocalShell RemoveSNMP
      _LocalShell RemoveDiags $(Option) $(SrcDir) $(AddCopy) $(DoCopy)

      set FileListSectionsToDelete = { +

         DetectionFilesList, +
         MiscDeleteFileList, +
         CoreFilesList, +
#if defined(SUPPORT_HOUSTON)
         CoreFilesList-Houston, +
#endif
#if defined(SUPPORT_LONDON)
         CoreFilesList-London, +
#endif
#if defined(SUPPORT_CHASKA)
         CoreFilesList-Chaska, +
#endif
#if defined(SUPPORT_CLYDESDALE)
         CoreFilesList-Clydesdale, +
#endif
#if defined(SUPPORT_PRIME)
         CoreFilesList-PRIme, +
#endif
#if defined(SUPPORT_SYNC570)
         CoreFilesList-Sync570, +
#endif
#if defined(SUPPORT_ACCELEPORT_2000)
         CoreFilesList-AccelePort2000, +
#endif

         }

      ForListDo $(FileListSectionsToDelete)

         set FileListSection = $($)

         set DescList = ^($(FileListSection), 0)
         ForListDo $(DescList)

            set Desc = $($)
            set Section = #($(FileListSection), $(Desc), 1)
            set DstDir = #($(FileListSection), $(Desc), 2)

            ifstr(i) $(Section) != "Files-Redistributable"                             // don't delete redist. mfc/msvcrt dlls
	            _Trace("Adding "$(Desc)" for deletion", _Show(Section)_Show(DstDir))
               _LocalShell AddSectionFilesToRemoveList $(Section) $(DstDir)
            endif

         EndForListDo
      EndForListDo

      _LocalShell AddSingleFileToRemoveList $(STF_CONTEXTINFNAME)
      _LocalShell RemoveFilesInRemoveList

   endif


CheckProductList = +

	_OpenRegKey_HKLM($(KEY_DIGIROOT), KeyDigiRoot)
	_IfValidKey($(KeyDigiRoot))
		EnumRegKey $(KeyDigiRoot) DigiProductList
		QueryListSize NumDigiProducts $(DigiProductList)
		ifint $(NumDigiProducts) == 0
			DeleteRegTree $(!REG_H_LOCAL) $(KEY_DIGIROOT)
		endif
	endif

	set Status = "STATUS_SUCCESSFUL"

Exit = +

	_CloseRegKey($(KeyAdapter))
	_CloseRegKey($(KeyNetRules))
	_CloseRegKey($(KeyProduct))
	_CloseRegKey($(KeyDigiRoot))

	_Exit("DeinstallMode", _Show(Status))
	Return $(Status)
