; Script generated by the HM NIS Edit Script Wizard.

; HM NIS Edit Wizard helper defines
!define PRODUCT_NAME "BIOVIA Foundation 2022SP1"
!define PRODUCT_SHORT_NAME "Foundation2022SP1"
!define PRODUCT_VERSION_MAJOR "22"
!define PRODUCT_VERSION_MINOR "1"
!define PRODUCT_VERSION_RELEASE "101"
!define PRODUCT_VERSION_BUILD "10302"
!define PRODUCT_VERSION "${PRODUCT_VERSION_MAJOR}.${PRODUCT_VERSION_MINOR}.${PRODUCT_VERSION_RELEASE}.${PRODUCT_VERSION_BUILD}"
!define PRODUCT_PUBLISHER "Dassault Systèmes"
!define PRODUCT_WEB_SITE "http://www.3ds.com"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"
!define PRODUCT_STARTMENU_REGVAL "NSIS:StartMenuDir"

;Global variables for Summary screen
Var servFound
var HTTPPortNumber
var HTTPSPortNumber
var ShutDwnPortNumber
var LicFilePath
Var UpSelected
;Var InstallType
Var upgradeSilent

; MUI 1.67 compatible ------
!include "MUI2.nsh"
!include "include\defines.nsh"
!include "include\CharStrip.nsh"
;!include "include\GetInQuotes.nsh"
!include "include\ConfigRead.nsh"
!include "include\StrngSrch.nsh"
!include "include\SilentFunctions.nsh"
!include "include\SrchWinServ.nsh"
;!include "include\UpgradeNew.nsh"
!include "include\ExistHubShow.nsh"
!include "include\ReplaceLine.nsh"
!include "include\FileSearch.nsh"
!include "include\FoundationDialog.nsh"
!include "include\SummaryPage.nsh"
!include "include\Ports.nsh"
!include "FileFunc.nsh"
!include "include\FileExt.nsh"
!include "include\GetParent.nsh"
!include "include\AdminCheck.nsh"
!include "include\TestPorts.nsh"
!include "TextReplace.nsh"
!include "InstallOptions.nsh"
!include "include\UninstBtnAction.nsh"
!include "include\HubPageLeave.nsh"

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "images\hub_new.ico"
!define MUI_UNICON "images\hub_new.ico"

!define MUI_WELCOMEPAGE_TITLE_3LINES
!define MUI_WELCOMEFINISHPAGE_BITMAP "images\NSIS-bg3DS.bmp"
;!define MUI_WELCOMEFINISHPAGE_BITMAP_NOSTRETCH
!define MUI_WELCOMEPAGE_TEXT "This wizard will guide you through the installation of $(^NameDA).$\r$\n$\r$\nIt is recommended that you close all other applications before starting Setup.$\r$\n$\r$\n$_CLICK"
!define MUI_WELCOMEFINISHPAGE_BITMAP_TRANSPARENT_TEXT
!define /date MYTIMESTAMP "%Y%m%d-%H-%M-%S"

; Welcome page
!insertmacro MUI_PAGE_WELCOME
; License page
!define MUI_LICENSEPAGE_CHECKBOX
;!insertmacro MUI_PAGE_LICENSE "Agreement.rtf"
;Upgrade/New Install
;Page custom UpgradeNewShow UpgradeNewLeave
;Existing hub Service page
;Uninstall exisiting hub
Page custom ExistHub ExisthubLeave
; Directory page
!insertmacro MUI_PAGE_DIRECTORY

BrandingText "Dassault Systèmes"
; Start menu page
;var ICONS_GROUP

;!define MUI_STARTMENUPAGE_NODISABLE
;!define MUI_STARTMENUPAGE_DEFAULTFOLDER "BIOVIA"
;!define MUI_STARTMENUPAGE_REGISTRY_ROOT "${PRODUCT_UNINST_ROOT_KEY}"
;!define MUI_STARTMENUPAGE_REGISTRY_KEY "${PRODUCT_UNINST_KEY}"
;!define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "${PRODUCT_STARTMENU_REGVAL}"
;!insertmacro MUI_PAGE_STARTMENU Application $ICONS_GROUP

Page custom HUBPageShow HUBPageLeave
Page custom SummaryPage

