setCompressor lzma
; fwbuilder.nsi
;
;
!verbose 1
;--------------------------------
;Variables

Var MUI_TEMP
Var STARTMENU_FOLDER

; GENERATION is used to build the path in the registry, it should be coordinated
; with the path defined in FWBSettings class
;
!define GENERATION "@PROJECT_GENERATION@"
!define VERSION "@PROJECT_VERSION@"
!define APPNAME "FirewallBuilder"

!include WinVer.nsh
Function .onInit
  ${IfNot} ${AtLeastWin7}
    MessageBox MB_OK  "Firewall Builder require at least Windows 7."
    Abort
  ${EndIf}
FunctionEnd

;****************************************************************************
;Include Modern UI

  !include "MUI2.nsh"

;****************************************************************************


  Name "Firewall Builder ${VERSION}"
  Caption "Firewall Builder installation"
  OutFile "fwbuilder-${VERSION}.exe"

;  Default installation folder
  InstallDir "$PROGRAMFILES\FWBuilder${GENERATION}"

;****************************************************************************
; We need to keep installation data and program settings in
; registry folders with different names. QSettings always looks into
;****************************************************************************
;
;  Get installation folder from registry if available

  InstallDirRegKey HKLM Software\firewallbuilder.org\${APPNAME}\${GENERATION} "Install_Dir"

;****************************************************************************
;Interface Settings

  !define MUI_ABORTWARNING

  ;Start Menu Folder Page Configuration
  !define MUI_STARTMENUPAGE_REGISTRY_ROOT "HKLM"
  !define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\firewallbuilder.org\${APPNAME}\${GENERATION}"
  !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "Start Menu Folder"

  LicenseText "GNU GENERAL PUBLIC LICENSE"
  LicenseData "doc\COPYING"

  !define MUI_HEADERIMAGE
  !define MUI_HEADERIMAGE_BITMAP       fwbuilder-160x60.bmp
  !define MUI_WELCOMEFINISHPAGE_BITMAP fwbuilder-164x314.bmp

;****************************************************************************
; The following macros add PageEx statements

  !insertmacro MUI_PAGE_LICENSE  "doc\COPYING"
;  !insertmacro MUI_PAGE_COMPONENTS
  !insertmacro MUI_PAGE_DIRECTORY
  !insertmacro MUI_PAGE_STARTMENU Application $STARTMENU_FOLDER
  !insertmacro MUI_PAGE_INSTFILES

; Uninstaller starts with a custom page that asks the user to fill survey

  !insertmacro MUI_UNPAGE_CONFIRM
  !insertmacro MUI_UNPAGE_INSTFILES
;  UninstPage custom un.UninstallSurveyPageText

; Set language
  !insertmacro MUI_LANGUAGE "English"

;****************************************************************************
; Request elevated priviliges

  RequestExecutionLevel admin

;****************************************************************************
; The stuff to install
Section "FWBuilder (required)"

; Set output path to the installation directory.
;  SetOutPath $INSTDIR

  SetOutPath $INSTDIR\resources

  File resources\resources.xml
  File resources\templates.xml
  File resources\objects_init.xml
  File resources\fwbuilder.dtd

  File /r resources\configlets
  File /r resources\help
  File /r resources\os
  File /r resources\platform
  File /r resources\migration

  SetOutPath $INSTDIR

  File /oname=COPYING.doc doc\COPYING
  File "doc\FWBuilder-Routing-LICENSE.txt"
  File "doc\README.iosacl"
  File "doc\README.ipt"
  File "doc\README.ipf"
  File "doc\README.ipfw"
  File "doc\README.pf"
  File "doc\README.pix"
  File "doc\README.pix_routing"
  File "doc\README.routing"
  File "doc\README.nxosacl"
  File "doc\README.junos"

  File /a "fwbuilder.exe"
  File /a "fwbedit.exe"
  File /a "fwb_iosacl.exe"
  File /a "fwb_ipt.exe"
  File /a "fwb_ipf.exe"
  File /a "fwb_ipfw.exe"
  File /a "fwb_pf.exe"
  File /a "fwb_pix.exe"
  File /a "fwb_procurve_acl.exe"
  File /a "fwb_nxosacl.exe"
  File /a "fwb_junosacl.exe"

; Install Qt and its dependencies

  File "/opt/mxe/usr/i686-w64-mingw32.shared/qt5/bin/Qt5Core.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/qt5/bin/Qt5Gui.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/qt5/bin/Qt5Network.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/qt5/bin/Qt5PrintSupport.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/qt5/bin/Qt5Widgets.dll"
  File /r "/opt/mxe/usr/i686-w64-mingw32.shared/qt5/plugins/platforms"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libbz2.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libgcc_s_sjlj-1.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libglib-2.0-0.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libharfbuzz-0.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libiconv-2.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libintl-8.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libpcre-1.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libpcre16-0.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libpcre2-16-0.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libpng16-16.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libstdc++-6.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libwinpthread-1.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libxml2-2.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libxslt-1.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/zlib1.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libssl-1_1.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libcrypto-1_1.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libfreetype-6.dll"
  File "/opt/mxe/usr/i686-w64-mingw32.shared/bin/liblzma-5.dll"
  File /nonfatal "/opt/mxe/usr/i686-w64-mingw32.shared/bin/libzstd.dll"

; Install RCS for these files
;
  File "w32-bin\ci.exe"
  File "w32-bin\co.exe"
  File "w32-bin\rcs.exe"
  File "w32-bin\rcsdiff.exe"
  File "w32-bin\rlog.exe"
  File "w32-bin\diff.exe"
  File "w32-bin\rcslib.dll"

