@echo off

rem *************************************************************************************
rem 
rem   Procedure for adding new applications to CleanWipe
rem
rem     Install the App and find its install name under the uninstall guid that InstallShield creates at: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{GUID}
rem     add code in FindApp.bat to locate this application.  Make sure that FindApp.bat sets an environment variable for the app.  ( Example: SNAC_Folder )
rem
rem     Run GenDynamicFilesKeys.exe -enum on the target machine to see all installed application InstallShield upgrade codes
rem     Use this output to add entry to GenDynamicFilesKeys_in.txt for the installed application.
rem
rem     Add an entry for the application uninstall in the /DELKEY"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\UnInstall\{XXXXX} setion of ModifyRegistryValues.bat
rem
rem     Edit :Remove_Win32Services method in RunCleanWipe.bat and add the application services there.
rem
rem     In DeletePostReboot.bat there is a section of lines for each found app folder like the following:
rem       if exist "%SESM_Folder%" for %%f in ("%SESM_Folder%") do ESUGUnEn /LF"%DeleteFilesPostRestartLog%" /deltree%%f
rem     add a line for the new app.
rem
rem     Make sure that :Remove_LiveUpdate is finding the LiveUpdate uninstaller. Special code was needed for 64Bit OS.
rem
rem     Edit :SetupEnvironment method in RunCleanWipe.bat and make sure we are moving app folder names into DeleteFilesEnVar.txt
rem
rem     Edit :SetupRunOnce and make sure that we are setting the app folder names into the environment for the restart.
rem 
rem *************************************************************************************

rem TBD items:
rem
rem   Sedona testing.
rem
rem   FindApp.bat code that stes various XXX_Folder variables such as SESM_Folder is creating a path containing a trailing backslash.
rem   These paths are then used in the input files to the DeleteFiles.exe code.  DeleteFiles.exe however does not recognize paths 
rem   ending in this backslash.  Example use CleanWipe to uninstall SESM without running the MSI uninstall option and you will still
rem   be left with the c:\Program Files\Symantec\Symantec Enterprise Protection Manager folder on disk that is empty.
rem
rem   CleanWipe uninstall of SESM on 64Bit OS without running MSI Uninstall is leaving the uninstaller for SESM in the
rem   control pannel/ add/remove programs. The registry entry for this uninstall
rem   HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\{B2C6885C-5AF7-4F6E-B6BF-2C5DEAACBD71}"
rem   was deleted under HKLM\Software\Wow6432Node\Microsoft\.  So why is it still there in crippled form.
rem
rem *************************************************************************************


cls
prompt $g

rem *********************************
rem This section used by ESUGUnEn.exe
rem Setting Environment Variables 
call UnEngVar.bat

rem
rem We should differentiate in the log file the first run of CleanWipe
rem from any resumption of CleanWipe after a restart.
rem
if /i "%1" == "-restart" goto :skipDelLogs1
if /i "%2" == "-restart" goto :skipDelLogs1
if /i "%3" == "-restart" goto :skipDelLogs1

if /i "%1" == "-restart2" goto :skipDelLogs1
if /i "%2" == "-restart2" goto :skipDelLogs1
if /i "%3" == "-restart2" goto :skipDelLogs1

if exist "logs\" RD /s /q "logs"
goto :skipDelLogs2

:skipDelLogs1
set StartupText=Resuming CleanWipe Operation & goto :skipDelLogs3
:skipDelLogs2
set StartupText=Start of CleanWipe
:skipDelLogs3

if not exist "logs\" MD logs
set SAVCleanWipe=logs\SAVCleanWipe.log
rem *********************************

echo.>> %SAVCleanWipe%
echo.>> %SAVCleanWipe%
echo *****************************************************************>> %SAVCleanWipe%
echo *****************************************************************>> %SAVCleanWipe%
echo.>> %SAVCleanWipe%
echo %StartupText% at %time% and %date%.>> %SAVCleanWipe%
type version.txt >> %SAVCleanWipe%
set StartupText=

echo %comspec% | find /i "\system32\cmd.exe" > nul
if "%errorlevel%" == "0" goto runFromCmd
set >> %SAVCleanWipe%
echo Detected unsupported command processor: %comspec%. >> %SAVCleanWipe%
esugdlgcontrol -title "CleanWipe Error" -msg "CleanWipe detected unsupported command processor: \"%comspec%\". This may lead to unpredictable results. Please launch CleanWipe again using cmd.exe" -error
goto :exit2 

:runFromCmd

rem
rem Creating date time stamp for log names
rem
set temptime=%time%
set temptime=%temptime:.=_%
set temptime=%temptime::=_%
set temptime=%temptime: =%

set tempdate=%date%
set tempdate=%tempdate:/=_%
set tempdate=%tempdate: =_%

set timestamp=date_%tempdate%-time_%temptime%

rem
rem Recording current file system/folder location
rem
echo.>>%SAVCleanWipe%
if exist CurrDir.txt del /q CurrDir.txt
set CurrDir=
cd > CurrDir.txt
for /f "delims=$" %%a in (CurrDir.txt) do set CurrDir=%%a
echo CleanWipe is running from "%CurrDir%" >> %SAVCleanWipe%
if exist CurrDir.txt del /q CurrDir.txt
echo.>>%SAVCleanWipe%
echo *****************************************************************>> %SAVCleanWipe%
echo *****************************************************************>> %SAVCleanWipe%

rem
rem parse cmdline arguments    { -silent -restart -debug -showprogress }
rem 
set silent=0
set debug=0
set restart=0
set showprogress=0
set WARN_FIRST=1

if /i "%1" == "-silent" set silent=1 & set WARN_FIRST=0
if /i "%2" == "-silent" set silent=1 & set WARN_FIRST=0
if /i "%3" == "-silent" set silent=1 & set WARN_FIRST=0
if /i "%4" == "-silent" set silent=1 & set WARN_FIRST=0

if /i "%1" == "-debug" set debug=1
if /i "%2" == "-debug" set debug=1
if /i "%3" == "-debug" set debug=1
if /i "%4" == "-debug" set debug=1

if /i "%1" == "-restart" set restart=1
if /i "%2" == "-restart" set restart=1
if /i "%3" == "-restart" set restart=1
if /i "%4" == "-restart" set restart=1

if /i "%1" == "-restart2" set restart=2
if /i "%2" == "-restart2" set restart=2
if /i "%3" == "-restart2" set restart=2
if /i "%4" == "-restart2" set restart=2

if /i "%1" == "-showprogress" set showprogress=1
if /i "%2" == "-showprogress" set showprogress=1
if /i "%3" == "-showprogress" set showprogress=1
if /i "%4" == "-showprogress" set showprogress=1

call :GetOS
echo ProgramFilesX=%ProgramFilesX% >> %SAVCleanWipe%
echo OS=%OS% >> %SAVCleanWipe%
echo OS_64=%OS_64% >> %SAVCleanWipe%

call :CleanupRunOnce
call :SetupEnvironment


rem *********************************
rem These will be the default settings for a SILENT run unless edited.
rem -----------------------------
rem   Settings Configuration
rem
rem     1=enable, 0=disable
rem -----------------------------
set Call_MSI=1
set REMOVE_DRVRS=1
set REMOVE_DRVRS_RST=0
set REMOVE_LIVEUPDATE=1
set REMOVE_SYMEVENT=1
set REMOVE_DEFS=1
set REMOVE_WINDOWS_FILES=1
set REBOOT=0
set Detailed_Search=1
rem *********************************

rem
rem on a restart we don't want to run MSI or SPM Uninstall again
rem
if %restart%==0 goto :notRestart
set Call_MSI=0
set REMOVE_DRVRS=0
set REMOVE_DRVRS_RST=%REMOVE_DRVRS_RST2%
set REMOVE_LIVEUPDATE=%REMOVE_LIVEUPDATE2%
set REMOVE_SYMEVENT=%REMOVE_SYMEVENT2%
set REMOVE_DEFS=%REMOVE_DEFS2%
set REMOVE_WINDOWS_FILES=%REMOVE_WINDOWS_FILES2%
set Detailed_Search=%Detailed_Search2%
if %restart%==2 goto :StartWork
set REBOOT=%REBOOT2%
goto :ListSettings
:notRestart

rem Silent = %Silent%
rem Warn_First = %Warn_First%
if %SILENT%==1 set REBOOT=1 & set Detailed_Search=1 & goto :ListSettings

rem *************************************************************************************
rem Interactive_Mode
rem
rem  Ask the user a series of questions about this run of silent install.
rem
rem    Run uninstall? and if so restart SAVCleanWipe post restart of the computer.
rem
rem
rem *************************************************************************************
:Interactive_Mode

set CLEANWIPE_TITLE=CleanWipe
for /f "delims=$" %%a in (version.txt) do set CLEANWIPE_TITLE=%%a

rem
rem Splash Screen
rem
esugdlgcontrol -title "%CLEANWIPE_TITLE%" -eula -file CleanWipeOpen.txt
if %errorlevel%==1 goto :exit2

echo   [INTERACTIVE MODE SELECTED] >> %SAVCleanWipe%

:MakeUserSettings
if exist User_Settings.txt del /q User_Settings.txt

set Call_MSI=0
set REMOVE_DRVRS=0
set REMOVE_DRVRS_RST=0
set REMOVE_DEFS=0
set REMOVE_SYMEVENT=0
set REMOVE_LIVEUPDATE=0
set REMOVE_WINDOWS_FILES=0
set Detailed_Search=0
set REBOOT=0

if exist User_Settings.txt del /q User_Settings.txt

rem
rem skip asking to run MSI Uninstall if no product uninstaller was found
rem

if "%APPCode%"=="" echo No Symantec Product code found.  Skipping uninstall menu question.>> %SAVCleanWipe% & goto :skipAskUninstall

if "%APPCode2%"=="" goto :1AppFound

if "%APPCode3%"=="" goto :2AppFound

if "%APPCode4%"=="" goto :3AppFound

if "%APPCode5%"=="" goto :4AppFound

if "%APPCode6%"=="" goto :5AppFound

:6AppFound
esugdlgcontrol -title "Uninstall Applications" -msg "Do you want CleanWipe to uninstall %SAVProduct%, %SAVProduct2%, %SAVProduct3%, %SAVProduct4%, %SAVProduct5% and %SAVProduct6%? This will force a restart with CleanWipe automatically resuming processing." -confirm
if %errorlevel%==0 (set Call_MSI=1) & (echo User selected to uninstall % %SAVProduct%, %SAVProduct2%, %SAVProduct3%, %SAVProduct4%, %SAVProduct5% and %SAVProduct6%%. >> %SAVCleanWipe%) else echo User selected not uninstall %SavProduct%, %SAVProduct2%, %SAVProduct3%, %SAVProduct4%, %SAVProduct5% and %SAVProduct6%. >> %SAVCleanWipe%
goto :skipAskUninstall

:5AppFound
esugdlgcontrol -title "Uninstall Applications" -msg "Do you want CleanWipe to uninstall %SAVProduct%, %SAVProduct2%, %SAVProduct3%, %SAVProduct4% and %SAVProduct5%? This will force a restart with CleanWipe automatically resuming processing." -confirm
if %errorlevel%==0 (set Call_MSI=1) & (echo User selected to uninstall %SAVProduct%, %SAVProduct2%, %SAVProduct3%, %SAVProduct4% and %SAVProduct5%. >> %SAVCleanWipe%) else echo User selected not uninstall %SavProduct%, %SAVProduct2%, %SAVProduct3%, %SAVProduct4% and %SAVProduct5%. >> %SAVCleanWipe%
goto :skipAskUninstall

:4AppFound
esugdlgcontrol -title "Uninstall Applications" -msg "Do you want CleanWipe to uninstall %SAVProduct%, %SAVProduct2%, %SAVProduct3% and %SAVProduct4%? This will force a restart with CleanWipe automatically resuming processing." -confirm
if %errorlevel%==0 (set Call_MSI=1) & (echo User selected to uninstall %SAVProduct%, %SAVProduct2%, %SAVProduct3% and %SAVProduct4%%. >> %SAVCleanWipe%) else echo User selected not uninstall %SavProduct%, %SAVProduct2%, %SAVProduct3% and %SAVProduct4%. >> %SAVCleanWipe%
goto :skipAskUninstall

