; ---------------------------------------
; NSIS Installer for @CMAKE_PROJECT_NAME@
; ---------------------------------------

!include "MUI2.nsh"
!include "FileFunc.nsh"
!include "LogicLib.nsh"

!define software @CMAKE_PROJECT_NAME@
!define version "@PROJECT_VERSION@-@GIT_RECORD_IDENTIFIER@"

!define arch "ucrt64"
!define platform "win10"
!define buildsystem "MSYS"

!define buildtype "@CMAKE_BUILD_TYPE@"

!define homeDir "C:\msys64\home\polipo"

!define sourceDir "${homeDir}\devel\@LOWCASE_PROJECT_NAME_NO_VER@\development"

!define buildDir "${homeDir}\devel\@LOWCASE_PROJECT_NAME_NO_VER@\build-area\win10"

;;;;; When a documentation set (PDF, HTML) has been successfully built, that set
;;;;; is copied to this directory for winpackage target consumption.
;;;;; On the MS Windows Virtualbox image, W:
;;;;; is the host system's /home/rusconi/devel/msxpertsuite-store
;;;;; read/write directory.
!define localDocStore "W:\docs\@LOWCASE_PROJECT_NAME_NO_VER@"

!define dllSetForPackagesDir "${homeDir}\devel\dll-set-for-packages"

; EXE icon MUST be defined here, before pages:
!define MUI_ICON "${sourceDir}/images/icons/${software}.ico"
; Optional uninstaller icon:
!define MUI_UNICON "${sourceDir}/images/icons/${software}.ico"


Name "${software}"
OutFile "${software}-${platform}-${buildsystem}-${arch}-v${version}-${buildtype}-setup.exe"
InstallDir "$PROGRAMFILES64\${software}"
InstallDirRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${software}" "InstallLocation"
ShowInstDetails show

Icon "${sourceDir}\images\icons\${software}.ico"

; ---------------------------------------------------
; Installer pages
; ---------------------------------------------------
!define MUI_WELCOMEFINISHPAGE_BITMAP "${sourceDir}\images\splashscreen-installer.bmp"
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE "${sourceDir}\LICENSE"
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES

!define MUI_FINISHPAGE_RUN "$INSTDIR\${software}.exe"
!define MUI_FINISHPAGE_RUN_TEXT "Run ${software} now"

!define MUI_FINISHPAGE_LINK "Visit MsXpertSuite"
!define MUI_FINISHPAGE_LINK_LOCATION "http://www.msxpertsuite.org"

!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
UninstPage uninstConfirm
UninstPage instfiles

!insertmacro MUI_LANGUAGE "English"

; ------------------------------
; Installation Section
; ------------------------------
Section "Install Files"

  SetOutPath "$INSTDIR"

  ; Copy DLLs and subfolders
  File /r "${dllSetForPackagesDir}\*.dll"

  File /r "${dllSetForPackagesDir}\iconengines\*.dll"
  File /r "${dllSetForPackagesDir}\imageformats\*.dll"
  File /r "${dllSetForPackagesDir}\platforms\*.dll"
  File /r "${dllSetForPackagesDir}\styles\*.dll"

  ; Copy EXE
  File "${buildDir}\src\${software}.exe"

  ; Copy icon (optional, for Start Menu or shortcuts)
  File "${sourceDir}\images\icons\${software}.ico"

  ; Copy documentation
  SetOutPath "$INSTDIR\doc"
  File "${sourceDir}\doc\history.html"
  File "${localDocStore}\${software}-doc.pdf"

  SetOutPath "$INSTDIR\doc\html"
  File /r "${localDocStore}\html\user-manual\*"

  ; Write the uninstaller
  WriteUninstaller "$INSTDIR\Uninstall.exe"

  ; Optional: create Start Menu shortcuts
  CreateDirectory "$SMPROGRAMS\${software}"
  CreateShortcut "$SMPROGRAMS\${software}\${software}.lnk" "$INSTDIR\${software}.exe"
  CreateShortcut "$SMPROGRAMS\${software}\Uninstall ${software}.lnk" "$INSTDIR\Uninstall.exe"

SectionEnd

; ------------------------------
; Shortcuts
; ------------------------------
Section "Shortcuts"
  CreateDirectory "$SMPROGRAMS\${software}"

  ; Main shortcut
  CreateShortCut "$SMPROGRAMS\${software}\${software}.lnk" "$INSTDIR\${software}.exe" "" "$INSTDIR\${software}.ico" 0

  ; Uninstall shortcut
  CreateShortCut "$SMPROGRAMS\${software}\Uninstall ${software}.lnk" "$INSTDIR\Uninstall.exe"

SectionEnd

; ------------------------------
; Uninstaller
; ------------------------------
Section "Uninstall"

  Delete "$INSTDIR\${software}.exe"
  Delete "$INSTDIR\${software}.ico"
  Delete "$INSTDIR\Uninstall.exe"

  Delete "$INSTDIR\*.dll"

  Delete "$INSTDIR\iconengines\*.dll"
  RMDir "$INSTDIR\iconengines"
  Delete "$INSTDIR\imageformats\*.dll"
  RMDir "$INSTDIR\imageformats"
  Delete "$INSTDIR\platforms\*.dll"
  RMDir "$INSTDIR\platforms"
  Delete "$INSTDIR\styles\*.dll"
  RMDir "$INSTDIR\styles"

  Delete "$INSTDIR\doc\*.*"
  RMDir /r "$INSTDIR\doc"

  Delete "$INSTDIR\images\icons\${software}.ico"

  Delete "$SMPROGRAMS\${software}\${software}.lnk"
  Delete "$SMPROGRAMS\${software}\Uninstall ${software}.lnk"
  RMDir "$SMPROGRAMS\${software}"

  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${software}"

  ; Remove the install directory if empty
  RMDir "$INSTDIR"

SectionEnd

; ------------------------------
; Run application after install
; ------------------------------
; Section -Post
;   Exec "$INSTDIR/${software}.exe"
; SectionEnd