Function ExistHub
LogSet on
  call functionSrchServ
  SimpleSC::ExistsService "$servFound"
  Pop $0 ; returns an errorcode if the service doesn?t exists (<>0)/service exists (0)

  ${If} $0 == 0
    call fnc_ExistingHub_Show
  ${EndIf}
LogSet off
FunctionEnd

Function ExisthubLeave

FunctionEnd

Function SummaryPage
  call fnc_SummaryDialog_Show
FunctionEnd

; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
!define MUI_TEXT_INSTALLING_SUBTITLE "$(^NameDA) is being installed."

!define MUI_FINISHPAGE_RUN
!define MUI_FINISHPAGE_RUN_CHECKED
!define MUI_FINISHPAGE_RUN_TEXT "Open ${PRODUCT_NAME} Home Page"
!define MUI_FINISHPAGE_RUN_FUNCTION "LaunchLink"
; Finish page
!define  MUI_FINISHPAGE_TITLE "${PRODUCT_NAME} Installation Complete"

!insertmacro MUI_PAGE_FINISH
!define MUI_TEXT_FINISH_INFO_TEXT "$(^NameDA) has been installed on your computer."

Function .onInit
;Rename $INSTDIR\Install.log $INSTDIR\Install-${MYTIMESTAMP}.log
  !insertmacro INSTALLOPTIONS_EXTRACT_AS "Silent.ini" "Silent"

  call IsUserAdmin
  call functionSrchServ
    ${StrContains} $0 "16_" "$servFound "
    StrCmp $0 "" notfound
      MessageBox mb_iconstop "Upgrade from any version of 2016 Foundation to Foundation 2018 not supported!"
      Abort
    notfound:
    ;MessageBox MB_OK 'found a compatible version'
FunctionEnd

Function .onInstFailed
   DetailPrint "${PRODUCT_NAME} install failed."
  FunctionEnd

;code to launch hub from finish page
Function LaunchLink

  ExecShell "" "$INSTDIR\${PRODUCT_NAME}.url"
FunctionEnd

Function HUBPageShow
  call PopulatePorts
  call fnc_FoundationDialog_Show

FunctionEnd

Function HUBPageLeave
 call HubPageValidate
FunctionEnd

; Uninstaller pages

!insertmacro MUI_UNPAGE_INSTFILES
!define MUI_UNTEXT_UNINSTALLING_SUBTITLE "$(^NameDA) is being uninstalled."

; Language files
!insertmacro MUI_LANGUAGE "English"

; MUI end ------

Name "${PRODUCT_NAME}"
OutFile "${PRODUCT_SHORT_NAME}.exe"
RequestExecutionLevel admin
InstallDir "$PROGRAMFILES64\BIOVIA\Foundation\"
ShowInstDetails nevershow
ShowUnInstDetails nevershow

Section "MainSection" SEC01