:3AppFound
esugdlgcontrol -title "Uninstall Applications" -msg "Do you want CleanWipe to uninstall %SAVProduct%, %SAVProduct2% and %SAVProduct3%? This will force a restart with CleanWipe automatically resuming processing." -confirm
if %errorlevel%==0 (set Call_MSI=1) & (echo User selected to uninstall %SAVProduct%, %SAVProduct2% and %SAVProduct3%. >> %SAVCleanWipe%) else echo User selected not uninstall %SavProduct%, %SAVProduct2% and %SAVProduct3%. >> %SAVCleanWipe%
goto :skipAskUninstall

:2AppFound
esugdlgcontrol -title "Uninstall Applications" -msg "Do you want CleanWipe to uninstall %SAVProduct% and %SAVProduct2%? This will force a restart with CleanWipe automatically resuming processing." -confirm
if %errorlevel%==0 (set Call_MSI=1) & (echo User selected to uninstall %SavProduct% and %SAVProduct2%. >> %SAVCleanWipe%) else echo User selected not uninstall %SavProduct% and %SAVProduct2%. >> %SAVCleanWipe%
goto :skipAskUninstall

:1AppFound
esugdlgcontrol -title "Uninstall Applications" -msg "Do you want CleanWipe to uninstall %SAVProduct%? This will force a restart with CleanWipe automatically resuming processing." -confirm
if %errorlevel%==0 (set Call_MSI=1) & (echo User selected to uninstall %SavProduct% >> %SAVCleanWipe%) else echo User selected not uninstall %SavProduct% >> %SAVCleanWipe%


:skipAskUninstall

set REMOVE_DRVRS=1
set REMOVE_DEFS=1
set REMOVE_SYMEVENT=1
set REMOVE_WINDOWS_FILES=1

if %Call_MSI%==0 goto :skipAskUninstall1
esugdlgcontrol -title "Uninstall LiveUpdate" -msg "If LiveUpdate remains after uninstalling Symantec products do you want to uninstall LiveUpdate?" -confirm
if %errorlevel%==0 set REMOVE_LIVEUPDATE=1 & echo User selected to remove LiveUpdate. >> %SAVCleanWipe%
goto :skipAskUninstall2
:skipAskUninstall1
esugdlgcontrol -title "Uninstall LiveUpdate" -msg "Do you want to uninstall LiveUpdate?" -confirm
if %errorlevel%==0 set REMOVE_LIVEUPDATE=1 & echo User selected to remove LiveUpdate. >> %SAVCleanWipe%
:skipAskUninstall2

esugdlgcontrol -title "Detailed MSI Product Code Search" -msg "Do you want to do a detailed MSI Product Code registry search? It can take up to 30 minutes." -confirm
if %errorlevel%==0 set Detailed_Search=1 & echo User selected to do a detailed MSI product code search. >> %SAVCleanWipe%

set REBOOT=1

rem *************************************************************************************
rem ListSettings
rem
rem  Show the settings and get user approval
rem
rem *************************************************************************************
:ListSettings
rem
rem First echo the settings to log file
rem 
echo.>> %SAVCleanWipe%
echo ----------------------------- >> %SAVCleanWipe%
echo Options set at start of run. >> %SAVCleanWipe%
echo ...1=enable, 0=disable... >> %SAVCleanWipe%
echo ----------------------------- >> %SAVCleanWipe%
echo.>> %SAVCleanWipe%
echo Call_MSI = %Call_MSI% >> %SAVCleanWipe%
echo REMOVE_DRVRS = %REMOVE_DRVRS% >> %SAVCleanWipe%
echo REMOVE_DRVRS_RST = %REMOVE_DRVRS_RST% >> %SAVCleanWipe%
echo REMOVE_LIVEUPDATE = %REMOVE_LIVEUPDATE% >> %SAVCleanWipe%
echo REMOVE_SYMEVENT = %REMOVE_SYMEVENT% >> %SAVCleanWipe%
echo REMOVE_DEFS = %REMOVE_DEFS% >> %SAVCleanWipe%
echo REMOVE_WINDOWS_FILES = %REMOVE_WINDOWS_FILES% >> %SAVCleanWipe%
echo RESTART = %REBOOT% >> %SAVCleanWipe%
echo WARN_FIRST = %WARN_FIRST% >> %SAVCleanWipe%
echo Detailed_Search = %Detailed_Search% >> %SAVCleanWipe%
echo SILENT = %SILENT% >> %SAVCleanWipe%
echo.>> %SAVCleanWipe%

rem
rem Now if we are ruinning silent (or after a restart) we automatically accept the settings
rem and go on to starting the work
rem
if %silent%==1 goto :StartWork
if NOT %restart%==0 goto :StartWork

rem
rem if not running silent we echo the settings to the screen and 
rem ask for user to confirm.
rem
rem   This also echos the settings to User_Settings.txt file.
rem 
if %Call_MSI%==0 goto :skipEchoUninstall
echo Uninstall %SavProduct% = %Call_MSI% >> User_Settings.txt
if "%SavProduct2%"=="" goto :skipEchoUninstall
echo Uninstall %SavProduct2% = %Call_MSI% >> User_Settings.txt
if "%SavProduct3%"=="" goto :skipEchoUninstall
echo Uninstall %SavProduct3% = %Call_MSI% >> User_Settings.txt
if "%SavProduct4%"=="" goto :skipEchoUninstall
echo Uninstall %SavProduct4% = %Call_MSI% >> User_Settings.txt
if "%SavProduct5%"=="" goto :skipEchoUninstall
echo Uninstall %SavProduct5% = %Call_MSI% >> User_Settings.txt
if "%SavProduct6%"=="" goto :skipEchoUninstall
echo Uninstall %SavProduct6% = %Call_MSI% >> User_Settings.txt
:skipEchoUninstall
echo Remove DRIVERS = %REMOVE_DRVRS% >> User_Settings.txt
echo Remove LIVEUPDATE = %REMOVE_LIVEUPDATE% >> User_Settings.txt
echo Remove SYMEVENT = %REMOVE_SYMEVENT% >> User_Settings.txt
echo Remove Virus Definitions = %REMOVE_DEFS% >> User_Settings.txt
echo Remove Windows files = %REMOVE_WINDOWS_FILES% >> User_Settings.txt
echo RESTART = %REBOOT% >> User_Settings.txt
echo Verbose (On screen) output = %WARN_FIRST% >> User_Settings.txt
echo Detailed MSI Product Code Search = %Detailed_Search% >> User_Settings.txt
echo SILENT = %SILENT% >> User_Settings.txt

esugdlgcontrol -title "Selected User Settings" -msg "Are the following settings correct?" -eula -file User_Settings.txt
if %errorlevel%==0 goto :ApprovedUserSettings

esugdlgcontrol -title "%CLEANWIPE_TITLE%" -msg "Do you wish to cancel CleanWipe?" -confirm
if %errorlevel%==0 goto :exit2
goto :MakeUserSettings


:ApprovedUserSettings
if exist User_Settings.txt del /q User_Settings.txt
echo User approved of the above settings. >> %SAVCleanWipe%

rem *************************************************************************************
rem StartWork
rem
rem  Prior to hitting this label no actual work should be done:
rem 
rem     Only Display of Splash Screen, User Prompts which set environment vars
rem 
rem *************************************************************************************
:StartWork

if %restart%==2 goto :StartWork2

if %restart%==1 goto :StartWork1

REM Need to unregister these before the files are removed
call :Unregister_DLLs
call :Restore_Network_Settings

:StartWork1

call :Run_MSI
call :Remove_Drivers
rem
rem Next lines needed because we want the forced restart to happen before continuing into any additional calls.
rem

REM Perform if ((%Call_MSI%==1 AND %NEEDREBOOTFORMSI%==1) OR %REMOVE_DRVRS%==1)
IF NOT %REMOVE_DRVRS%==1 (
  IF NOT %Call_MSI%==1  goto :SkipReboot
  IF NOT %NEEDREBOOTFORMSI%==1  goto :SkipReboot
)

   echo =========================== >> %SAVCleanWipe%
   echo Restart the computer... >> %SAVCleanWipe%
   if %WARN_FIRST%==1 (
      echo ===========================
      echo Restart the computer... 
      echo ===========================
      echo.
      esugdlgcontrol -title "Restart Computer" -msg "CleanWipe is going to restart the system." -warn
   )

   ESUGUnEn /LF"logs\Restart.log" /reboot /force
   goto :eof


:SkipReboot
call :Remove_Win32Services
call :Shutdown_Win32Services
call :Terminate_Processes
call :Remove_Firewall_Exceptions
call :Remove_LiveUpdate
call :Clean_MSI_Registry
call :Remove_SYMEvent
call :Remove_Website
call :Remove_VirusDefinitions
call :ModifyRegistryKeyValues
call :RemoveTeefer2
call :Remove_DatabaseItems
call :Remove_WindowsFiles
call :Remove_ApplicationFiles
call :Remove_ApplicationFiles2
call :Remove_EmptyFolders

goto :exit

:StartWork2
call :Remove_ApplicationFiles2
call :Remove_EmptyFolders

goto :exit

rem *************************************************************************************
rem *************************************************************************************
rem **********     Subroutines     ******************************************************
rem *************************************************************************************
rem *************************************************************************************

rem *************************************************************************************
rem GetOS
rem
rem  Determine operating system.
rem
rem  Note: CleanWipe does not support OS versions prior to Windows NT.
rem        If we detect such OS version here we log a message and exit.
rem
rem    The following ENV Variables are set:
rem
rem       %ProgramFilesX%    ( example: C:\Program Files )
rem       %OS_64%            ( 1 for 64 bit )
rem
rem       Note: OS should already be present in the ENV    ( Windows_NT is the only supported OS )
rem
rem *************************************************************************************
:GetOS
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :GetOS >> %SAVCleanWipe% 
set ProgramFilesX=""
set OS_64=0

REM ESUGUnEn2 /IS64BITOS returns zero for 32-bit OS and 1 for 64-bit OS
ESUGUnEn2 /LF"%SAVCleanWipe%" /IS64BITOS
set LargeOS=SOFTWARE
if %errorlevel%==1 set LargeOS=SOFTWARE\Wow6432Node

if "%LargeOS%"=="SOFTWARE\Wow6432Node" set OS_64=1
if "%LargeOS%"=="SOFTWARE\Wow6432Node" echo Found OS is 64 bit. >> %SAVCleanWipe%
if "%LargeOS%"=="SOFTWARE\Wow6432Node" Set ProgramFilesX=%ProgramFiles(x86)%

if "%LargeOS%"=="SOFTWARE" set ProgramFilesX=%ProgramFiles%

if "%LargeOS%"=="SOFTWARE" (echo Did not detect a 64 bit OS. >> %SAVCleanWipe%) else (echo Detected a 64 bit OS. >> %SAVCleanWipe%)

if "%OS%"=="Windows_NT" goto :WINNT
if %silent%==1 goto :BadOS
esugdlgcontrol -title "%CLEANWIPE_TITLE%" -msg "CleanWipe only supports Windows NT or later operating systems." -warn
:BadOS
echo CleanWipe only supports Windows NT or later operating systems. >> %SAVCleanWipe%
:WINNT

if %debug%==1 echo Leaving :GetOS >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem SetupEnvironment
rem
rem  Removes our WIN32 Registry entries in the RunOnce table
rem
rem *************************************************************************************
:SetupEnvironment
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :SetupEnvironment >> %SAVCleanWipe% 
if %showprogress%==1 echo SetupEnvironment ...

if %WARN_FIRST%==1 echo External step - Dynamically Setting Environment Variables.
echo Dynamically Setting Environment Variables. >> %SAVCleanWipe%
if %restart% neq 0 goto :SetupEnvironment_1

call GenEnVar GenEnVar_in.txt >> %SAVCleanWipe% 2>&1
call GetSepDirs > sepbasedir.txt

echo %%ProgramFiles(x64)%%	%ProgramFiles%>>DeleteFilesEnVar.txt
echo SET ProgramFiles(x64)=%ProgramFiles%>>DynamicSetEnVar.bat

call DynamicSetEnVar.bat