;; Starting with 4.0.2, we now package putty tools with fwbuilder
  File "w32-bin\plink.exe"
  File "w32-bin\pscp.exe"


; Write the installation path into the registry
  WriteRegStr HKLM Software\firewallbuilder.org\${APPNAME}\${GENERATION} "Install_Dir" "$INSTDIR"

; Write the uninstall keys for Windows
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\FWBuilder ${GENERATION}" "DisplayName"     "Firewall Builder ${VERSION}"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\FWBuilder ${GENERATION}" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\FWBuilder ${GENERATION}" "Publisher"       "https://github.com/fwbuilder"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\FWBuilder ${GENERATION}" "DisplayVersion"  "${VERSION}"


; Write file associations and icons

  WriteRegStr HKLM "Software\Classes\.fwb" "" "fwbfile"
  WriteRegStr HKLM "Software\Classes\fwbfile\shell\open\command" "" "$INSTDIR\fwbuilder.exe -noexec %1"
  WriteRegStr HKLM "Software\Classes\fwbfile\DefaultIcon"        "" "$INSTDIR\fwbuilder.exe,0"

  WriteRegStr HKLM "Software\Classes\.fwl" "" "fwlfile"
  WriteRegStr HKLM "Software\Classes\fwlfile\shell\open\command" "" "$INSTDIR\fwbuilder.exe -noexec %1"
  WriteRegStr HKLM "Software\Classes\fwlfile\DefaultIcon"        "" "$INSTDIR\fwbuilder.exe,0"


; Create registry entry for putty session with ssh keepalive
  WriteRegDWORD HKCU "Software\SimonTatham\PuTTY\Sessions\fwb_session_with_keepalive" "PingInterval" "0"
  WriteRegDWORD HKCU "Software\SimonTatham\PuTTY\Sessions\fwb_session_with_keepalive" "PingIntervalSecs" "10"

; ========================================================================
; Configure installer to use our prepackaged plink.exe and pscp.exe but only if it was not configured before
;
; ******** THESE KEYS MUST MATCH THOSE USED BY the class FWBSettings 
;
  ReadRegStr $0 HKCU "Software\firewallbuilder.org\${APPNAME}\${GENERATION}\SSH" "SSHPath"
  StrCmp $0 "" 0 +3
    WriteRegStr HKCU "Software\firewallbuilder.org\${APPNAME}\${GENERATION}\SSH" "SSHPath" "$INSTDIR\plink.exe"
    WriteRegStr HKCU "Software\firewallbuilder.org\${APPNAME}\${GENERATION}\SSH" "SCPPath" "$INSTDIR\pscp.exe"
; ========================================================================

  !insertmacro MUI_STARTMENU_WRITE_BEGIN Application

  SetShellVarContext all

  CreateDirectory "$SMPROGRAMS\$STARTMENU_FOLDER"
  CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\FWBuilder.lnk" "$INSTDIR\fwbuilder.exe" "" "$INSTDIR\fwbuilder.exe"
  CreateShortCut "$SMPROGRAMS\$STARTMENU_FOLDER\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0

  !insertmacro MUI_STARTMENU_WRITE_END

  WriteUninstaller "uninstall.exe"

SectionEnd

;=============================================================================
; uninstall stuff

UninstallText "This will uninstall FWBuilder. Hit next to continue."

;=============================================================================

Section "Uninstall"

  ; remove registry keys
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\FWBuilder ${GENERATION}"
  DeleteRegKey HKLM "Software\firewallbuilder.org\${APPNAME}\${GENERATION}"
  DeleteRegKey /ifempty HKLM "Software\firewallbuilder.org\${APPNAME}"
  DeleteRegKey /ifempty HKLM "Software\firewallbuilder.org"

  DeleteRegKey HKCU "Software\firewallbuilder.org\${APPNAME}\${GENERATION}"
  DeleteRegKey /ifempty HKCU "Software\firewallbuilder.org\${APPNAME}"
  DeleteRegKey /ifempty HKCU "Software\firewallbuilder.org"

  DeleteRegKey HKCR ".fwb"
  DeleteRegKey HKCR ".fwl"
  DeleteRegKey HKCR "fwbfile"
  DeleteRegKey HKCR "fwlfile"
  DeleteRegKey HKCR "FirewallBuilder.AssocFile.FWB"
  DeleteRegKey HKCR "FirewallBuilder.AssocFile.FWL"

; remove files
; MUST REMOVE UNINSTALLER, too
  Delete  $INSTDIR\uninstall.exe
  Delete "$INSTDIR\*.*"
  Delete "$INSTDIR\migration\*.*"
  Delete "$INSTDIR\resources\os\*.*"
  Delete "$INSTDIR\resources\platform\*.*"
  Delete "$INSTDIR\resources\help\*.*"
  Delete "$INSTDIR\resources\configlets\*.*"
  Delete  $INSTDIR\COPYING.doc

  RMDir /r "$INSTDIR"

;  Remove Start menu shortcuts

  !insertmacro MUI_STARTMENU_GETFOLDER Application $STARTMENU_FOLDER

;  SetShellVarContext all

  Delete "$SMPROGRAMS\$STARTMENU_FOLDER\FWBuilder.lnk"
  Delete "$SMPROGRAMS\$STARTMENU_FOLDER\Uninstall.lnk"
  RMDir  "$SMPROGRAMS\$STARTMENU_FOLDER"

SectionEnd

; eof
