${SegmentFile}

!define CUSTOM_APPDIR '$AppDirectory\DriverBoosterPro'

Var CUSTOM_LOGGEDINUSER
Var CUSTOM_BOL_HWINFO
Var CUSTOM_HWINFOFILE

!include x64.nsh


${SegmentPre}
	;=== create env. var for %userprofile%\AppData\LocalLow
	StrCpy $0 LOCALLOWAPPDATA
	ReadRegStr $1 HKCU 'SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders' {A520A1A4-1780-4FF6-BD18-167343C5AF16}
	${DebugMsg} "Setting environment variable $0 to $1"
	System::Call Kernel32::SetEnvironmentVariable(tr0,tr1)
!macroend

${SegmentPrePrimary}
	${If} ${RegistryKeyExists} 'HKLM\SYSTEM\CurrentControlSet\services\HWiNFO32'
		StrCpy $CUSTOM_BOL_HWINFO '1' ; we'll use local service
	${Else}
		StrCpy $CUSTOM_BOL_HWINFO '0'
		nsExec::ExecToStack	'"${CUSTOM_APPDIR}\4.5.0\HWiNFO\HWiNFO.exe" /brandname'
	${EndIf}
!macroend

${SegmentPostPrimary}
	;=== delete any scheduled tasks
	;Get user name
	System::Call "advapi32::GetUserName(t .r0, *i ${NSIS_MAX_STRLEN} r1) i.r2"
	StrCpy $CUSTOM_LOGGEDINUSER $0

	${IfThen} ${RunningX64} ${|} ${DisableX64FSRedirection} ${|}
	
		${If} ${FileExists} '$SYSDIR\Tasks\Driver Booster SkipUAC ($CUSTOM_LOGGEDINUSER)'
			nsExec::ExecToStack '$SYSDIR\SCHTASKS.EXE /Delete /TN "Driver Booster SkipUAC ($CUSTOM_LOGGEDINUSER)" /F'
		${EndIf}

		${If} ${FileExists} '$SYSDIR\Tasks\Driver Booster Scheduler'
			nsExec::ExecToStack '$SYSDIR\SCHTASKS.EXE /Delete /TN "Driver Booster Scheduler" /F'
		${EndIf}

	${IfThen} ${RunningX64} ${|} ${EnableX64FSRedirection} ${|}
	
	;=== remove custom service if we run it
	${If} $CUSTOM_BOL_HWINFO != '1'
		; get system file path
		ReadRegStr $0 HKLM 'SYSTEM\CurrentControlSet\services\HWiNFO32' ImagePath
		StrCpy $1 $0 4
		${If} $1 == "\??\"
			StrCpy $CUSTOM_HWINFOFILE $0 "" 4 ;fix path of eg. "\??\C:\Windows\system32\drivers\HWiNFO32.SYS"
		${Else}
			StrCpy $CUSTOM_HWINFOFILE $0
		${EndIf}
		; stop and remove service
		nsExec::ExecToStack '"$SYSDIR\SC.EXE" STOP HWiNFO32'
		nsExec::ExecToStack '"$SYSDIR\SC.EXE" DELETE HWiNFO32'
		; delete registry keys
		DeleteRegKey HKLM 'SYSTEM\ControlSet001\services\HWiNFO32'
		DeleteRegKey HKLM 'SYSTEM\CurrentControlSet\services\HWiNFO32'
		; delete the .sys file
		Delete '$CUSTOM_HWINFOFILE'
	${EndIf}
!macroend
