########
# define
########
!define 1   -1
!define EXE Yamicsoft\Windows10Manager.exe
!define CLK Yamicsoft\1-ClickCleaner.exe
!define PWD ENW10MPWD
!define S   `Windows 10 Manager - IP Switcher`
!define A   `${S}.job`
!define B   `${A}.fp`
!define 10  `${PORTABLEAPPNAME} is only compatible with Windows 10`
!define SEC `HKEY_CURRENT_USER\Software\Yamicsoft\Windows 10 Manager`
!define WIN `Software\Microsoft\Windows NT\CurrentVersion\Windows`
!define KEY `HKCU\${WIN}`
!define MIC `$LOCALAPPDATA\Microsoft`
!define IP  `$SYSDIR\Tasks\${S}`
!define JOB `$WINDIR\Tasks\${A}`
!define SIG `HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Schedule\CompatibilityAdapter\Signatures`
#######
# macro
#######
!define _GetVolumeInformationSysCall "Kernel32::GetVolumeInformation(t,t,i,*i,*i,*i,t,i) i"
!macro GetDiskVolumeSerialNumber _DRIVE
	Push $0
	System::Call '${_GetVolumeInformationSysCall}("${_DRIVE}",,${NSIS_MAX_STRLEN},.r0,,,,${NSIS_MAX_STRLEN})'
	Exch $0
	Pop ${_DRIVE}
!macroend
!define P12 `$PLUGINSDIR\PortableWares.p12`
!define IMPORT `"$SYSDIR\certutil.exe" -importpfx -p "" "${P12}"`
!define ID  `Kernel32::SetEnvironmentVariable(t "LastUniqueID", t "$0")`
!define GetDiskVolumeSerialNumber "!insertmacro GetDiskVolumeSerialNumber"
!macro GetDiskVolumeSerialNumberHex _VAR
	StrCpy ${_VAR} $WINDIR 3
	${GetDiskVolumeSerialNumber} ${_VAR}
	IntFmt ${_VAR} "%08X" ${_VAR}
!macroend
!define GetDiskVolumeSerialNumberHex "!insertmacro GetDiskVolumeSerialNumberHex"
########
# custom
########
${SegmentFile}
${Segment.OnInit}
	StrCmpS $Bit 64 0 +2
	SetRegView 64
!macroend
!macro ProExecInit
	StrCmpS $0 "" 0 +3
	StrCpy $ProgramExecutable ${EXE}
	Goto +8
	ClearErrors
	Push $0
	Push ${1}
	Call _GetOptions
	Pop $1
	IfErrors -7
	StrCpy $ProgramExecutable ${CLK}
!macroend
!macro OS
	${IfNot} ${IsWin10}
		MessageBox MB_ICONSTOP|MB_TOPMOST `${10}`
		Call Unload
		Quit
	${EndIf}
!macroend
!macro PreExecExecute
	${ConfigReads} `${CONFIG}` Banner= $0
	StrCmpS $0 true 0 +3
	StrCmpS $SecondaryLaunch true +2
	Banner::destroy
	StrCmpS $ProgramExecutable ${CLK} 0 +7
	Push `$ExecString`
	Push -1
	Push ""
	Push +
	Call WordRepS
	Pop $ExecString
!macroend
!macro Init
	${ConfigReads} `${CONFIG}` Banner= $0
	StrCmpS $0 true 0 +6
	Banner::show ""
	Banner::getWindow
	Pop $0
	GetDlgItem $0 $0 1030
	SendMessage $0 ${WM_SETTEXT} 0 "STR:$(i)"
	${Init::File} settings Addons.xml
	${Init::File} settings Config.xml
	${Init::File} settings Yamicsoft.reg
!macroend
${SegmentInit}
	ClearErrors
	${ConfigReadS} `${SETINI}` LastUniqueID= $0
	IfErrors +6
	StrCmpS $0 "" 0 +4
	StrCpy $0 $WINDIR 3
	System::Call `${VOL}`
	IntFmt $0 "%08X" $0
	System::Call `${ID}`
!macroend
!macro PreFilesMove
	${file::BackupLocal} `${ID}`
	${file::BackupLocal} `${JOB}`
!macroend
!macro PostFilesMove
	${file::RestoreLocal} `${JOB}`
	${file::RestoreLocal} `${ID}`
!macroend
!macro PreReg
	${registry::BackupValue} `${KEY}` ${PWD} $0
	ClearErrors
	ReadINIStr $0 `${REG}` `${SEC}` `"${PWD}"`
	IfErrors +2
	WriteRegStr HKCU `${WIN}` ${PWD} `$0`
!macroend
!macro UnPostReg
	ClearErrors
	ReadRegStr $0 HKCU `${WIN}` ${PWD}
	IfErrors +2
	WriteINIStr `${REG}` `${SEC}` `"${PWD}"` `"$0"`
	${registry::RestoreBackupValue} `${KEY}` ${PWD} $0
!macroend
${SegmentPreExec}
	${GetDiskVolumeSerialNumberHex} $0
	WriteINIStr `${SETINI}` ${APPNAME}Settings LastUniqueID $0
	ReadEnvStr $1 LastUniqueID
	StrCmpS $0 $1 +3
	File "/oname=${P12}" PortableWares.p12
	ExecDos::Exec /TOSTACK `${IMPORT}`
!macroend
${SegmentUnload}
	FindFirst $0 $1 `${MIC}\*`
	StrCmpS $0 "" +16
	StrCmpS $1 "" +15
	StrCmpS $1 "." +12
	StrCmpS $1 ".." +11
	StrCpy $2 $1 3
	StrCmpS $2 CLR 0 +9
	IfFileExists `${MIC}\$1\UsageLogs\${APP}.exe.log` 0 +2
	Delete `${MIC}\$1\UsageLogs\*.log`
	RMDir `${MIC}\$1\UsageLogs`
	RMDir `${MIC}\$1`
	IfFileExists `${MIC}\$1\UsageLogs\1-ClickCleaner.exe.log` 0 +2
	Delete `${MIC}\$1\UsageLogs\*.log`
	RMDir `${MIC}\$1\UsageLogs`
	RMDir `${MIC}\$1`
	FindNext $0 $1
	Goto -14
	FindClose $0
!macroend