; --------------------------------------------
; NSIS Installer for ${software}
; --------------------------------------------

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

!define software MineXpert3
!define version "11.6.1-build-20260608-94e346d1"

!define arch "vcrt64"
!define platform "win10"
!define buildsystem "MXE"

!define buildtype "Release"

!define homeDir "/home/rusconi"

!define sourceDir "${homeDir}/devel/minexpert/development"

!define buildDir "${homeDir}/devel/minexpert/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/minexpert"

!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 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