if "%Reporting%"=="" goto :SetupEnvironment_0
set SESM_Folder=%Reporting%\
echo %%SESM_Folder%%	%SESM_Folder%>>DeleteFilesEnVar.txt
echo SET SESM_Folder=%SESM_Folder%>>DynamicSetEnVar.bat

:SetupEnvironment_0
if not "%SAV_Folder%"=="" goto :SetupEnvironment_1
if "%SAVCE%"=="" goto :SetupEnvironment_1
if not exist "%SAVCE%" goto :SetupEnvironment_1
	set SAV_Folder=%SAVCE%\
	echo %%SAV_Folder%%	%SAV_Folder%>>DeleteFilesEnVar.txt
	echo SET SAV_Folder=%SAV_Folder%>>DynamicSetEnVar.bat

:SetupEnvironment_1

call DynamicSetEnVar.bat
rem For the restart=2 case we need to skip the call to FindApplications
rem but we need to add any App folder names to DeleteFilesEnVar.txt
if %restart% neq 2 goto :SetupEnvironment_2
echo %%CommonProgramFiles%%	%CommonProgramFiles%>>DeleteFilesEnVar.txt
echo %%SEA_Folder%%	%SEA_Folder%>>DeleteFilesEnVar.txt
echo %%SPA_Folder%%	%SPA_Folder%>>DeleteFilesEnVar.txt
echo %%SPM_Folder%%	%SPM_Folder%>>DeleteFilesEnVar.txt
echo %%SNAC_Folder%%	%SNAC_Folder%>>DeleteFilesEnVar.txt
echo %%SESM_Folder%%	%SESM_Folder%>>DeleteFilesEnVar.txt
echo %%SCS_Folder%%	%SCS_Folder%>>DeleteFilesEnVar.txt
echo %%SAV_Folder%%	%SAV_Folder%>>DeleteFilesEnVar.txt
echo %%NAV_Folder%%	%NAV_Folder%>>DeleteFilesEnVar.txt
echo %%LU_Folder%%	%LU_Folder%>>DeleteFilesEnVar.txt
goto :SetupEnvironment_3

:SetupEnvironment_2
call :FindApplications

if "%SESM_Folder%" neq "" goto SetupEnvironment_3
if "%Reporting%"=="" goto SetupEnvironment_3
set SESM_Folder=%Reporting%\
echo %%SESM_Folder%%	%SESM_Folder%>>DeleteFilesEnVar.txt
echo SET SESM_Folder=%SESM_Folder%>>DynamicSetEnVar.bat

:SetupEnvironment_3
if exist DeleteFilesEnVar_filtered.txt del DeleteFilesEnVar_filtered.txt
for /f "tokens=1,2* delims=	"  %%s in (DeleteFilesEnVar.txt) do (
	if not "%%t"=="" echo %%s	%%t>> DeleteFilesEnVar_filtered.txt
)
move /y DeleteFilesEnVar_filtered.txt DeleteFilesEnVar.txt
echo. >> %SAVCleanWipe% 
echo *** Contents of SET after variables are set: >> %SAVCleanWipe%
set >> %SAVCleanWipe%
echo. >> %SAVCleanWipe% 

if %debug%==1 echo Leaving :SetupEnvironment >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem CleanupRunOnce
rem
rem  Removes our WIN32 Registry entries in the RunOnce table
rem
rem *************************************************************************************
:CleanupRunOnce
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :CleanupRunOnce >> %SAVCleanWipe% 
if exist "%SystemDrive%\RunTwice.bat" del /q "%SystemDrive%\RunTwice.bat" > NUL
for /l %%a in (1,1,10) do call :CleanupRunOnceAux %%a
echo Removed RunTwice.bat in RunOnce WIN32 Registry. >> %SAVCleanWipe%
if %debug%==1 echo Leaving :CleanupRunOnce >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem CleanupRunOnceAux
rem
rem  This is a helper for CleanRunOnce
rem
rem *************************************************************************************
:CleanupRunOnceAux
set CCnt=
set CCnt=%1
if exist tmplog.txt del /q tmplog.txt
ESUGUnEn2 /LF"./tmplog.txt" /DELVAL"HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Runonce" "ESUGUNENG%CCnt%" > NUL
if exist tmplog.txt del /q tmplog.txt
goto :eof


rem *************************************************************************************
rem SetupRunOnce
rem
rem  Creates the RunTwice.bat file in the root folder and its 
rem  corresponding entry in the WIN32 Retistry RunOnce table.
rem
rem *************************************************************************************
:SetupRunOnce
call :CleanupRunOnce
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :SetupRunOnce >> %SAVCleanWipe% 
echo Creating RunTwice.bat and Making the entry in RunOnce WIN32 Registry.>> %SAVCleanWipe%
echo.@echo off > %SystemDrive%\RunTwice.bat
echo.%CurrDir:~0,2% >> %SystemDrive%\RunTwice.bat
echo.cd "%CurrDir%" >> %SystemDrive%\RunTwice.bat

echo.set CommonProgramFiles=%CommonProgramFiles%>> %SystemDrive%\RunTwice.bat
echo.set SEA_Folder=%SEA_Folder%>> %SystemDrive%\RunTwice.bat
echo.set SPA_Folder=%SPA_Folder%>> %SystemDrive%\RunTwice.bat
echo.set SPM_Folder=%SPM_Folder%>> %SystemDrive%\RunTwice.bat
echo.set SNAC_Folder=%SNAC_Folder%>> %SystemDrive%\RunTwice.bat
echo.set SESM_Folder=%SESM_Folder%>> %SystemDrive%\RunTwice.bat
echo.set SCS_Folder=%SCS_Folder%>> %SystemDrive%\RunTwice.bat
echo.set SAV_Folder=%SAV_Folder%>> %SystemDrive%\RunTwice.bat
echo.set NAV_Folder=%NAV_Folder%>> %SystemDrive%\RunTwice.bat
echo.set LU_Folder=%LU_Folder%>> %SystemDrive%\RunTwice.bat
echo.set ProgramFiles(x64)=%ProgramFiles%>> %SystemDrive%\RunTwice.bat

echo.set REMOVE_DRVRS_RST2=%REMOVE_DRVRS% >> %SystemDrive%\RunTwice.bat
echo.set REMOVE_LIVEUPDATE2=%REMOVE_LIVEUPDATE% >> %SystemDrive%\RunTwice.bat
echo.set REMOVE_SYMEVENT2=%REMOVE_SYMEVENT% >> %SystemDrive%\RunTwice.bat
echo.set REMOVE_DEFS2=%REMOVE_DEFS% >> %SystemDrive%\RunTwice.bat
echo.set REMOVE_WINDOWS_FILES2=%REMOVE_WINDOWS_FILES% >> %SystemDrive%\RunTwice.bat
echo.set REBOOT2=%REBOOT% >> %SystemDrive%\RunTwice.bat
echo.set Detailed_Search2=%Detailed_Search% >> %SystemDrive%\RunTwice.bat

if %silent%==0 goto :SetupRunOnce2
if %1==-restart2 goto :SetupRunonceA
if %showprogress%==1 goto :SetupRunOnceC
if %debug%==0 echo.start /min RunCleanWipe.bat -silent -restart >> %SystemDrive%\RunTwice.bat
if %debug%==1 echo.start /min RunCleanWipe.bat -silent -restart -debug >> %SystemDrive%\RunTwice.bat
goto :SetupRunOnce3

:SetupRunonceA
if %showprogress%==1 goto :SetupRunOnceD
if %debug%==0 echo.start /min RunCleanWipe.bat -silent -restart2 >> %SystemDrive%\RunTwice.bat
if %debug%==1 echo.start /min RunCleanWipe.bat -silent -restart2 -debug >> %SystemDrive%\RunTwice.bat
goto :SetupRunOnce3

:SetupRunOnce2
if %1==-restart2 goto :SetupRunonceB
if %showprogress%==1 goto :SetupRunOnceE
if %debug%==0 echo.start /min RunCleanWipe.bat -restart >> %SystemDrive%\RunTwice.bat
if %debug%==1 echo.start /min RunCleanWipe.bat -restart -debug >> %SystemDrive%\RunTwice.bat
goto :SetupRunOnce3

:SetupRunonceB
if %showprogress%==1 goto :SetupRunOnceF
if %debug%==0 echo.start /min RunCleanWipe.bat -restart2 >> %SystemDrive%\RunTwice.bat
if %debug%==1 echo.start /min RunCleanWipe.bat -restart2 -debug >> %SystemDrive%\RunTwice.bat
goto :SetupRunOnce3

:SetupRunOnceC
if %debug%==0 echo.start /min RunCleanWipe.bat -silent -restart -showprogress >> %SystemDrive%\RunTwice.bat
if %debug%==1 echo.start /min RunCleanWipe.bat -silent -restart -debug -showprogress >> %SystemDrive%\RunTwice.bat
goto :SetupRunOnce3

:SetupRunOnceD
if %debug%==0 echo.start /min RunCleanWipe.bat -silent -restart2 -showprogress >> %SystemDrive%\RunTwice.bat
if %debug%==1 echo.start /min RunCleanWipe.bat -silent -restart2 -debug -showprogress >> %SystemDrive%\RunTwice.bat
goto :SetupRunOnce3

:SetupRunOnceE
if %debug%==0 echo.start /min RunCleanWipe.bat -restart -showprogress >> %SystemDrive%\RunTwice.bat
if %debug%==1 echo.start /min RunCleanWipe.bat -restart -debug -showprogress >> %SystemDrive%\RunTwice.bat
goto :SetupRunOnce3

:SetupRunOnceF
if %debug%==0 echo.start /min RunCleanWipe.bat -restart2 -showprogress >> %SystemDrive%\RunTwice.bat
if %debug%==1 echo.start /min RunCleanWipe.bat -restart2 -debug -showprogress >> %SystemDrive%\RunTwice.bat
goto :SetupRunOnce3

:SetupRunOnce3
echo.del /q %SystemDrive%\RunTwice.bat >> %SystemDrive%\RunTwice.bat

ESUGUNEN2 /lf"%SAVCleanWipe%" /RUNONCE"%SystemDrive%\RunTwice.bat"
if %debug%==1 echo Leaving :SetupRunOnce >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof

rem *************************************************************************************
rem Remove_Drivers
rem
rem  Shutdown and then uninstall any drivers that require a reboot to remove
rem
rem *************************************************************************************
:Remove_Drivers
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Remove_Drivers >> %SAVCleanWipe% 
if %showprogress%==1 echo Removing Drivers ...
if %REMOVE_DRVRS%==0 if %REMOVE_DRVRS_RST%==0 echo *** Drivers will not be shutdown and removed.>> %SAVCleanWipe%  & goto :RemoveDrvrs_SKIP
echo.>>%SAVCleanWipe%


