; Copyleft (c) n/a-n/a Azure Zanculmarktum

!define BUILDDATE "23 octobre 2014"
!define APPNAME "Kerish Doctor"
!define APP "KerishDoctor"
!define APPVER "0.0.0.0"
!define VER "1.0"
!define APPEXE "KerishDoctor.exe"
!define APPDIR "KerishDoctor"
!define RUNASADMIN

; Custom:
!define CUSTOMUI

; === Define Registry ===
!define REGKEY1 "HKEY_CURRENT_USER\Software\Kerish Products\Kerish Doctor"
!define REGKEY2 "HKEY_LOCAL_MACHINE\SOFTWARE\Kerish Products\Kerish Doctor"

; Shell:
!define REGKEYSHELLROOT1 "HKEY_LOCAL_MACHINE"
!define REGKEYSHELL1 "*"       #             \SOFTWARE\Classes\*
!define REGSHELL1 "KDUnlocker" #                                \shell\KDUnlocker

!define REGKEYSHELLROOT2 "HKEY_LOCAL_MACHINE"
!define REGKEYSHELL2 "AllFilesystemObjects" # \SOFTWARE\Classes\AllFilesystemObjects
!define REGSHELL2 "KDShredder"              #                                       \shell\KDShredder

!define REGKEYSHELLROOT3 "HKEY_LOCAL_MACHINE"
!define REGKEYSHELL3 "{645FF040-5081-101B-9F08-00AA002F954E}" # \SOFTWARE\Classes\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}
!define REGSHELL3 "KDFileRecovery"                            #                                                               \shell\KDFileRecovery

!define REGKEYSHELLROOT4 "HKEY_LOCAL_MACHINE"
!define REGKEYSHELL4 "{645FF040-5081-101B-9F08-00AA002F954E}" # \SOFTWARE\Classes\CLSID\{645FF040-5081-101B-9F08-00AA002F954E}
!define REGSHELL4 "KDShredder"                                #                                                               \shell\KDShredder

!define REGKEYSHELLROOT5 "HKEY_LOCAL_MACHINE"
!define REGKEYSHELL5 "exefile" #            \SOFTWARE\Classes\exefile
!define REGSHELL5 "KDAppInfo"  #                                     \shell\KDAppInfo

; === Define Directory ===
; (all users):
!define LOCALDIR1 "$APPDATA\Kerish Products"
!define SUBDIR1 "\Kerish Doctor" ; beginning with back-slash
!define PORTABLEDIR1 "$SETTINGSDIR"

; === Define File ===
!define LOCALFILE1 "$SYSDIR\Tasks\Kerish Doctor"

; === Best Compression ===
SetCompress auto
SetCompressor /SOLID lzma
SetCompressorDictSize 32
SetDatablockOptimize on

; === Include ===
!include Include\Launcher.nsh
!include Include\Macros.nsh

; === Set basic information ===
Name "${APPNAME} Portable"
OutFile "..\..\${APP}Portable.exe"
Icon "${APP}.ico"

Var LNG ; used by UserDefaultLang
Var LOCKEDFILE1
Var LOCKEDFILE2

