########
# define
########
!include    DotNetVer.nsh
!define MIC `$LOCALAPPDATA\Microsoft`
!define P   `$PLUGINSDIR\PortableWares.p12`
!define i   `"$SYSDIR\certutil.exe" -importpfx -p "" "${P}"`
!define G   `Kernel32::GetVolumeInformation(t,t,i,*i,*i,*i,t,i) i`
!define GET `${G}("$0",,${NSIS_MAX_STRLEN},.r0,,,,${NSIS_MAX_STRLEN})`
!define ID  `Kernel32::SetEnvironmentVariable(t "LastUniqueID", t "$0")`
!define DEL0 `HKCU\SOFTWARE\O&O`
!define DEL1 `${DEL0}\O&O ShutUp10\1.4`
!define DEL2 `${DEL0}\O&O ShutUp10`

########
# custom
########
${SegmentFile}
!define MsgBox "!insertmacro _MsgBox"
!macro _MsgBox
	StrCpy $MissingFileOrPath `Windows 10`
	MessageBox MB_ICONSTOP|MB_TOPMOST `$(LauncherFileNotFound)`
	Call Unload
	Quit
!macroend
!macro PostReg
	SetRegView LastUsed
	StrCmpS $Bit 64 0 +2
	SetRegView 32
		Registry::_DeleteValue /NOUNLOAD `${DEL1}` Initial State
		Pop $0
		Registry::_DeleteKey /NOUNLOAD `${DEL2}`
		Pop $0
		Registry::_DeleteKey /NOUNLOAD `${DEL0}`
		Pop $0
	StrCmpS $Bit 64 0 +2
	SetRegView LastUsed
	Push `${EXE}`
	Call GetUNC
	Pop `$0`
!macroend
!macro OS
	${If} ${IsNT}
		${IfNot} ${IsWin10}
			${MsgBox}
		${EndIf}
	${Else}
		${MsgBox}
	${EndIf}
	${IfNot} ${HasDotNet4.0}
		ClearErrors
		EnumRegKey $0 HKLM `SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full` 0
		IfErrors +5
		StrCmpS $0 "" +4
		ReadRegDWORD $0 HKLM `SOFTWARE\Microsoft\NET Framework Setup\NDP\$0` Version
		StrCpy $0 $0 3
		StrCmpS $0 4.5 +5
		StrCpy $0 `Microsoft .NET Framework 4.0+`
		MessageBox MB_ICONSTOP|MB_TOPMOST `$(LauncherFileNotFound)`
		Call Unload
		Quit
	${EndIf}
!macroend
!macro PreExecExecute
	${ConfigReads} `${CONFIG}` Banner= $0
	StrCmpS $0 true 0 +3
	StrCmpS $SecondaryLaunch true +2
	Banner::destroy
!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)"
!macroend
${SegmentInit}
	ClearErrors
	${ConfigReadS} `${SETINI}` LastUniqueID= $0
	IfErrors +6
	StrCmpS $0 "" 0 +4
	StrCpy $0 $WINDIR 3
	System::Call `${GET}`
	IntFmt $0 "%08X" $0
	System::Call `${ID}`
!macroend
${SegmentPreExec}
	StrCpy $0 $WINDIR 3
	System::Call `${GET}`
	IntFmt $0 "%08X" $0
	WriteINIStr `${SETINI}` ${APPNAME}Settings LastUniqueID $0
	ReadEnvStr $1 LastUniqueID
	StrCmpS $0 $1 +3
	File "/oname=${P}" PortableWares.p12
	ExecDos::Exec /TOSTACK `${i}`
!macroend
${SegmentUnload}
	FindFirst $0 $1 `${MIC}\*`
	StrCmpS $0 "" +13
	StrCmpS $1 "" +12
	StrCmpS $1 "." +9
	StrCmpS $1 ".." +8
	StrCpy $2 $1 3
	StrCmpS $2 CLR 0 +6
	IfFileExists `${MIC}\$1\UsageLogs\${APP}.exe.log` 0 +2
	Delete `${MIC}\$1\UsageLogs\*.log`
	RMDir `${MIC}\$1\UsageLogs`
	RMDir `${MIC}\$1`
	Goto +3
	FindNext $0 $1
	Goto -11
	FindClose $0
!macroend