if %REMOVE_DRVRS%==1 (
   if %WARN_FIRST%==1 echo External step - Removing Drivers.
   echo Removing drivers... >> %SAVCleanWipe%
   call :SetupRunOnce -restart
   for /f "delims=" %%s in (DisableAndRemoveServices.txt) do (
      echo Disabling %%s >> %SAVCleanWipe% 2>&1
      sc sdset "%%s" "D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;IU)(A;;CCLCSWLOCRRC;;;SU)" >> %SAVCleanWipe% 2>&1
      sc config "%%s" start= disabled >> %SAVCleanWipe% 2>&1
	  if %errorlevel%==0 (
		reg add "HKLM\SYSTEM\CurrentControlSet\Services\%%s" /v DeleteFlag /t REG_DWORD /d 1 /f > nul 2>&1
		sc delete "%%s" >> %SAVCleanWipe% 2>&1
	  )
   )
   ESUGUnEn2 /LF"%SAVCleanWipe%" /DELKEY"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\SYMTDI"
   echo deleting "%SystemRoot%\system32\drivers\SymTdi.sys" >> %SAVCleanWipe% 2>&1
   DEL /F "%SystemRoot%\system32\drivers\SymTdi.sys" >> %SAVCleanWipe% 2>&1
   ESUGUnEn2 /LF"%SAVCleanWipe%" /DELSISNAT
   reg delete "HKLM\SYSTEM\CurrentControlSet\Services\SepTempInstaller" /f > nul 2>&1
) else if %REMOVE_DRVRS_RST%==1 (
   if %WARN_FIRST%==1 echo External step - Removing Driver Files.
   echo Removing driver files. >> %SAVCleanWipe%
   for /f "delims=" %%s in (DisableAndRemoveServices.txt) do (
	   sc delete "%%s" >> %SAVCleanWipe% 2>&1
	   reg delete "HKLM\SYSTEM\CurrentControlSet\Services\%%s" /f > nul 2>&1
   )
   if exist AutoDeleteFiles_Drivers_FOLDER.txt del AutoDeleteFiles_Drivers_FOLDER.txt
   if exist "%SystemRoot%\system32\drivers\SEP" (
	  rd /s /q "%SystemRoot%\system32\drivers\SEP" >> %SAVCleanWipe% 2>&1
   )
   if exist "%SystemRoot%\SysWOW64\drivers\SEP" (
      dir /s /b /a-d "%SystemRoot%\SysWOW64\drivers\SEP" >> AutoDeleteFiles_Drivers_FOLDER.txt
	  dir /s /b /ad "%SystemRoot%\SysWOW64\drivers\SEP" | sort /r >> AutoDeleteFiles_Drivers_FOLDER.txt
	  echo.%SystemRoot%\SysWOW64\drivers\SEP >> AutoDeleteFiles_Drivers_FOLDER.txt
   )
   call DeleteFiles.exe DeleteFiles_in_Drivers_files.txt >> %SAVCleanWipe% 2>&1
   if exist AutoDeleteFiles_Drivers_FOLDER.txt call DeleteFiles.exe AutoDeleteFiles_Drivers_FOLDER.txt >> %SAVCleanWipe% 2>&1
   if exist %SystemRoot%\system32\drivers\srtsp.* del %SystemRoot%\system32\drivers\srtsp.*
   if exist %SystemRoot%\system32\drivers\srtspl.* del %SystemRoot%\system32\drivers\srtspl.*
   if exist %SystemRoot%\system32\drivers\srtspx.* del %SystemRoot%\system32\drivers\srtspx.*
   if exist %SystemRoot%\system32\drivers\srtsp64.* del %SystemRoot%\system32\drivers\srtsp64.*
   if exist %SystemRoot%\system32\drivers\srtspl64.* del %SystemRoot%\system32\drivers\srtspl64.*
   if exist %SystemRoot%\system32\drivers\srtspx64.* del %SystemRoot%\system32\drivers\srtspx64.*
   if exist %SystemRoot%\system32\drivers\sysplant.sys del %SystemRoot%\system32\drivers\sysplant.sys
   if exist %SystemRoot%\system32\drivers\wgx64.sys del %SystemRoot%\system32\drivers\wgx64.sys
   if exist %SystemRoot%\system32\drivers\wpshelper.*.* del %SystemRoot%\system32\drivers\wpshelper.*
   if exist %SystemRoot%\system32\drivers\SYMEVENT64x86.* del %SystemRoot%\system32\drivers\SYMEVENT64x86.*
)

set ModifyRegistryValuesLog=logs\ModifyRegistryValues.log
ESUGUnEn2 /LF"%ModifyRegistryValuesLog%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" ccApp

ESUGUnEn2 /LF"%ModifyRegistryValuesLog%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" SISStatusDlg

ESUGUnEn2 /LF"%ModifyRegistryValuesLog%" /DELVAL"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" SISStatusDlg

reg delete "HKCR\*\shellex\ContextMenuHandlers\LDVPMenu" /f > nul 2>&1

goto :eof

:RemoveDrvrs_SKIP
if %debug%==1 echo Leaving :Remove_Driver >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof

rem *************************************************************************************
rem Run_MSI
rem
rem  Uninstall existing product via installed product's uninstaller.
rem  Outputs NEEDREBOOTFORMSI=1 when we need to force reboot afterwards
rem                             (usually the MSI engine will force one for us)
rem
rem *************************************************************************************
:Run_MSI

call :CleanupRunOnce

set NEEDREBOOTFORMSI=0
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Run_MSI >> %SAVCleanWipe% 
if %Call_MSI%==0 echo *** MSIEXEC will not be used. >> %SAVCleanWipe%  & goto :RunMSI_SKIP
echo.>>%SAVCleanWipe%

echo.>>%SAVCleanWipe%

set ModifyRegistryValuesLog=logs\ModifyRegistryValues.log
ESUGUnEn2 /LF"%ModifyRegistryValuesLog%" /DELKEY"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\InProgress"

rem
rem this code expects that if SPM and also some MSI uninstall for SAV\NAV\SEP\SEPM
rem was found, that SPM will be in APPCode and SAVProduct and the MSI unistall will
rem be in APPCode2 and SAVProduct2
rem 
if not "%APPCode%"=="{DAE05E18-1549-4B01-8A2C-973519A86404}" goto Run_MSI_Continue1
if %WARN_FIRST%==1 echo External step - Uninstall SPM 5.1
echo Calling SPMUnst.bat to uninstall SPM 5.1. >> %SAVCleanWipe%
call SPMUnst.bat

:Run_MSI_Continue1
echo Calling MSIUnst.bat to uninstall all listed Nav/Sav product codes. >> %SAVCleanWipe%
echo See the folder "CleanWipe_MSI_Logs" under "%CurrDir%" for the MSI logs created. >> %SAVCleanWipe%
call MSIUnst.bat
REM NEEDREBOOTFORMSI=0 means don't reboot, NEEDREBOOTFORMSI=1 means need to force reboot afterwards for MSI
if %errorlevel%==1641 (
   echo MSIExec returned with exit code 1641 signalling that the uninstaller forced a restart.  >> %SAVCleanWipe%
   echo.>>%SAVCleanWipe%
   set NEEDREBOOTFORMSI=0
) else (
   set NEEDREBOOTFORMSI=1
   echo The MSI uninstaller did not restart the computer when it was finished! >> %SAVCleanWipe%
   echo.>>%SAVCleanWipe%
)
call :SetupRunOnce -restart
goto :eof

:RunMSI_SKIP
if %debug%==1 echo Leaving :Run_MSI >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
call :SetupRunOnce -restart
goto :eof


rem *************************************************************************************
rem Remove_Win32Services
rem
rem  Removing WIN32 Services
rem
rem *************************************************************************************
:Remove_Win32Services
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Remove_Win32Services >> %SAVCleanWipe% 

if %WARN_FIRST%==1 echo External step - Removing services.
echo Removing services. >> %SAVCleanWipe%
if %showprogress%==1 echo Removing Symantec Win32Services ...

esugreg HKLM SYSTEM\CurrentControlSet\Services\smcservice Description >nul
if %errorlevel%==1 goto :TestForSNAC_Service
if %warn_first%==1 echo --------------- Removing smcservice service.
echo Removing smcservice service >>%SAVCleanWipe% 
esugunen /LF"%SAVCleanWipe%" /delserv"smcservice"

:TestForSNAC_Service
esugreg HKLM SYSTEM\CurrentControlSet\Services\SNAC Description >nul
if %errorlevel%==1 goto :TestForSNACNP_Service
if %warn_first%==1 echo --------------- Removing SNAC service.
echo Removing SNAC service >>%SAVCleanWipe% 
esugunen /LF"%SAVCleanWipe%" /delserv"SNAC"

:TestForSNACNP_Service
esugreg HKLM SYSTEM\CurrentControlSet\Services\SnacNP\NetworkProvider Name >nul
if %errorlevel%==1 goto :TestForSymantecEventMgr_Service
if %warn_first%==1 echo --------------- Removing SnacNP service.
echo Removing SnacNP service >>%SAVCleanWipe% 
esugunen /LF"%SAVCleanWipe%" /delserv"SnacNP"

:TestForSymantecEventMgr_Service
esugreg HKLM SYSTEM\CurrentControlSet\Services\ccEvtMgr Description >nul
if %errorlevel%==1 goto :TestForSymantecSettingsMgr_Service
if %warn_first%==1 echo --------------- Removing ccEvtMgr service.
echo Removing ccEvtMgr service >>%SAVCleanWipe% 
esugunen /LF"%SAVCleanWipe%" /delserv"ccEvtMgr"

:TestForSymantecSettingsMgr_Service
esugreg HKLM SYSTEM\CurrentControlSet\Services\ccSetMgr Description >nul
if %errorlevel%==1 goto :TestForEraserUtilRebootDrv_Service
if %warn_first%==1 echo --------------- Removing ccSetMgr service.
echo Removing ccSetMgr service >>%SAVCleanWipe% 
esugunen /LF"%SAVCleanWipe%" /delserv"ccSetMgr"

:TestForEraserUtilRebootDrv_Service
esugreg HKLM SYSTEM\CurrentControlSet\Services\EraserUtilRebootDrv DisplayName >nul
if %errorlevel%==1 goto :TestFor_Service_done
if %warn_first%==1 echo --------------- Removing EraserUtilRebootDrv service.
echo Removing ccSetMgr service >>%SAVCleanWipe% 
esugunen /LF"%SAVCleanWipe%" /delserv"EraserUtilRebootDrv"

:TestFor_Service_done

if %debug%==1 echo Leaving :Remove_Win32Services >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem Shutdown_Win32Services
rem
rem  Shutting down WIN32 Services
rem
rem *************************************************************************************
:Shutdown_Win32Services
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Shutdown_Win32Services >> %SAVCleanWipe% 
if %showprogress%==1 echo Shutdown Symantec Win32Services ...

if %WARN_FIRST%==1 echo External step - Call ServiceShutdown.exe to shutdown services.
echo Call ServiceShutdown.exe to shutdown services. >> %SAVCleanWipe%
echo results logged to: .\logs\ServiceShutdown_in.txt.log >> %SAVCleanWipe%

call ServiceShutdown ServiceShutdown_in.txt 4000 > NUL

if %debug%==1 echo Leaving :Shutdown_Win32Services >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof

rem *************************************************************************************
rem Terminate_Processes
rem
rem  Terminating Processes
rem
rem *************************************************************************************
:Terminate_Processes
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Terminate_Processes >> %SAVCleanWipe% 
if %showprogress%==1 echo Terminate Processes ...
if %WARN_FIRST%==1 echo External step - Call TerminateProcesses.bat to terminate processes.

echo Call TerminateProcesses.bat to shutdown services. >> %SAVCleanWipe%
echo results logged to: .\logs\TerminateProcesses.log >> %SAVCleanWipe%
call TerminateProcesses.bat

if %debug%==1 echo Leaving :Terminate_Processes >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof

rem *************************************************************************************
rem Remove_Firewall_Exceptions
rem
rem  Removing Firewall Exceptions
rem
rem *************************************************************************************
:Remove_Firewall_Exceptions
echo Removing Windows Firewall Exceptions
echo Removing Windows Firewall Exceptions >> %SAVCleanWipe% 
ver | find " 6." > nul
if %errorlevel% == 0 (
	rem Windows Vista+
	netsh advfirewall firewall delete rule name="SMC Service" >> %SAVCleanWipe% 2>&1
	netsh advfirewall firewall delete rule name="SNAC Service" >> %SAVCleanWipe% 2>&1
	netsh advfirewall firewall delete rule name="Symantec Email" >> %SAVCleanWipe% 2>&1
) else (
	rem Windows 2000/XP/2003
	for /f "tokens=4*" %%a in ('netsh firewall show allowedprogram ^| find /i "SMC Service"') do netsh firewall delete allowedprogram program = "%%b" profile = ALL >> %SAVCleanWipe% 2>&1
	for /f "tokens=4*" %%a in ('netsh firewall show allowedprogram ^| find /i "SNAC Service"') do netsh firewall delete allowedprogram program = "%%b" profile = ALL >> %SAVCleanWipe% 2>&1
	for /f "tokens=4*" %%a in ('netsh firewall show allowedprogram ^| find /i "Symantec Email"') do netsh firewall delete allowedprogram program = "%%b" profile = ALL >> %SAVCleanWipe% 2>&1
)
if %debug%==1 echo Leaving :Remove_Firewall_Exceptions >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof

:Restore_Network_Settings
rem *************************************************************************************
rem Restore_Network_Settings
rem
rem  Restoring RasMan and NetworkProvider order
rem
rem *************************************************************************************
echo Restoring Network settings
echo Restoring Network settings >> %SAVCleanWipe% 