Function Init
	; === Backup local registry keys ===
	${DetailPrintCustomUI} "Backup local registry keys"
	${registry::BackupLocal} "${REGKEY1}"
	${registry::BackupLocal} "${REGKEY2}"

	registry::_KeyExists /NOUNLOAD "HKEY_CURRENT_USER\Software\Enigma Protector"
	Pop $0
	StrCmp $0 "-1" +3
		CreateDirectory "$EXEDIR\Data"
			WriteINIStr "$EXEDIR\Data\${APP}PortableRuntimeData.ini" "${APP}Portable" "HKCUSoftwareEnigmaProtectorExists" "true"
	registry::_KeyExists /NOUNLOAD "HKEY_LOCAL_MACHINE\SOFTWARE\Server Info"
	Pop $0
	StrCmp $0 "-1" +3
		CreateDirectory "$EXEDIR\Data"
			WriteINIStr "$EXEDIR\Data\${APP}PortableRuntimeData.ini" "${APP}Portable" "HKLMSoftwareServerInfoExists" "true"

	; Shell:
	${DetailPrintCustomUI} "Backup shell registry keys"
	${registry::BackupShell} "${REGKEYSHELLROOT1}" "${REGKEYSHELL1}" "${REGSHELL1}"
	${registry::BackupShell} "${REGKEYSHELLROOT2}" "${REGKEYSHELL2}" "${REGSHELL2}"
	registry::_DeleteKey /NOUNLOAD "${REGKEYSHELLROOT3}\SOFTWARE\Classes\CLSID\${REGSHELL3}-BackupBy${APP}Portable"
	registry::_MoveKey /NOUNLOAD "${REGKEYSHELLROOT3}\SOFTWARE\Classes\CLSID\${REGKEYSHELL3}\shell\${REGSHELL3}" "${REGKEYSHELLROOT3}\SOFTWARE\Classes\CLSID\${REGSHELL3}-BackupBy${APP}Portable"
	registry::_DeleteKey /NOUNLOAD "${REGKEYSHELLROOT4}\SOFTWARE\Classes\CLSID\${REGSHELL4}-BackupBy${APP}Portable"
	registry::_MoveKey /NOUNLOAD "${REGKEYSHELLROOT4}\SOFTWARE\Classes\CLSID\${REGKEYSHELL4}\shell\${REGSHELL4}" "${REGKEYSHELLROOT4}\SOFTWARE\Classes\CLSID\${REGSHELL4}-BackupBy${APP}Portable"
	${registry::BackupShell} "${REGKEYSHELLROOT5}" "${REGKEYSHELL5}" "${REGSHELL5}"

	; === Restore portable registry keys ===
	${DetailPrintCustomUI} "Restore portable registry keys"
	${registry::RestorePortable}

	; === Backup local directory ===
	${DetailPrintCustomUI} "Backup local directory"
	${directory::BackupLocalAll} "${LOCALDIR1}" "${SUBDIR1}" ; APPDATA for all users "X:\Documents and Settings\All Users\Application Data" (WinXP) "X:\ProgramData" (Vista, Win7...)

	; === UserDefaultLang ===
	${DetailPrintCustomUI} "UserDefaultLang"
	; Launcher will change app language according Windows language,
	; see also: http://medicinecollections.zat.su/lngcode.html
	StrCmp $LANGID "1033" "" +2
		StrCpy $LNG "English"
	StrCmp $LANGID "1049" "" +2
		StrCpy $LNG "Russian"
	ReadINIStr $0 "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "UserDefaultLang"
	StrCmp $0 "" "" +3
		WriteINIStr "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "UserDefaultLang" "true"
		StrCpy $0 "true"
	ReadINIStr $1 "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "Language"
	StrCmp $1 "" "" +3
		WriteINIStr "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "Language" "English"
		StrCpy $1 "English"
	StrCmp $0 "true" CheckLanguage Language
	Language:
		StrCpy $LNG "$1"
	CheckLanguage:
		StrCmp $LNG "" DefaultLanguage
			IfFileExists "$DEFAULTDATA\Help\$LNG.chm" "" DefaultLanguage
			IfFileExists "$DEFAULTDATA\Locale\$LNG.lng" "" DefaultLanguage
			IfFileExists "$DEFAULTDATA\Resources\$LNG\*.*" ChangeLanguage DefaultLanguage
	DefaultLanguage:
		StrCpy $LNG "English"
		Goto CheckLanguage
	ChangeLanguage:
		SetShellVarContext all
		CreateDirectory "${LOCALDIR1}${SUBDIR1}\Help"
			CopyFiles /SILENT "$DEFAULTDATA\Help\$LNG.chm" "${LOCALDIR1}${SUBDIR1}\Help"
		CreateDirectory "${LOCALDIR1}${SUBDIR1}\Locale"
			CopyFiles /SILENT "$DEFAULTDATA\Locale\$LNG.lng" "${LOCALDIR1}${SUBDIR1}\Locale"
		CreateDirectory "${LOCALDIR1}${SUBDIR1}\Resources\$LNG"
			CopyFiles /SILENT "$DEFAULTDATA\Resources\$LNG\*.*" "${LOCALDIR1}${SUBDIR1}\Resources\$LNG"
		SetShellVarContext current
		WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Kerish Products\Kerish Doctor" "Language" "$LNG.lng"
		WriteINIStr "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "Language" "$LNG"
		CreateDirectory "$EXEDIR\Data"
			WriteINIStr "$EXEDIR\Data\${APP}PortableRuntimeData.ini" "${APP}Portable" "LastLanguage" "$LNG"

	; === Restore portable directory ===
	${DetailPrintCustomUI} "Restore portable directory"
	SetShellVarContext all
	CreateDirectory "${LOCALDIR1}${SUBDIR1}\Help"
		CopyFiles /SILENT "$DEFAULTDATA\Help\Readme.txt" "${LOCALDIR1}${SUBDIR1}\Help"
	CreateDirectory "${LOCALDIR1}${SUBDIR1}\Database"
		CopyFiles /SILENT "$DEFAULTDATA\Database\Daily.sec" "${LOCALDIR1}${SUBDIR1}\Database"
		CopyFiles /SILENT "$DEFAULTDATA\Database\Daily2.sec" "${LOCALDIR1}${SUBDIR1}\Database"
		CopyFiles /SILENT "$DEFAULTDATA\Database\Daily3.sec" "${LOCALDIR1}${SUBDIR1}\Database"
		CopyFiles /SILENT "$DEFAULTDATA\Database\Daily4.sec" "${LOCALDIR1}${SUBDIR1}\Database"
		CopyFiles /SILENT "$DEFAULTDATA\Database\GPUTemp.dll" "${LOCALDIR1}${SUBDIR1}\Database"
		CopyFiles /SILENT "$DEFAULTDATA\Database\Heuristics.dat" "${LOCALDIR1}${SUBDIR1}\Database"
		CopyFiles /SILENT "$DEFAULTDATA\Database\Keys.lic" "${LOCALDIR1}${SUBDIR1}\Database"
		CopyFiles /SILENT "$DEFAULTDATA\Database\Main.sec" "${LOCALDIR1}${SUBDIR1}\Database"
		CopyFiles /SILENT "$DEFAULTDATA\Database\System.ilg" "${LOCALDIR1}${SUBDIR1}\Database"
		CopyFiles /SILENT "$DEFAULTDATA\Database\System.prt" "${LOCALDIR1}${SUBDIR1}\Database"
	CreateDirectory "${LOCALDIR1}${SUBDIR1}\Resources"
		CopyFiles /SILENT /FILESONLY "$DEFAULTDATA\Resources\*.*" "${LOCALDIR1}${SUBDIR1}\Resources"
	SetShellVarContext current
	${directory::RestorePortableAll} "${LOCALDIR1}" "${SUBDIR1}" "${PORTABLEDIR1}" ; APPDATA for all users "X:\Documents and Settings\All Users\Application Data" (WinXP) "X:\ProgramData" (Vista, Win7...)

	; === Backup local file ===
	${DetailPrintCustomUI} "Backup local file"
	${file::BackupLocal} "${LOCALFILE1}" ; APPDATA for current user "X:\Documents and Settings\%username%\Application Data" (WinXP) "X:\Users\%username%\AppData\Roaming" (Vista, Win7...), also used if LOCALDIR is LOCALAPPDATA

	IfFileExists "$SYSDIR\GPUTemp.dll" "" +3
		CreateDirectory "$EXEDIR\Data"
			WriteINIStr "$EXEDIR\Data\${APP}PortableRuntimeData.ini" "${APP}Portable" "SysDirGPUTempDllExists" "true"

	; === Default settings ===
	${DetailPrintCustomUI} "Default settings"
	WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Kerish Products\Kerish Doctor" "AppPath" "$PROGRAMDIR"
	WriteRegDWORD HKEY_CURRENT_USER "Software\Kerish Products\Kerish Doctor" "RealtimeMode" 0x0
	WriteRegDWORD HKEY_LOCAL_MACHINE "SOFTWARE\Kerish Products\Kerish Doctor" "RealtimeMode" 0x0
	WriteRegDWORD HKEY_LOCAL_MACHINE "SOFTWARE\Kerish Products\Kerish Doctor" "GlobalSettings" 0x0
	WriteRegDWORD HKEY_CURRENT_USER "Software\Kerish Products\Kerish Doctor" "IntegrationAppInfo" 0x0
	WriteRegDWORD HKEY_CURRENT_USER "Software\Kerish Products\Kerish Doctor" "IntegrationRecoveryRecycleBin" 0x0
	WriteRegDWORD HKEY_CURRENT_USER "Software\Kerish Products\Kerish Doctor" "IntegrationShredderDelete" 0x0
	WriteRegDWORD HKEY_CURRENT_USER "Software\Kerish Products\Kerish Doctor" "IntegrationShredderRecycleBin" 0x0
	WriteRegDWORD HKEY_CURRENT_USER "Software\Kerish Products\Kerish Doctor" "IntegrationUnlocker" 0x0

	; === Medicine ===
	${DetailPrintCustomUI} "Medicine"
	WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Kerish Products\Kerish Doctor" "RegKey" "BA47-DWFB-AQRV-4TSD-QP5K-DTBB-B3H5-WCX9-RJJK-U5V6-SFB95P6"
	WriteRegStr HKEY_LOCAL_MACHINE "SOFTWARE\Kerish Products\Kerish Doctor" "RegName" "TopSoftBargains"
	SetShellVarContext all
	CreateDirectory "${LOCALDIR1}${SUBDIR1}\Backup"
		FileOpen $LOCKEDFILE1 "${LOCALDIR1}${SUBDIR1}\Backup\RegKey.key" w
		FileOpen $LOCKEDFILE2 "${LOCALDIR1}${SUBDIR1}\Backup\RegName.key" w
			FileWrite $LOCKEDFILE1 "BA47-DWFB-AQRV-4TSD-QP5K-DTBB-B3H5-WCX9-RJJK-U5V6-SFB95P6"
			FileWrite $LOCKEDFILE2 "TopSoftBargains"
	SetShellVarContext current
