; The Node.js binding is x64 only: Node dropped its 32-bit Windows builds in v23,
; so there is no ia32 addon to ship. create-installer.py only defines NSISNODEJS
; when the addon was actually staged; the architecture check is the second half of
; that guarantee, so an x86 installer can never offer a component with no payload.
!ifdef NSISNODEJS
	!ifndef NSIS_X86
		!define NODEJS_SECTION
	!endif
!endif

Section "USB-CEC driver" SecDriver
	SectionIn RO
	SectionIn 1 2 3

	; Copy the driver installer
	SetOutPath "$INSTDIR\driver"
	File "..\build\p8-usbcec-driver-installer.exe"

	; Install driver
	ExecWait '"$INSTDIR\driver\p8-usbcec-driver-installer.exe" /S'
SectionEnd

Section "libCEC" SecLibCec
	SetShellVarContext all
	SectionIn 1 2
	SectionIn RO

	; Remove the old-style managed wrappers and their apps from previous installs,
	; so upgrading never leaves a mix of old (C++/CLI, .NET Framework) and new
	; (unified pure C# net8.0) assemblies side by side.
	RMDir /r "$INSTDIR\netfx"
	Delete "$INSTDIR\net8.0\LibCecSharpCore.dll"
	Delete "$INSTDIR\net8.0\LibCecSharpCore.deps.json"
	Delete "$INSTDIR\net8.0\LibCecSharpCore.runtimeconfig.json"
	Delete "$INSTDIR\net8.0\LibCecSharpCore.xml"
	Delete "$INSTDIR\net8.0\LibCecSharpCore.pdb"
	Delete "$INSTDIR\net8.0\Ijwhost.dll"
	Delete "$INSTDIR\net8.0\CecSharpCoreTester.exe"
	Delete "$INSTDIR\net8.0\CecSharpCoreTester.dll"
	Delete "$INSTDIR\net8.0\CecSharpCoreTester.deps.json"
	Delete "$INSTDIR\net8.0\CecSharpCoreTester.runtimeconfig.json"
	Delete "$INSTDIR\net8.0\CecSharpCoreTester.pdb"

	; Copy binaries and support files
	SetOutPath "$INSTDIR"
	File "..\ChangeLog"
	File "..\README.md"
	File "..\AUTHORS"
	File "..\LICENSE.md"
	File "..\docs\README.developers.md"
	File "..\docs\README.windows.md"
	File "..\support\windows\tv_on.cmd"
	File "..\support\windows\tv_off.cmd"
	File "${BINARY_SOURCE_DIR}\cec.dll"
	File "/oname=p8-logo.ico" "favicon.ico"

	; Copy the headers
	SetOutPath "$INSTDIR\include"
	File /r /x *.so "${BINARY_SOURCE_DIR}\include\libcec\*.h"

	; Store installation folder
	WriteRegStr HKLM "Software\Pulse-Eight\${BASE_REGKEY}" "" $INSTDIR

	; Package uninstaller
	!ifndef INNER
		SetOutPath $INSTDIR
		File $%TEMP%\uninstall_libcec.exe
	!endif

	!insertmacro MUI_STARTMENU_WRITE_BEGIN Application
	SetOutPath $INSTDIR

	CreateDirectory "$SMPROGRAMS\$StartMenuFolder"
	CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Uninstall ${BASE_REGKEY}.lnk" "$INSTDIR\uninstall_libcec.exe" \
		"" "$INSTDIR\uninstall_libcec.exe" 0 SW_SHOWNORMAL \
		"" "Uninstall ${BASE_REGKEY}."

	WriteINIStr "$SMPROGRAMS\$StartMenuFolder\Visit Pulse-Eight.url" "InternetShortcut" "URL" "https://www.pulse-eight.com/"
	!insertmacro MUI_STARTMENU_WRITE_END

	;add entry to add/remove programs
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${BASE_REGKEY}" \
		"DisplayName" "${BASE_REGKEY}"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${BASE_REGKEY}" \
		"UninstallString" "$INSTDIR\uninstall.exe"
	WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${BASE_REGKEY}" \
		"NoModify" 1
	WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${BASE_REGKEY}" \
		"NoRepair" 1
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${BASE_REGKEY}" \
		"InstallLocation" "$INSTDIR"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${BASE_REGKEY}" \
		"DisplayIcon" "$INSTDIR\cec-client.exe,0"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${BASE_REGKEY}" \
		"Publisher" "Pulse-Eight Limited"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${BASE_REGKEY}" \
		"HelpLink" "https://www.pulse-eight.com/"
	WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${BASE_REGKEY}" \
		"URLInfoAbout" "https://www.pulse-eight.com"
SectionEnd

!ifndef NSISINCLUDEPDB
Section "libCEC for Python" SecPythonCec
	SectionIn 1 2

	; Copy binaries
	SetOutPath "$INSTDIR\python\cec"
	
	File "${BINARY_SOURCE_DIR}\cec.dll"
	File "${BINARY_SOURCE_DIR}\python\cec\_pycec.pyd"
	File "${BINARY_SOURCE_DIR}\python\cec\cec.py"
	!ifdef NSIS_X86
		File  "${BINARY_SOURCE_DIR}\python\cec\__init__.py"
	!endif
	SetOutPath "$INSTDIR\python"
	File "${BINARY_SOURCE_DIR}\python\pyCecClient.py"
SectionEnd
!endif

Section "libCEC for .Net" SecDotNetCore
	SectionIn 1 2

	; Copy binaries (pure C# LibCecSharp; no Ijwhost.dll / runtimeconfig for the library)
	SetOutPath "$INSTDIR\net8.0"
	File "${BINARY_SOURCE_DIR}\cec.dll"
	File "${BINARY_SOURCE_DIR}\net8.0\LibCecSharp.deps.json"
	File "${BINARY_SOURCE_DIR}\net8.0\LibCecSharp.dll"
	File "${BINARY_SOURCE_DIR}\net8.0\LibCecSharp.xml"
	File /nonfatal "${BINARY_SOURCE_DIR}\net8.0\CecSharpTester.exe"
	File /nonfatal "${BINARY_SOURCE_DIR}\net8.0\CecSharpTester.deps.json"
	File /nonfatal "${BINARY_SOURCE_DIR}\net8.0\CecSharpTester.dll"
	File /nonfatal "${BINARY_SOURCE_DIR}\net8.0\CecSharpTester.runtimeconfig.json"
SectionEnd

!ifdef NSISDOTNETAPPS
!include "nsis\cec-tray.nsh"
!endif

!ifdef NODEJS_SECTION
!include "nsis\nodejs.nsh"
!endif

Section "libCEC client (cec-client)" SecCecClient
	SectionIn 1

	; Copy binaries
	SetOutPath "$INSTDIR"
	File "${BINARY_SOURCE_DIR}\cec-client.exe"
	File "${BINARY_SOURCE_DIR}\cecc-client.exe"

	; Start menu item
	!insertmacro MUI_STARTMENU_WRITE_BEGIN Application
	SetShellVarContext all
	CreateDirectory "$SMPROGRAMS\$StartMenuFolder"
    CreateShortCut "$SMPROGRAMS\$StartMenuFolder\CEC Test client.lnk" "$INSTDIR\cec-client.exe" \
		"" "$INSTDIR\p8-logo.ico" 0 SW_SHOWNORMAL \
		"" "Start the CEC Test client."
	!insertmacro MUI_STARTMENU_WRITE_END
SectionEnd

Section "Adapter Firmware" SecFwUpgrade
	SectionIn 1

	; Copy the driver installer
	SetOutPath "$INSTDIR\driver"
	File "..\build\p8-usbcec-bootloader-driver-installer.exe"
	; install driver
	ExecWait '"$INSTDIR\driver\p8-usbcec-bootloader-driver-installer.exe" /S'

	SetOutPath "$INSTDIR"
	File "..\build\libusb0.dll"
	NSISdl::download https://p8.opdenkamp.eu/cec/cec-firmware-latest.exe cec-firmware-latest.exe

	SetShellVarContext all
	CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Firmware Upgrade.lnk" "$INSTDIR\cec-firmware-latest.exe" \
		"" "$INSTDIR\p8-logo.ico" 0 SW_SHOWNORMAL \
		"" "Upgrade the firmware of the CEC adapter to the latest version."
SectionEnd

!ifdef NSISINCLUDEPDB
!include "nsis\libcec-pdb.nsh"
!endif

; Required options
Function .onSelChange
	!ifdef NSISDOTNETAPPS
	${If} ${SectionIsSelected} ${SecTray}
		!define /math MYSECTIONFLAGS ${SF_SELECTED} | ${SF_RO}
		!insertmacro SetSectionFlag ${SecDotNetCore} ${MYSECTIONFLAGS}
		!undef MYSECTIONFLAGS
	${Else}
		!insertmacro ClearSectionFlag ${SecDotNetCore} ${SF_RO}
	${EndIf}
	!endif

	${If} ${SectionIsSelected} ${SecCecClient}
	${OrIf} ${SectionIsSelected} ${SecDotNetCore}
	!ifdef NODEJS_SECTION
	${OrIf} ${SectionIsSelected} ${SecNodeJs}
	!endif
	!ifndef NSISINCLUDEPDB
	${OrIf} ${SectionIsSelected} ${SecPythonCec}
	!endif
		!define /math MYSECTIONFLAGS ${SF_SELECTED} | ${SF_RO}
		!insertmacro SetSectionFlag ${SecLibCec} ${MYSECTIONFLAGS} 
		!undef MYSECTIONFLAGS
	${Else}
		!insertmacro ClearSectionFlag ${SecLibCec} ${SF_RO}
	${EndIf}

FunctionEnd