ESUGUnEn2 /LF"%SAVCleanWipe%" /RASMAN
ESUGUnEn2 /LF"%SAVCleanWipe%" /NPORDER

if %debug%==1 echo Leaving :Restore_Network_Settings >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof

rem *************************************************************************************
rem Clean_MSI_Registry
rem
rem  ???
rem
rem *************************************************************************************
:Clean_MSI_Registry
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Clean_MSI_Registry >> %SAVCleanWipe% 
if %WARN_FIRST%==1 echo External step - Cleaning the MSI Registry.
echo Cleaning the MSI Registry. >> %SAVCleanWipe% 
if %showprogress%==1 echo Cleaning the MSI Registry ...

call enumMSI.bat

if exist GenDynData.txt del /q GenDynData.txt
for /F "tokens=1,2  delims=" %%a in (GenDynamicFilesKeys_in.txt) do call :Step4Parse %%a %%b 
goto :DoStep5

:Step4Parse 
set Product=
set Product=%1
set ProdID=
set ProdID=%2
if not defined Product goto :eof
if not defined ProdID goto :eof
for %%a in (removeproductkeys.reg removecomponentkeys.reg removeproductkeys2.reg removecomponentkeys2.reg) do if exist %%a del /q %%a
echo.%Product%	%ProdID%>GenDynamicFilesKeys.txt
call GenDynamicFilesKeys.exe GenDynamicFilesKeys.txt -silent >nul

rem copy DynamicMSIFilesToDelete.txt DynamicMSIFilesToDelete2.txt 
rem copy removeproductkeys.reg removeproductkeys2.reg 
rem copy removecomponentkeys.reg removecomponentkeys2.reg 
rem echo Take a look at GenDynamicFilesKeys.exe output
rem pause
call DeleteFiles.exe DynamicMSIFilesToDelete.txt >> %SAVCleanWipe% 2>&1

if exist removeproductkeys.reg ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"removeproductkeys.reg"
if exist removecomponentkeys.reg ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"removecomponentkeys.reg"
if exist removeproductkeys2.reg ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"removeproductkeys2.reg"
if exist removecomponentkeys2.reg ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"removecomponentkeys2.reg"
if %OS_64%==1 (
	if exist removeproductkeys.reg ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"removeproductkeys.reg" /WOW6432
	if exist removecomponentkeys.reg ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"removecomponentkeys.reg" /WOW6432
	if exist removeproductkeys2.reg ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"removeproductkeys2.reg" /WOW6432
	if exist removecomponentkeys2.reg ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"removecomponentkeys2.reg" /WOW6432
)

for %%a in (removeproductkeys.reg removecomponentkeys.reg removeproductkeys2.reg removecomponentkeys2.reg) do if exist %%a del /q %%a
goto :eof

:DoStep5
ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"SCSKeys.reg"
ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"noquar.reg"
if %OS_64%==1 (
	ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"SCSKeys.reg" /WOW6432
	ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"noquar.reg" /WOW6432
)

rem
rem Note from CF.  I have been unsuccessful in reverse engineering what the /delmsicode
rem                option is doing.  Therefore, I have not coded the /delmsicode option
rem                in the ESUGUnEn2.exe replacement code.  For this reason I have added
rem                code here to conditionally skip these calls on 64Bit OS.
rem

if %OS_64%==1 echo skipping ESUGUnEn /delmsicode calls on 64Bit OS >> %SAVCleanWipe% & goto :Skip_delmsicode

echo performing ESUGUnEn /delmsicode calls >> %SAVCleanWipe%

ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"%APPCode%" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{24BF7A02-B60A-494B-843A-793BBC77DED4}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{F7BE9C8A-C2E6-470D-B703-0A1845E6FF8C}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{0EFC6259-3AD8-4CD2-BC57-D4937AF5CC0E}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{68C33091-72FA-4DB8-A40E-8CEAE4552D73}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{7712ADB6-E855-4116-B9E0-1E23BE4E7CE6}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{7D2B86CA-2D5D-469E-92ED-E56B62BD1D3C}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{BD12EB48-DBDF-11D3-BEEA-00A0CC272509}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{C037167E-B804-4CBD-8D52-7D2A84BE543F}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{CD05B6DD-65EC-43AC-BE00-73394F19B3AA}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{1F211E59-C268-4A86-ACC2-5B0CD153C26C}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{D6C64C68-F9F5-11D3-BEEA-00A0CC272509}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{BD12EB47-DBDF-11D3-BEEA-00A0CC272509}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{74BE21DB-FDBD-3D11-EBAE-000ACC725290}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{86C46C6D-5F9F-3D11-EBAE-000ACC725290}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{0EFC6259-3AD8-4CD2-BC57-D4937AF5CC0E}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{7D2B86CA-2D5D-469E-92ED-E56B62BD1D3C}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{5F5A0389-C8D6-49D6-8812-9F7F439E72AC}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{1FB9B789-CFB6-48CD-A579-1D0A30F36435}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{848AC794-8B81-440A-81AE-6474337DB527}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{D1E5AC9D-DA9D-4167-8130-30B59D01F52D}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{00CD72B3-E2DF-4DFC-BCC1-5CC4F564518D}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{A6F7E997-2236-4145-A028-438F2484241A}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{B5FDA445-CAC4-4BA6-A8FB-A7212BD439DE}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{1F211E59-C268-4A86-ACC2-5B0CD153C26C}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{3E172636-AE83-474A-9D07-E31C22C6DDC2}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{790EC520-CCCC-4810-A0FE-061633204CE4}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{5A633ED0-E5D7-4D65-AB8D-53ED43510284}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{0DE336A5-7D5E-56D4-BAD835DE34152048}" > NUL
ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{390E8423-8825-9AC4-3BBA116A57EF1A9F}" > NUL

if "%REMOVE_CQUARANTINE%" == "1" ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{2BEE3B33-03F4-4544-BBD0-F7D2840DF66F}" > NUL
if "%REMOVE_CQUARANTINE%" == "1" ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{86A46236-C44B-4217-81E9-6B691C82E1DD}" > NUL
if "%REMOVE_CQUARANTINE%" == "1" ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{72AC27E9-CB80-4022-B59A-99FDB3BC633F}" > NUL
if "%REMOVE_CQUARANTINE%" == "1" ESUGUnEn /LF"%SAVCleanWipe%" /delmsicode"{D6E79EB8-7619-4E59-A5CE-6DF24E6EA36D}" > NUL

:Skip_delmsicode

if %debug%==1 echo Leaving :Clean_MSI_Registry >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem Remove_SYMEvent
rem
rem  Uninstall SymEvent
rem
rem *************************************************************************************
:Remove_SYMEvent
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Remove_SYMEvent >> %SAVCleanWipe%
if %showprogress%==1 (
if %REMOVE_SYMEVENT%==1 (
 echo Removing SYMEvent ...
)
)
if %REMOVE_SYMEVENT%==0 echo *** SymEvent will not be uninstalled. >> %SAVCleanWipe%  & goto :Remove_SYMEvent_SKIP
 
if %WARN_FIRST%==1 echo External step - Call UninstallSymEvnt.bat to uninstall SymEvent.
set SymEntLog=logs\SymEvntUnst.Log
echo Call UninstallSymEvnt.bat to uninstall SymEvent. >> %SAVCleanWipe%
echo results logged to: %SymEntLog% >> %SAVCleanWipe%
call UninstallSymEvnt.bat
ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"noSymEvent.reg"
if %OS_64%==1 (
	ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"noSymEvent.reg" /WOW6432
)

call DeleteFiles.exe DeleteFiles_in_SymEvent.txt >> %SAVCleanWipe% 2>&1
ESUGUnEn2 /LF"%SymEntLog%" /DELKEY"HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432NODE%\Symantec\Symevent"
ESUGUnEn2 /LF"%SymEntLog%" /DELKEY"HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432NODE%\Symantec\Managed Products\SymEvent"

ESUGUnEn2 /LF"%SymEntLog%" /DELKEY"HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\SymEvent"
ESUGUnEn2 /LF"%SymEntLog%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432NODE%\Microsoft\Windows\CurrentVersion\SharedDLLs" "%windir%\System32\Drivers\SYMEVENT.SYS"
ESUGUnEn2 /LF"%SymEntLog%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432NODE%\Microsoft\Windows\CurrentVersion\SharedDLLs" "%windir%\system32\Drivers\SYMEVENT64x86.SYS"

ESUGUnEn2 /LF"%SymEntLog%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432NODE%\Microsoft\Windows\CurrentVersion\SharedDLLs" "%SYMDIRFILES%\S32EVNT1.DLL"%
ESUGUnEn2 /LF"%SymEntLog%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432NODE%\Microsoft\Windows\CurrentVersion\SharedDLLs" "%SYMDIRFILES%\SYMEVENT.SYS"
ESUGUnEn2 /LF"%SymEntLog%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432NODE%\Microsoft\Windows\CurrentVersion\SharedDLLs" "%SYMDIRFILES%\SYMEVNT.386"
ESUGUnEn2 /LF"%SymEntLog%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432NODE%\Microsoft\Windows\CurrentVersion\SharedDLLs" "%SYMDIRFILES%\SYMEVNT1.DLL"

ESUGUnEn2 /LF"%SymEntLog%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432NODE%\Microsoft\Windows\CurrentVersion\SharedDLLs" "C:\PROGRA~1\Symantec\S32EVNT1.DLL"
ESUGUnEn2 /LF"%SymEntLog%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432NODE%\Microsoft\Windows\CurrentVersion\SharedDLLs" "C:\PROGRA~1\Symantec\SYMEVENT.SYS"
ESUGUnEn2 /LF"%SymEntLog%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432NODE%\Microsoft\Windows\CurrentVersion\SharedDLLs" "C:\PROGRA~1\Symantec\SYMEVNT.386"
ESUGUnEn2 /LF"%SymEntLog%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE%WOW6432NODE%\Microsoft\Windows\CurrentVersion\SharedDLLs" "C:\PROGRA~1\Symantec\SYMEVNT1.DLL"

:Remove_SYMEvent_SKIP
if %debug%==1 echo Leaving :Remove_SYMEvent >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem Unregister_DLLs
rem
rem  Unregister DLL's
rem
rem *************************************************************************************
:Unregister_DLLs
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Unregister_DLLs >> %SAVCleanWipe%
if %showprogress%==1 echo Unregistering DLLs ...
 
if %WARN_FIRST%==1 echo External step - Call UnregisterDLLs.bat to unregister DLL's.
echo Call UnregisterDLLs.bat to unregister DLL's. >> %SAVCleanWipe%
call UnregisterDLLs.bat

if %debug%==1 echo Leaving :Unregister_DLLs >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem Remove_Website
rem
rem  Remove the SEPM Website
rem
rem *************************************************************************************
:Remove_Website
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Remove_Website >> %SAVCleanWipe%

esugreg HKLM SOFTWARE\Microsoft\InetStp InstallPath >nul
if %errorlevel%==1 goto :Remove_SEPM_Websites_SKIP

if %WARN_FIRST%==1 echo External step - Removing Website.
if %showprogress%==1 echo Removing SEPM Website ...
echo running cscript IISConfig.vbs -uninstall. >> %SAVCleanWipe%
cscript IISConfig.vbs -uninstall >nul

:Remove_SEPM_Websites_SKIP
if %debug%==1 echo Leaving :Remove_Website >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************3
rem Remove_ApplicationFiles
rem
rem  Remove Application Files
rem
rem *************************************************************************************
:Remove_ApplicationFiles
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Remove_ApplicationFiles >> %SAVCleanWipe%
if %showprogress%==1 echo Removing Application Files ...
 
if %WARN_FIRST%==1 echo External step - Call DeleteFiles.exe to remove all application files.
echo Call DeleteFiles.exe to remove application files. >> %SAVCleanWipe%