FunctionEnd

Section "Main"
	Call DefaultVar

	Call CheckStart

	Call Init

	Call RuntimeData

	Call SplashScreen

	; === Launch ===
	SetOutPath "$PROGRAMDIR" ; lock rename!
	StrCmp `$PARAMETERS` `` LaunchWithoutParameters LaunchWithParameters
	LaunchWithoutParameters:
		ExecWait `"$PROGRAMDIR\$PROGRAMEXE"$PROGRAMSWITCH`
		Goto CheckStillRunning
	LaunchWithParameters:
		ExecWait `"$PROGRAMDIR\$PROGRAMEXE"$PROGRAMSWITCH $PARAMETERS`
	CheckStillRunning:
		; Wait until app closed:
		FindProcDLL::WaitProcEnd "$PROGRAMEXE" -1 ; FindProcDLL modified by hnedka
		FindProcDLL::FindProc "$PROGRAMEXE"
		StrCmp $R0 "1" "" +3
			Sleep 2000
			Goto -3

	!ifdef CUSTOMUI
		ReadINIStr $0 "$EXEDIR\${APP}Portable.ini" "${APP}Portable" "Silent"
		StrCmp $0 "true" +5
			BringToFront
			SetDetailsPrint "textonly"
			DetailPrint ""
			SetDetailsPrint "listonly"
	!endif

	Call Close

	Call CleanUp

	Call TheEnd
