; ---------------------------------------
; 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 "vcrt64"
!define platform "win10"
!define buildsystem "MXE"

!define buildtype "@CMAKE_BUILD_TYPE@"

!define homeDir "/home/rusconi"

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

!define buildDir "${homeDir}/devel/@LOWCASE_PROJECT_NAME_NO_VER@/build-area/mxe"

;;;;; When a documentation set (PDF, HTML) has been successfully built, that set
;;;;; is copied to this directory for winpackage target consumption.
!define localDocStore "${homeDir}/devel/msxpertsuite-store/docs/@LOWCASE_PROJECT_NAME_NO_VER@"

!define dllSetForPackagesDir "${homeDir}/devel/mxe/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 data folder
  SetOutPath "$INSTDIR\data"
  File /r "${sourceDir}\data\*"

  ; 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\data\*.*"
  RMDir /r "$INSTDIR\data"

  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