echo Deleting files with randomly generated names...
echo DEL /F "%Symantec Shared Directory%\DecABI\*.tmp"  >> %SAVCleanWipe% 2>&1
DEL /F "%Symantec Shared Directory%\DecABI\*.tmp"  >> %SAVCleanWipe% 2>&1
echo DEL /F "%Symantec Shared Directory%\COH\*.tmp"  >> %SAVCleanWipe% 2>&1
DEL /F "%Symantec Shared Directory%\COH\*.tmp"  >> %SAVCleanWipe% 2>&1
echo DEL /F "%Symantec Shared Directory%\COH\sh*.dll"  >> %SAVCleanWipe% 2>&1
DEL /F "%Symantec Shared Directory%\COH\sh*.dll"  >> %SAVCleanWipe% 2>&1
echo DEL /F "%Common AppData%\Symantec\Symantec Endpoint Protection\Logs\*.log"  >> %SAVCleanWipe% 2>&1
DEL /F "%Common AppData%\Symantec\Symantec Endpoint Protection\Logs\*.log"  >> %SAVCleanWipe% 2>&1
echo DEL /F "%Local AppData%\Symantec\Symantec Endpoint Protection\Logs\*.log"  >> %SAVCleanWipe% 2>&1
DEL /F "%Local AppData%\Symantec\Symantec Endpoint Protection\Logs\*.log"  >> %SAVCleanWipe% 2>&1
echo DEL /F "%Local AppData%\Symantec\Symantec AntiVirus Corporate Edition\7.5\Logs\*.log"  >> %SAVCleanWipe% 2>&1
DEL /F "%Local AppData%\Symantec\Symantec AntiVirus Corporate Edition\7.5\Logs\*.log"  >> %SAVCleanWipe% 2>&1
echo DEL /F "%Local AppData%\Symantec\Symantec AntiVirus Corporate Edition\7.5\Logs\*.log"  >> %SAVCleanWipe% 2>&1
DEL /F "%Local AppData%\Symantec\Symantec AntiVirus Corporate Edition\7.5\Logs\*.log"  >> %SAVCleanWipe% 2>&1
echo DEL /F "%Local AppData%\Symantec\Norton AntiVirus Corporate Edition\7.5\Logs\*.log"  >> %SAVCleanWipe% 2>&1
DEL /F "%Local AppData%\Symantec\Norton AntiVirus Corporate Edition\7.5\Logs\*.log"  >> %SAVCleanWipe% 2>&1
echo DEL /F "%SAVCE%\SAVRT\*.tmp"  >> %SAVCleanWipe% 2>&1
DEL /F "%SAVCE%\SAVRT\*.tmp"  >> %SAVCleanWipe% 2>&1
echo DEL /F "%ProgramFiles%\Symantec Client Security\Symantec AntiVirus\SAVRT\*.tmp"  >> %SAVCleanWipe% 2>&1
DEL /F "%ProgramFiles%\Symantec Client Security\Symantec AntiVirus\SAVRT\*.tmp"  >> %SAVCleanWipe% 2>&1

if "%Common AppData%"=="" goto :SkipDeleteCachedInstalls
if exist "%Common AppData%\Symantec\Cached Installs" echo RD /s /q "%Common AppData%\Symantec\Cached Installs" >> %SAVCleanWipe% 2>&1
if exist "%Common AppData%\Symantec\Cached Installs" RD /s /q "%Common AppData%\Symantec\Cached Installs" >> %SAVCleanWipe% 2>&1

:SkipDeleteCachedInstalls

call DeleteFiles.exe DeleteFiles_in_AllApps_Files.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFiles_in_AllApps_Folders.txt >> %SAVCleanWipe% 2>&1

for /f "tokens=3*" %%s in ('reg query ^"HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders^" /v ^"Common Programs^"') do (
	set StartMenuFolder=%%t
)
if exist "%StartMenuFolder%\Symantec Endpoint Protection" 		  	rd /q /s "%StartMenuFolder%\Symantec Endpoint Protection"
if exist "%StartMenuFolder%\Symantec Endpoint Protection Win64" 	rd /q /s "%StartMenuFolder%\Symantec Endpoint Protection Win64"
if exist "%StartMenuFolder%\Symantec Endpoint Protection Manager" 	rd /q /s "%StartMenuFolder%\Symantec Endpoint Protection Manager"
if exist "%StartMenuFolder%\Symantec Network Access Control" 		rd /q /s "%StartMenuFolder%\Symantec Network Access Control"
if exist "%StartMenuFolder%\Symantec Network Access Control Win64" 	rd /q /s "%StartMenuFolder%\Symantec Network Access Control Win64"

if %debug%==1 echo Leaving :Remove_ApplicationFiles >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem Remove_ApplicationFiles2
rem
rem  Remove Application Files2
rem
rem *************************************************************************************
:Remove_ApplicationFiles2
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Remove_ApplicationFiles2 >> %SAVCleanWipe%
if %showprogress%==1 echo Removing ApplicationFiles - 2 ...
 
if %WARN_FIRST%==1 echo External step - Call DeleteFiles.exe to remove application specific files.
echo Call DeleteFiles.exe to remove application files. >> %SAVCleanWipe%

rem must add a test for every line in DeleteFiles_in_AllApps_Files2.txt such that
rem if any of those files exists we do one more restart of the computer.

set MUSTREBOOT=

set FILEMUSTREBOOT=%ProgramFilesDir%\Symantec Network Access\RasSymEap64.dll
call :RebootReqFileDeletes

set FILEMUSTREBOOT=%ProgramFilesDir%\Symantec Network Access\SSSensor.dll
call :RebootReqFileDeletes

set FILEMUSTREBOOT=%ProgramFilesDir%\Symantec Network Access\SymRasMan64.dll
call :RebootReqFileDeletes

set FILEMUSTREBOOT=%SESM_Folder%bin\secars.dll
call :RebootReqFileDeletes

set FILEMUSTREBOOT=%Symantec Shared Directory%\COH\COHClean.dll
call :RebootReqFileDeletes

set FILEMUSTREBOOT=%Symantec Shared Directory%\COH\sH*.dll
call :RebootReqFileDeletes

IF %REBOOT%==0 echo REBOOT Supressed due to user choice 
IF %REBOOT%==0 echo REBOOT Supressed due to user choice >> %SAVCleanWipe%
IF %REBOOT%==0 goto :Remove_ApplicationFiles2_3 
if NOT DEFINED MUSTREBOOT goto Remove_ApplicationFiles2_3

:Remove_ApplicationFiles2_2
if %REBOOT%==1 goto :Remove_ApplicationFiles2_3
echo.================================================== >> %SAVCleanWipe%
echo Changing settings to allow reboots. >> %SAVCleanWipe%
echo.================================================== >> %SAVCleanWipe%
echo.==================================================
echo Changing settings to allow reboots.
echo.==================================================
set REBOOT=1

:Remove_ApplicationFiles2_3

echo #### app folders ##### >> %SAVCleanWipe%
echo SAV_Folder	%SAV_Folder% >> %SAVCleanWipe%
echo SNAC_Folder	%SNAC_Folder% >> %SAVCleanWipe%
echo SESM_Folder	%SESM_Folder% >> %SAVCleanWipe%
echo SPA_Folder	%SPA_Folder% >> %SAVCleanWipe%
echo SPM_Folder	%SPM_Folder% >> %SAVCleanWipe%
echo SEA_Folder	%SEA_Folder% >> %SAVCleanWipe%
echo SCS_Folder	%SCS_Folder% >> %SAVCleanWipe%
echo SSC_Folder	%SSC_Folder% >> %SAVCleanWipe%
echo ######### >> %SAVCleanWipe%

if "%SAV_Folder%"=="" goto :Remove_ApplicationFiles2_3_01
if not exist "%SAV_Folder%" goto :Remove_ApplicationFiles2_3_01
echo Call DeleteFiles.exe to remove application files for SAV_FOLDER. >> %SAVCleanWipe%
if exist AutoDeleteFiles_SAV_FOLDER.txt del AutoDeleteFiles_SAV_FOLDER.txt
if exist "%SAV_FOLDER%\Res" (
   dir /s /b /a-d "%SAV_FOLDER%\Res" >> AutoDeleteFiles_SAV_FOLDER.txt
   dir /s /b /ad "%SAV_FOLDER%\Res" | sort /r >> AutoDeleteFiles_SAV_FOLDER.txt
)
if exist "%SAV_FOLDER%\SmcLU" (
   dir /s /b /a-d "%SAV_FOLDER%\SmcLU" >> AutoDeleteFiles_SAV_FOLDER.txt
   dir /s /b /ad "%SAV_FOLDER%\SmcLU" | sort /r >> AutoDeleteFiles_SAV_FOLDER.txt
)
if exist "%SAV_FOLDER%\..\SmcLU" (
   dir /s /b /a-d "%SAV_FOLDER%\..\SmcLU" >> AutoDeleteFiles_SAV_FOLDER.txt
   dir /s /b /ad "%SAV_FOLDER%\..\SmcLU" | sort /r >> AutoDeleteFiles_SAV_FOLDER.txt
)
if exist AutoDeleteFiles_SAV_FOLDER.txt call DeleteFiles.exe AutoDeleteFiles_SAV_FOLDER.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFiles_SAV_FOLDER.txt >> %SAVCleanWipe% 2>&1
if exist "%SAV_FOLDER%..\CurrentVersion" rmdir "%SAV_FOLDER%..\CurrentVersion"
call DeleteFiles.exe DeleteFolders_SAV_FOLDER.txt >> %SAVCleanWipe% 2>&1

:Remove_ApplicationFiles2_3_01
if "%SNAC_Folder%"=="" goto :Remove_ApplicationFiles2_3_02
if not exist "%SNAC_Folder%" goto :Remove_ApplicationFiles2_3_02
echo Call DeleteFiles.exe to remove application files for SNAC_FOLDER. >> %SAVCleanWipe%
if exist AutoDeleteFiles_SNAC_FOLDER.txt del AutoDeleteFiles_SNAC_FOLDER.txt
if exist "%SNAC_Folder%\SmcLU" (
   dir /s /b /a-d "%SNAC_Folder%\SmcLU" >> AutoDeleteFiles_SAV_FOLDER.txt
   dir /s /b /ad "%SNAC_Folder%\SmcLU" | sort /r >> AutoDeleteFiles_SAV_FOLDER.txt
)
if exist "%SNAC_Folder%\..\SmcLU" (
   dir /s /b /a-d "%SNAC_Folder%\..\SmcLU" >> AutoDeleteFiles_SAV_FOLDER.txt
   dir /s /b /ad "%SNAC_Folder%\..\SmcLU" | sort /r >> AutoDeleteFiles_SAV_FOLDER.txt
)
if exist "%SNAC_Folder%\Res" (
   dir /s /b /a-d "%SNAC_Folder%\Res" >> AutoDeleteFiles_SNAC_FOLDER.txt
   dir /s /b /ad "%SNAC_Folder%\Res" | sort /r >> AutoDeleteFiles_SNAC_FOLDER.txt
)
if exist AutoDeleteFiles_SNAC_FOLDER call DeleteFiles.exe AutoDeleteFiles_SNAC_FOLDER.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFiles_SNAC_FOLDER.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFolders_SNAC_FOLDER.txt >> %SAVCleanWipe% 2>&1

:Remove_ApplicationFiles2_3_02
if "%SESM_Folder%"=="" goto :Remove_ApplicationFiles2_3_03
if not exist "%SESM_Folder%" goto :Remove_ApplicationFiles2_3_03
echo Call DeleteFiles.exe to remove application files for SESM_FOLDER. >> %SAVCleanWipe%
call DeleteFiles.exe DeleteFiles_SEPM_FOLDER.txt >> %SAVCleanWipe% 2>&1
if exist AutoDeleteFiles_SEPM_FOLDER.txt del AutoDeleteFiles_SEPM_FOLDER.txt

rd /s /q "%SESM_Folder%apache"  >> %SAVCleanWipe% 2>&1
if exist "%SESM_Folder%apache" (
   dir /s /b /a-d "%SESM_Folder%apache" >> AutoDeleteFiles_SEPM_FOLDER.txt
   dir /s /b /ad "%SESM_Folder%apache" | sort /r >> AutoDeleteFiles_SEPM_FOLDER.txt
)