LogSet on
 SetOutPath "$INSTDIR"
  ${If} ${Silent}
  DetailPrint "Installing in SILENT mode...."
  ReadIniStr $HTTPPortNumber "$EXEDIR\Silent.ini" "Args" "http"
  ReadIniStr $HTTPSPortNumber "$EXEDIR\Silent.ini" "Args" "https"
  ReadIniStr $ShutDwnPortNumber "$EXEDIR\Silent.ini" "Args" "shutdown"
  ReadIniStr $LicFilePath "$EXEDIR\Silent.ini" "Args" "LicenseFile"
  ReadIniStr $upgradeSilent "$EXEDIR\Silent.ini" "Args" "Upgrade"

  DetailPrint "value of upgrade is :$upgradeSilent"
  DetailPrint "value of httpport is :$HTTPPortNumber"
  DetailPrint "value of httpport is :$HTTPSPortNumber"
  DetailPrint "value of httpport is :$ShutDwnPortNumber"
  DetailPrint "the license file path is :$LicFilePath"
  ${if} $upgradeSilent == "yes"
  DetailPrint "Upgrade existing in Silent mode..."
  call functionUpgradeSi
  ${EndIf}
  ${EndIf}
  SetOverwrite on
  IfFileExists $LicFilePath 0 +2
  CopyFiles "$LicFilePath" "$INSTDIR"
  WriteINIStr $EXEDIR\silent.ini Args Http $HTTPPortNumber
  WriteINIStr $EXEDIR\silent.ini Args Https $HTTPSPortNumber
  WriteINIStr $EXEDIR\silent.ini Args LicenseFile $LicFilePath

  ;File /r /x "linux" "hub\*.*"
  SetOutPath "$INSTDIR\gateway"
  ;File /r  "gateway\*.*"
  ;File "..\Version.txt"
  DetailPrint "Extracting hub package..."
  SetOutPath "$INSTDIR\Images"
  SetOverwrite on
  File "images\Hub.ico"
  File "images\hub_new.ico"

  IfFileExists "$INSTDIR\conf\backup\tomcat.properties" 0 +2
  CopyFiles "$INSTDIR\conf\backup\tomcat.properties" "$INSTDIR\conf\tomcat.properties"

  IfFileExists "$INSTDIR\conf\backup\app-config.groovy" 0 +2
  CopyFiles "$INSTDIR\conf\backup\app-config.groovy" "$INSTDIR\conf\app-config.groovy"

  IfFileExists "$INSTDIR\conf\backup\gwconfig" 0 +2
  CopyFiles "$INSTDIR\conf\backup\gwconfig" "$INSTDIR\gateway\gwconfig"

  IfFileExists "$INSTDIR\conf\backup\ServerGatewayService.exe.config" 0 +2
  CopyFiles "$INSTDIR\conf\backup\ServerGatewayService.exe.config" "$INSTDIR\gateway\ServerGatewayService.exe.config"


    Push "$INSTDIR\conf\tomcat.properties" ; file to modify
    Push "port.https=" ; string that a line must begin with *WS Sensitive*
    Push "port.https=$HTTPSPortNumber" ; string to replace whole line with
    Call ReplaceLineStr
    Push "$INSTDIR\conf\tomcat.properties"
    Push "port.http="
    Push "port.http=$HTTPPortNumber"
    Call ReplaceLineStr
    Push "$INSTDIR\conf\tomcat.properties"
    Push "port.shutdown="
    Push "port.shutdown=$ShutDwnPortNumber"
    Call ReplaceLineStr
    Push "$INSTDIR\conf\tomcat.properties"
    Push "tlsCompression.enable="
    Push "tlsCompression.enable=true"
    Call ReplaceLineStr
    Push "$INSTDIR\conf\tomcat.properties"
    Push "compression.enable="
    Push "compression.enable=true"
    Call ReplaceLineStr
    Push "$INSTDIR\conf\tomcat.properties"
    Push "tlsCompressableMimeTypes="
    Push "tlsCompressableMimeTypes=text/html,text/xml,text/plain,application/javascript,text/javascript,text/css,application/xhtml+xml,application/json,text/csv,application/csv"
    Call ReplaceLineStr
    Push "$INSTDIR\conf\tomcat.properties"
    Push "compressableMimeTypes="
    Push "compressableMimeTypes=text/html,text/xml,text/plain,application/javascript,text/javascript,text/css,application/xhtml+xml,application/json,text/csv,application/csv"
    Call ReplaceLineStr

  nsExec::Exec "$INSTDIR\bin\install.bat"
  nsExec::Exec "$INSTDIR\bin\startservice.bat"

  call functionSrchServ
  ; Install the integration gateway service
  SimpleSC::InstallService "BIOVIAIntegrationGatewayServer" "BIOVIA Integration Gateway Server" "16" "2" "$INSTDIR\gateway\ServerGatewayService.exe" "$servFound" "" ""
  SimpleSC::SetServiceDescription "BIOVIAIntegrationGatewayServer" "BIOVIA Integration Gateway Server Service"
  SimpleSC::StartService "BIOVIAIntegrationGatewayServer" "" 30
  Pop $0 ; returns an errorcode (<>0) otherwise success (0)

; Shortcuts
 ; !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
 ; !insertmacro MUI_STARTMENU_WRITE_END
  logSet off
SectionEnd