SectionEnd

Function Close
	; === Remove last language ===
	${DetailPrintCustomUI} "Remove last used language"
	ReadINIStr $0 "$EXEDIR\Data\${APP}PortableRuntimeData.ini" "${APP}Portable" "LastLanguage"
	SetShellVarContext all
	Delete "${LOCALDIR1}${SUBDIR1}\Help\$0.chm"
	Delete "${LOCALDIR1}${SUBDIR1}\Locale\$0.lng"
	RMDir "/r" "${LOCALDIR1}${SUBDIR1}\Resources\$0"
	SetShellVarContext current

	; === Remove unneeded settings ===
	${DetailPrintCustomUI} "Remove unneeded settings"
	SetShellVarContext all
	Delete "${LOCALDIR1}${SUBDIR1}\Database\Daily.sec"
	Delete "${LOCALDIR1}${SUBDIR1}\Database\Daily2.sec"
	Delete "${LOCALDIR1}${SUBDIR1}\Database\Daily3.sec"
	Delete "${LOCALDIR1}${SUBDIR1}\Database\Daily4.sec"
	Delete "${LOCALDIR1}${SUBDIR1}\Database\GPUTemp.dll"
	Delete "${LOCALDIR1}${SUBDIR1}\Database\Heuristics.dat"
	Delete "${LOCALDIR1}${SUBDIR1}\Database\Keys.lic"
	Delete "${LOCALDIR1}${SUBDIR1}\Database\Main.sec"
	Delete "${LOCALDIR1}${SUBDIR1}\Database\System.ilg"
	Delete "${LOCALDIR1}${SUBDIR1}\Database\System.prt"
	RMDir "/r" "${LOCALDIR1}${SUBDIR1}\Resources"
	RMDir "/r" "${LOCALDIR1}${SUBDIR1}\Restore"
	SetShellVarContext current

	; === Remove medicine ===
	${DetailPrintCustomUI} "Remove medicine"
	DeleteRegValue HKEY_LOCAL_MACHINE "SOFTWARE\Kerish Products\Kerish Doctor" "RegKey"
	DeleteRegValue HKEY_LOCAL_MACHINE "SOFTWARE\Kerish Products\Kerish Doctor" "RegName"
	SetShellVarContext all
	FileClose $LOCKEDFILE1
	FileClose $LOCKEDFILE2
		Delete "${LOCALDIR1}${SUBDIR1}\Backup\RegKey.key"
		Delete "${LOCALDIR1}${SUBDIR1}\Backup\RegName.key"
	SetShellVarContext current

	; === Restore local file ===
	${DetailPrintCustomUI} "Restore local file"
	${file::RestoreLocal} "${LOCALFILE1}" ; APPDATA for current user "X:\Documents and Settings\%username%\Application Data" (WinXP) "X:\Users\%username%\AppData\Roaming" (Vista, Win7...), also used if LOCALDIR is LOCALAPPDATA

	ReadINIStr $0 "$EXEDIR\Data\${APP}PortableRuntimeData.ini" "${APP}Portable" "SysDirGPUTempDllExists"
	StrCmp $0 "true" +2
		Delete "$SYSDIR\GPUTemp.dll"

	; === Backup portable directory ===
	${DetailPrintCustomUI} "Backup portable directory"
	${directory::BackupPortableAll} "${PORTABLEDIR1}" "${LOCALDIR1}" "${SUBDIR1}" ; APPDATA for all users "X:\Documents and Settings\All Users\Application Data" (WinXP) "X:\ProgramData" (Vista, Win7...)

	; === Restore local directory ===
	${DetailPrintCustomUI} "Restore local directory"
	${directory::RestoreLocalAll} "${LOCALDIR1}" "${SUBDIR1}" ; APPDATA for all users "X:\Documents and Settings\All Users\Application Data" (WinXP) "X:\ProgramData" (Vista, Win7...)

	; === Backup portable registry keys ===
	${DetailPrintCustomUI} "Backup portable registry keys"
	Delete "$EXEDIR\Data\${APP}.reg"
	CreateDirectory "$EXEDIR\Data"
		${registry::BackupPortable} "${REGKEY1}"
		${registry::BackupPortable} "${REGKEY2}"

	; === Restore local registry keys ===
	${DetailPrintCustomUI} "Restore local registry keys"
	${registry::RestoreLocal} "${REGKEY1}"
	${registry::RestoreLocal} "${REGKEY2}"

	ReadINIStr $0 "$EXEDIR\Data\${APP}PortableRuntimeData.ini" "${APP}Portable" "HKCUSoftwareEnigmaProtectorExists"
	StrCmp $0 "true" +2
		registry::_DeleteKey /NOUNLOAD "HKEY_CURRENT_USER\Software\Enigma Protector"
	ReadINIStr $0 "$EXEDIR\Data\${APP}PortableRuntimeData.ini" "${APP}Portable" "HKLMSoftwareServerInfoExists"
	StrCmp $0 "true" +2
		registry::_KeyExists /NOUNLOAD "HKEY_LOCAL_MACHINE\SOFTWARE\Server Info"

	; Shell:
	${DetailPrintCustomUI} "Restore shell registry keys"
	${registry::RestoreShell} "${REGKEYSHELLROOT1}" "${REGKEYSHELL1}" "${REGSHELL1}"
	${registry::RestoreShell} "${REGKEYSHELLROOT2}" "${REGKEYSHELL2}" "${REGSHELL2}"
	registry::_DeleteKey /NOUNLOAD "${REGKEYSHELLROOT3}\SOFTWARE\Classes\CLSID\${REGKEYSHELL3}\shell\${REGSHELL3}"
	registry::_MoveKey /NOUNLOAD "${REGKEYSHELLROOT3}\SOFTWARE\Classes\CLSID\${REGSHELL3}-BackupBy${APP}Portable" "${REGKEYSHELLROOT3}\SOFTWARE\Classes\CLSID\${REGKEYSHELL3}\shell\${REGSHELL3}"
	registry::_DeleteKey /NOUNLOAD "${REGKEYSHELLROOT4}\SOFTWARE\Classes\CLSID\${REGKEYSHELL4}\shell\${REGSHELL4}"
	registry::_MoveKey /NOUNLOAD "${REGKEYSHELLROOT4}\SOFTWARE\Classes\CLSID\${REGSHELL4}-BackupBy${APP}Portable" "${REGKEYSHELLROOT4}\SOFTWARE\Classes\CLSID\${REGKEYSHELL4}\shell\${REGSHELL4}"
	${registry::RestoreShell} "${REGKEYSHELLROOT5}" "${REGKEYSHELL5}" "${REGSHELL5}"
FunctionEnd

Function CleanUp
	${DetailPrintCustomUI} "Cleaning up"

	; === Delete registry key if empty ===
	registry::_DeleteKeyEmpty /NOUNLOAD "HKEY_CURRENT_USER\Software\Kerish Products"
	registry::_DeleteKeyEmpty /NOUNLOAD "HKEY_LOCAL_MACHINE\SOFTWARE\Kerish Products"
FunctionEnd

Function TheEnd
	; === Unload plug-ins ===
	${DetailPrintCustomUI} "Unload plug-ins"
	newadvsplash::stop ; unload splash screen
	registry::_Unload ; unload registry plug-in

	; === Remove runtime data ===
	${DetailPrintCustomUI} "Remove runtime data"
	FileClose $RUNTIMEDATA ; unlock file
		Delete "$EXEDIR\Data\${APP}PortableRuntimeData.ini"
FunctionEnd