rd /s /q "%SESM_Folder%ASA"  >> %SAVCleanWipe% 2>&1
if exist "%SESM_Folder%ASA" (
   dir /s /b /a-d "%SESM_Folder%ASA" >> AutoDeleteFiles_SEPM_FOLDER.txt
   dir /s /b /ad "%SESM_Folder%ASA" | sort /r >> AutoDeleteFiles_SEPM_FOLDER.txt
)
rd /s /q "%SESM_Folder%bin"  >> %SAVCleanWipe% 2>&1
if exist "%SESM_Folder%bin" (
   dir /s /b /a-d "%SESM_Folder%bin" >> AutoDeleteFiles_SEPM_FOLDER.txt
   dir /s /b /ad "%SESM_Folder%bin" | sort /r >> AutoDeleteFiles_SEPM_FOLDER.txt
)
rd /s /q "%SESM_Folder%data"  >> %SAVCleanWipe% 2>&1
if exist "%SESM_Folder%data" (
   dir /s /b /a-d "%SESM_Folder%data" >> AutoDeleteFiles_SEPM_FOLDER.txt
   dir /s /b /ad "%SESM_Folder%data" | sort /r >> AutoDeleteFiles_SEPM_FOLDER.txt
)
rd /s /q "%SESM_Folder%db"  >> %SAVCleanWipe% 2>&1
if exist "%SESM_Folder%db" (
   dir /s /b /a-d "%SESM_Folder%db" >> AutoDeleteFiles_SEPM_FOLDER.txt
   dir /s /b /ad "%SESM_Folder%db" | sort /r >> AutoDeleteFiles_SEPM_FOLDER.txt
)
rd /s /q "%SESM_Folder%Inetpub"  >> %SAVCleanWipe% 2>&1
if exist "%SESM_Folder%Inetpub" (
   dir /s /b /a-d "%SESM_Folder%Inetpub" >> AutoDeleteFiles_SEPM_FOLDER.txt
   dir /s /b /ad "%SESM_Folder%Inetpub" | sort /r >> AutoDeleteFiles_SEPM_FOLDER.txt
)
rd /s /q "%SESM_Folder%jre"  >> %SAVCleanWipe% 2>&1
if exist "%SESM_Folder%jre" (
   dir /s /b /a-d "%SESM_Folder%jre" >> AutoDeleteFiles_SEPM_FOLDER.txt
   dir /s /b /ad "%SESM_Folder%jre" | sort /r >> AutoDeleteFiles_SEPM_FOLDER.txt
)
rd /s /q "%SESM_Folder%Php"  >> %SAVCleanWipe% 2>&1
if exist "%SESM_Folder%Php" (
   dir /s /b /a-d "%SESM_Folder%Php" >> AutoDeleteFiles_SEPM_FOLDER.txt
   dir /s /b /ad "%SESM_Folder%Php" | sort /r >> AutoDeleteFiles_SEPM_FOLDER.txt
)

rd /s /q "%SESM_Folder%spm_backup*"  >> %SAVCleanWipe% 2>&1

rd /s /q "%SESM_Folder%tomcat"  >> %SAVCleanWipe% 2>&1
if exist "%SESM_Folder%tomcat" (
   dir /s /b /a-d "%SESM_Folder%tomcat" >> AutoDeleteFiles_SEPM_FOLDER.txt
   dir /s /b /ad "%SESM_Folder%tomcat" | sort /r >> AutoDeleteFiles_SEPM_FOLDER.txt
)
rd /s /q "%SESM_Folder%Tools"  >> %SAVCleanWipe% 2>&1
if exist "%SESM_Folder%Tools" (
   dir /s /b /a-d "%SESM_Folder%Tools" >> AutoDeleteFiles_SEPM_FOLDER.txt
   dir /s /b /ad "%SESM_Folder%Tools" | sort /r >> AutoDeleteFiles_SEPM_FOLDER.txt
)

call DeleteFiles.exe AutoDeleteFiles_SEPM_FOLDER.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFolders_SEPM_FOLDER.txt >> %SAVCleanWipe% 2>&1

:Remove_ApplicationFiles2_3_03
if "%SPA_Folder%"=="" goto :Remove_ApplicationFiles2_3_04
if not exist "%SPA_Folder%" goto :Remove_ApplicationFiles2_3_04
echo Call DeleteFiles.exe to remove application files for SPA_FOLDER. >> %SAVCleanWipe%
call DeleteFiles.exe DeleteFiles_SPA_FOLDER.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFolders_SPA_FOLDER.txt >> %SAVCleanWipe% 2>&1

:Remove_ApplicationFiles2_3_04
if "%SPM_Folder%"=="" goto :Remove_ApplicationFiles2_3_05
if not exist "%SPM_Folder%" goto :Remove_ApplicationFiles2_3_05
if exist AutoDeleteFiles_SPM_FOLDER.txt del AutoDeleteFiles_SPM_FOLDER.txt
if exist "%SPM_Folder%\tomcat\packages" (
   dir /s /b /a-d "%SPM_Folder%\tomcat\packages" >> AutoDeleteFiles_SPM_FOLDER.txt
   dir /s /b /ad "%SPM_Folder%\tomcat\packages" | sort /r >> AutoDeleteFiles_SPM_FOLDER.txt
)
if exist "%SPM_Folder%\Inetpub" (
   dir /s /b /a-d "%SPM_Folder%\Inetpub" >> AutoDeleteFiles_SPM_FOLDER.txt
   dir /s /b /ad "%SPM_Folder%\Inetpub" | sort /r >> AutoDeleteFiles_SPM_FOLDER.txt
)
if exist "%SPM_Folder%\data" (
   dir /s /b /a-d "%SPM_Folder%\data" >> AutoDeleteFiles_SPM_FOLDER.txt
   dir /s /b /ad "%SPM_Folder%\data" | sort /r >> AutoDeleteFiles_SPM_FOLDER.txt
)

echo Call DeleteFiles.exe to remove application files for SPM_FOLDER. >> %SAVCleanWipe%
if exist AutoDeleteFiles_SPM_FOLDER.txt call DeleteFiles.exe AutoDeleteFiles_SPM_FOLDER.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFiles_SPM_FOLDER.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFolders_SPM_FOLDER.txt >> %SAVCleanWipe% 2>&1

:Remove_ApplicationFiles2_3_05
if "%SEA_Folder%"=="" goto :Remove_ApplicationFiles2_3_06
if not exist "%SEA_Folder%" goto :Remove_ApplicationFiles2_3_06
echo Call DeleteFiles.exe to remove application files for SEA_FOLDER. >> %SAVCleanWipe%
call DeleteFiles.exe DeleteFiles_SEA_FOLDER.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFolders_SEA_FOLDER.txt >> %SAVCleanWipe% 2>&1

:Remove_ApplicationFiles2_3_06
if "%SCS_Folder%"=="" goto :Remove_ApplicationFiles2_3_07
if not exist "%SCS_Folder%" goto :Remove_ApplicationFiles2_3_07
echo Call DeleteFiles.exe to remove application files for SCS_FOLDER. >> %SAVCleanWipe%
call DeleteFiles.exe DeleteFiles_SCS_FOLDER.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFolders_SCS_FOLDER.txt >> %SAVCleanWipe% 2>&1

:Remove_ApplicationFiles2_3_07
if "%SSC_Folder%"=="" goto :Remove_ApplicationFiles2_3_08
if not exist "%SSC_Folder%" goto :Remove_ApplicationFiles2_3_08
echo Call DeleteFiles.exe to remove application files for SSC_FOLDER. >> %SAVCleanWipe%
call DeleteFiles.exe DeleteFiles_SSC_FOLDER.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFolders_SSC_FOLDER.txt >> %SAVCleanWipe% 2>&1

:Remove_ApplicationFiles2_3_08
echo Call DeleteFiles.exe to remove application files for SEP directories. >> %SAVCleanWipe%
if exist AutoDeleteFiles_SEPDirs.txt del AutoDeleteFiles_SEPDirs.txt
for /f "tokens=1,2 delims=^=" %%s in (sepbasedir.txt) do (
	if exist "%%t\.." (
		echo y| cacls "%%t\.." /G administrators:F /T >> %SAVCleanWipe% 2>&1
 		rd /q /s "%%t\.." >> %SAVCleanWipe% 2>&1
 		dir /s /b /a-d "%%t\.." >> AutoDeleteFiles_SEPDirs.txt
 		dir /s /b /ad "%%t\..k" | sort /r >> AutoDeleteFiles_SEPDirs.txt
	)
)
if exist AutoDeleteFiles_SEPDirs.txt call DeleteFiles.exe AutoDeleteFiles_SEPDirs.txt >> %SAVCleanWipe% 2>&1

:Remove_ApplicationFiles2_3_09
regsvr32 /u /s "%Symantec Shared Directory%\COH\COHClean.dll"
echo DEL /F "%Symantec Shared Directory%\COH\sh*.dll"  >> %SAVCleanWipe% 2>&1
DEL /F "%Symantec Shared Directory%\COH\sh*.dll"  >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFiles_in_AllApps_Files2.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFiles_in_AllApps_Folders2.txt >> %SAVCleanWipe% 2>&1


if %debug%==1 echo Leaving :Remove_ApplicationFiles2 >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem Remove_LiveUpdate
rem
rem  Uninstall LiveUpdate
rem
rem *************************************************************************************
:Remove_LiveUpdate
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Remove_LiveUpdate >> %SAVCleanWipe%
if %showprogress%==1 (
if %REMOVE_LIVEUPDATE%==1 (
 echo Uninstalling LiveUpdate ...
)
)

if exist "%SESM_Folder%bin\LuCatalog.exe" call "%SESM_Folder%bin\LuCatalog.exe" -cleanup

if %REMOVE_LIVEUPDATE%==0 echo *** LiveUpdate will not be uninstalled. >> %SAVCleanWipe%  & goto :Remove_LiveUpdate_SKIP

if %WARN_FIRST%==1 echo External step - Removing LiveUpdate.

REM UninstallLiveUpdate.exe returns the LSETUP process return code, or -1 if the process wasn't run.
REM The LSETUP process returns 1 if user selected to cancel the uninstall. In that case we don't run DeleteFiles
if %SILENT%==0 goto :UninstallLiveUpdate2

   call UninstallLiveUpdate.exe -silent >> %SAVCleanWipe%
   echo UninstallLiveUpdate.exe exit code was: %errorlevel% >> %SAVCleanWipe%

   if %errorlevel%==1 goto :Remove_LiveUpdate_SKIP
   goto :UninstallLiveUpdate3

:UninstallLiveUpdate2
   call UninstallLiveUpdate.exe >> %SAVCleanWipe%
   echo UninstallLiveUpdate.exe exit code was: %errorlevel% >> %SAVCleanWipe%

   if %errorlevel%==1 goto :Remove_LiveUpdate_SKIP
   goto :UninstallLiveUpdate3

:UninstallLiveUpdate3
if exist AutoDeleteFiles_in_LiveUpdate.txt del AutoDeleteFiles_in_LiveUpdate.txt
if exist "%Common AppData%\Symantec\LiveUpdate\Downloads" (
   dir /s /b /a-d "%Common AppData%\Symantec\LiveUpdate\Downloads" >> AutoDeleteFiles_in_LiveUpdate.txt
   dir /s /b /ad "%Common AppData%\Symantec\LiveUpdate\Downloads" | sort /r >> AutoDeleteFiles_in_LiveUpdate.txt
)
if exist AutoDeleteFiles_in_LiveUpdate.txt call DeleteFiles.exe AutoDeleteFiles_in_LiveUpdate.txt >> %SAVCleanWipe% 2>&1
call DeleteFiles.exe DeleteFiles_in_LiveUpdate.txt >> %SAVCleanWipe% 2>&1

if not exist "C:\Program Files\Symantec\Cached Installs\{BF56BF57-58CF-4B75-87D6-62E89519A259}\LUSETUP.EXE" goto :SkipLiveUpdateUninstall
echo removing folder: C:\Program Files\Symantec\Cached Installs\{BF56BF57-58CF-4B75-87D6-62E89519A259} >> %SAVCleanWipe% 
RD /s /q "C:\Program Files\Symantec\Cached Installs\{BF56BF57-58CF-4B75-87D6-62E89519A259}"

:SkipLiveUpdateUninstall

ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"nolu.reg"
if %OS_64%==1 (
	ESUGUnEn2 /LF"%SAVCleanWipe%" /DELREG"nolu.reg" /WOW6432
)