Section -AdditionalIcons
logSet on
 SetShellVarContext all
  SetOutPath $INSTDIR
  IpConfig::GetHostName
  Pop $0
  Pop $R0
  IpConfig::GetPrimaryDNSSuffix
  Pop $0
  Pop $R1

 ; !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
  ;ReadRegStr $0 HKLM "System\CurrentControlSet\Control\ComputerName\ActiveComputerName" "ComputerName"
  ${If} $R1 != ""
    WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "https://$R0.$R1:$HTTPSPortNumber/foundation/hub"
  ${Else}
    WriteIniStr "$INSTDIR\${PRODUCT_NAME}.url" "InternetShortcut" "URL" "https://$R0:$HTTPSPortNumber/foundation/hub"
  ${EndIf}
  CreateDirectory "$SMPROGRAMS\BIOVIA\Foundation"
  CreateShortCut "$SMPROGRAMS\BIOVIA\Foundation\${PRODUCT_NAME}.lnk" "$INSTDIR\${PRODUCT_NAME}.url" "" "$INSTDIR\images\hub_new.ico"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\images\Hub.ico"
  ;CreateShortCut "$SMPROGRAMS\BIOVIA\Foundation\Uninstall.lnk" "$INSTDIR\uninst.exe"
  ;!insertmacro MUI_STARTMENU_WRITE_END
   logSet off
SectionEnd

Section -Post
logSet on
 ; SetOutPath $INSTDIR
  
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\uninst.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "${PRODUCT_VERSION}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "URLInfoAbout" "${PRODUCT_WEB_SITE}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "Publisher" "${PRODUCT_PUBLISHER}"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "InstallLocation" "$INSTDIR"
   SetRegView 64
   WriteRegStr HKLM "Software\BIOVIA\Foundation" "INSTALL_DIR" "$INSTDIR"
   WriteRegStr HKLM "Software\BIOVIA\Foundation" "VERSION" "${PRODUCT_VERSION}"
   DetailPrint "${PRODUCT_NAME} installed successfully."
  logSet off
SectionEnd


Function un.onUninstSuccess
  HideWindow
  IfSilent +2
   MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) was successfully removed from your computer."
FunctionEnd

Function un.onInit
  IfSilent +3
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to completely remove $(^Name) and all of its components?" IDYES +2
  Abort
FunctionEnd

Section Uninstall
logSet on
DetailPrint "Uninstall Foundation begin..."
SetShellVarContext all
 ; !insertmacro MUI_STARTMENU_GETFOLDER "Application" $ICONS_GROUP
 CopyFiles "$INSTDIR\conf\tomcat.properties" "C:\Hub-backup\conf\tomcat.properties"
 CopyFiles "$INSTDIR\conf\app-config.groovy" "C:\Hub-backup\conf\app-config.groovy"
 ; backup gateway config files in the install directory to reinstate during upgrade
 CopyFiles "$INSTDIR\gateway\ServerGatewayService.exe.config" "$INSTDIR\conf\backup\ServerGatewayService.exe.config"
 CopyFiles "$INSTDIR\gateway\gwconfig" "$INSTDIR\conf\backup\gwconfig"
 ; backup files to an independent directory if the install directory gets wiped out
 CopyFiles "$INSTDIR\gateway\ServerGatewayService.exe.config" "C:\Hub-backup\gateway\ServerGatewayService.exe.config"
 CopyFiles "$INSTDIR\gateway\gwconfig" "C:\Hub-backup\gateway\gwconfig"
  Delete "$INSTDIR\${PRODUCT_NAME}.url"
  Delete "$INSTDIR\uninst.exe"

  ;Delete "$SMPROGRAMS\BIOVIA\Foundation\Uninstall.lnk"
  Delete "$SMPROGRAMS\BIOVIA\Foundation\${PRODUCT_NAME}.lnk"

  nsExec::Exec "$INSTDIR\bin\stopservice.bat"
  nsExec::Exec "$INSTDIR\bin\uninstall.bat"
  SimpleSC::StopService "BIOVIAIntegrationGatewayServer" 1 30
  Pop $0
  SimpleSC::RemoveService "BIOVIAIntegrationGatewayServer"
  Pop $0
  RMDir "$SMPROGRAMS\BIOVIA\Foundation"
  RMDir "$SMPROGRAMS\BIOVIA"
  RMDir /r "$INSTDIR\jre"
  RMDir /r "$INSTDIR\lib"
  RMDir /r "$INSTDIR\bin"
  RMDir /r "$INSTDIR\webapps"
  RMDir /r "$INSTDIR\work"
  RMDir /r "$INSTDIR\Images"
  RMDir /r "$INSTDIR\gateway"
  Delete "$INSTDIR\hub.jar"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
   SetRegView 64
  DeleteRegKey HKLM "Software\BIOVIA\Foundation"
  DetailPrint "Successfull uninstalled ${PRODUCT_NAME}"
  SetAutoClose true
  logSet off

SectionEnd