:Remove_LiveUpdate_SKIP
if %debug%==1 echo Leaving :Remove_LiveUpdate >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem Remove_VirusDefinitions
rem
rem  Remove Virus Definitions and Decomposers
rem
rem *************************************************************************************
:Remove_VirusDefinitions
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Remove_VirusDefinitions >> %SAVCleanWipe%
if %showprogress%==1 (
if %REMOVE_DEFS%==1 (
 echo Removing Virus Defintions ...
)
)
if %REMOVE_DEFS%==0 echo *** All Virus Definitions and Decomposers will not be removed. >> %SAVCleanWipe%  & goto :Remove_VirusDefinitions_SKIP
 
if %WARN_FIRST%==1 echo External step - All Virus Definitions and Decomposers will be removed.
echo All Virus Definitions and Decomposers will be removed. >> %SAVCleanWipe%

echo DeleteDirectoryTree.bat results logged to: .\logs\DeleteDirectoryTree.log >> %SAVCleanWipe%
echo DeletePostReboot.bat    results logged to: .\logs\DeleteFilesPostRestartLog.log >> %SAVCleanWipe%
call DeleteDirectoryTree.bat
call DeletePostReboot.bat

:Remove_VirusDefinitions_SKIP
if %debug%==1 echo Leaving :Remove_VirusDefinitions >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem Remove_WindowsFiles
rem
rem  Remove files from Windows directories
rem
rem *************************************************************************************
:Remove_WindowsFiles
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Remove_WindowsFiles >> %SAVCleanWipe%
if %showprogress%==1 (
if %REMOVE_WINDOWS_FILES%==1 (
 echo Removing Symantec files under the windows folder ...
)
)
if %REMOVE_WINDOWS_FILES%==0 echo *** Symantec files under the Windows folder will not be deleted. >> %SAVCleanWipe%  & goto :Remove_WindowsFiles_SKIP
 
if %WARN_FIRST%==1 echo External step - Symantec files under the Windows folder will be deleted.
echo Symantec files under the Windows folder will be deleted. >> %SAVCleanWipe%

call DeleteFiles.exe DeleteFiles_in_Windows_files.txt >> %SAVCleanWipe% 2>&1
rem DeleteFiles.exe is 32bit program so it's unable to remove SymVPN.dll from native 64bit Windows directory.
if exist "%SystemRoot%\system32\SymVPN.dll" del "%SystemRoot%\system32\SymVPN.dll" >> %SAVCleanWipe% 2>&1
if exist "%SystemRoot%\system32\sysfer.dll" del "%SystemRoot%\system32\sysfer.dll" >> %SAVCleanWipe% 2>&1
if exist "%SystemRoot%\system32\FwsVPN.dll" del "%SystemRoot%\system32\FwsVPN.dll" >> %SAVCleanWipe% 2>&1
if exist "%SystemRoot%\system32\snacnp.dll" del "%SystemRoot%\system32\snacnp.dll" >> %SAVCleanWipe% 2>&1
if exist "%SystemRoot%\system32\sysferThunk.dll" del "%SystemRoot%\system32\sysferThunk.dll" >> %SAVCleanWipe% 2>&1

if exist "%SystemRoot%\SysWOW64\sysfer.dll" del "%SystemRoot%\SysWOW64\sysfer.dll" >> %SAVCleanWipe% 2>&1
if exist "%SystemRoot%\SysWOW64\FwsVPN.dll" del "%SystemRoot%\SysWOW64\FwsVPN.dll" >> %SAVCleanWipe% 2>&1
if exist "%SystemRoot%\SysWOW64\snacnp.dll" del "%SystemRoot%\SysWOW64\snacnp.dll" >> %SAVCleanWipe% 2>&1
if exist "%SystemRoot%\SysWOW64\sysferThunk.dll" del "%SystemRoot%\SysWOW64\sysferThunk.dll" >> %SAVCleanWipe% 2>&1

:Remove_WindowsFiles_SKIP
if %debug%==1 echo Leaving :Remove_WindowsFiles >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem ModifyRegistryKeyValues
rem
rem  ???
rem
rem *************************************************************************************
:ModifyRegistryKeyValues
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :ModifyRegistryKeyValues >> %SAVCleanWipe%
if %showprogress%==1 echo Restoring Registry Values ...
 
if %WARN_FIRST%==1 echo External step - Modifying registry key values.
echo Modifying registry key values. >> %SAVCleanWipe%

echo ModifyRegistryValues.bat results logged to: .\logs\ModifyRegistryValues.log >> %SAVCleanWipe%
call ModifyRegistryValues.bat

rem
rem Defect 1159159 - RasMan and NetwrokProvider WIN32 Registry values need to be restored to settings
rem                  that were present before SNAC inserted itself in the chain.  This is done by
rem                  calling the RestorePreviousSettings() custom action code in SnacMainCA.dll.
rem
CustomActionWrapper SnacMainCA.dll RestorePreviousSettings
if %errorlevel%==0 goto :ModifyRegistryKeyValues_1
echo SnacMainCA.dll:RestorePreviousSettings - Failure.  >> %SAVCleanWipe%
goto :ModifyRegistryKeyValues_2
:ModifyRegistryKeyValues_1
echo SnacMainCA.dll:RestorePreviousSettings - Success.  >> %SAVCleanWipe%
:ModifyRegistryKeyValues_2

if %debug%==1 echo Leaving :ModifyRegistryKeyValues >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof

rem *************************************************************************************
rem RemoveTeefer2
rem *************************************************************************************
:RemoveTeefer2
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering RemoveTeefer2 >> %SAVCleanWipe%
if %showprogress%==1 echo Uninstalling Teefer ...
 
if %WARN_FIRST%==1 echo External step - Uninstalling Teefer.
echo Uninstalling Teefer. >> %SAVCleanWipe%

ESUGUnEn2 /LF"%SAVCleanWipe%" /DELTEEFER
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof



rem *************************************************************************************
rem Remove_EmptyFolders
rem
rem *************************************************************************************
:Remove_EmptyFolders
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Remove_EmptyFolders >> %SAVCleanWipe%
if %showprogress%==1 echo Removing Empty Folders ...
 
if %WARN_FIRST%==1 echo External step - Removing empty folders.
echo Removing empty folders. >> %SAVCleanWipe%

if "%SAV_Folder%"=="" goto :Remove_EmptyFolders_2
if exist "%SAV_Folder%" GenDynamicFilesKeys -deletefolder "%SAV_Folder%"

:Remove_EmptyFolders_2
if "%SESM_Folder%"=="" goto :Remove_EmptyFolders_3
rem if exist "%SESM_Folder%" 
GenDynamicFilesKeys -deletefolder "%SESM_Folder%"

:Remove_EmptyFolders_3
if "%SNAC_Folder%"=="" goto :Remove_EmptyFolders_4
rem if exist "%SNAC_Folder%" 
GenDynamicFilesKeys -deletefolder "%SNAC_Folder%"

rem
rem Intentionally no test for if "%LU_Folder%"==""
rem because we see that the item in the registry if
rem it exists is already surrounded with quotes. Since
rem we currently have no way to strip the quotest this
rem causes unsolvable problem with using this test.
rem 
:Remove_EmptyFolders_4
GenDynamicFilesKeys -deletefolder %LU_Folder%

:Remove_EmptyFolders_5
if "%ProgramFilesDir%"=="" goto :Remove_EmptyFolders_6
if "%ProgramFilesDir%\Symantec\Cached Installs"=="" goto :Remove_EmptyFolders_6
if exist "%ProgramFilesDir%\Symantec\Cached Installs" RD /s /q "%ProgramFilesDir%\Symantec\Cached Installs"
if exist "%ProgramFilesDir%\Symantec\Cached Installs" GenDynamicFilesKeys -deletefolder "%ProgramFilesDir%\Symantec\Cached Installs"

:Remove_EmptyFolders_6
if exist AutoDeleteFolders_AppData_FOLDER.txt (
   for /f "delims=" %%f in (AutoDeleteFolders_AppData_FOLDER.txt) do if exist "%%f" rd /s /q "%%f" >> %SAVCleanWipe% 
)

:Remove_EmptyFolders_7
for /f "tokens=1,2 delims=^=" %%s in (sepbasedir.txt) do (
	if exist "%%t" (
		rd /q /s "%%t" >> %SAVCleanWipe% 2>&1
	)
)

if %debug%==1 echo Leaving :Remove_EmptyFolders >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem Remove_DatabaseItems
rem
rem *************************************************************************************
:Remove_DatabaseItems
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :Remove_DatabaseItems >> %SAVCleanWipe%
if %showprogress%==1 echo Removing SEPM DatabaseItems ...
if %WARN_FIRST%==1 echo External step - Removing ODBC DSN(s).

echo Removing DSN 'SymantecEndpointSecurityDSN' for driver 'Adaptive Server Anywhere 9.0'. >> %SAVCleanWipe%

ESUGUnEn2 /LF"%SAVCleanWipe%" /DELDSN"SymantecEndpointSecurityDSN" "Adaptive Server Anywhere 9.0"

echo Removing DSN 'SymantecEndpointSecurityDSN' for driver 'SQL Server'. >> %SAVCleanWipe%

ESUGUnEn2 /LF"%SAVCleanWipe%" /DELDSN"SymantecEndpointSecurityDSN" "SQL Server"

ESUGUnEn2 /LF"%SAVCleanWipe%" /DELKEY"HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\SymantecEndpointSecurityDSN"
ESUGUnEn2 /LF"%SAVCleanWipe%" /DELVAL"HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI" "SymantecEndpointSecurityDSN"


if %debug%==1 echo Leaving :Remove_DatabaseItems >> %SAVCleanWipe% 
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
goto :eof


rem *************************************************************************************
rem  FindApplications
rem
rem Locate the install folder for Symantec Applications and add entries in the
rem DeleteFilesEnVar.txt file.
rem
rem *************************************************************************************
:FindApplications
call FindApp.bat
goto :eof

rem *************************************************************************************
rem  RebootReqFileDeletes
rem 
rem  input
rem     FILEMUSTREBOOT - full path to file
rem
rem  output
rem     MUSTREBOOT=1 if file is found
rem
rem Test for the existence of files that require a reboot to clean up, if deleted.
rem Does not actually delete the files.
rem
rem *************************************************************************************
:RebootReqFileDeletes
if NOT EXIST "%FILEMUSTREBOOT%" goto :eof
echo REBOOT Required - Application specific file found.
echo     %FILEMUSTREBOOT%
echo REBOOT Required - Application specific file found. >> %SAVCleanWipe%
echo     %FILEMUSTREBOOT%  >> %SAVCleanWipe%
set MUSTREBOOT=1
goto :eof


rem *************************************************************************************
rem *************************************************************************************
rem **********     Exit handling     ****************************************************
rem *************************************************************************************
rem *************************************************************************************
:exit
if %debug%==1 echo.================================================== >> %SAVCleanWipe% 
if %debug%==1 echo Entering :exit >> %SAVCleanWipe%
call :CleanupRunOnce
if %REBOOT%==0 echo *** End of processing Restart of computer will be skipped. >> %SAVCleanWipe%  & goto :exit1

echo =========================== >> %SAVCleanWipe%
if %WARN_FIRST%==1 echo ===========================
echo Restart the computer... >> %SAVCleanWipe%
if %WARN_FIRST%==1 echo Restart the computer... 
if %WARN_FIRST%==1 echo ===========================
if %WARN_FIRST%==1 echo.

call :SetupRunOnce -restart2

if %WARN_FIRST%==1 (esugdlgcontrol -title "Restart Computer" -msg "CleanWipe is going to restart the system." -warn)
set RestartLog=logs\Restart.log
ESUGUnEn /LF"%RestartLog%" /reboot /force
goto :eof

:exit1
set CLEANWIPE_TITLE=CleanWipe
for /f "delims=$" %%a in (version.txt) do set CLEANWIPE_TITLE=%%a
if %WARN_FIRST%==1 (esugdlgcontrol -title "%CLEANWIPE_TITLE%" -msg "CleanWipe has completed. See the folder (%CurrDir%\logs) for the logs created." -info)

:exit2 
exit

