; Script generated by the HM NIS Edit Script Wizard.

; HM NIS Edit Wizard helper defines
!define PRODUCT_NAME "@NAME@"
!define PRODUCT_VERSION "@VERSION@"
!define PRODUCT_VARIANT "@VARIANT@"
!define PRODUCT_PUBLISHER "Universite de Montreal"
!define PRODUCT_WEB_SITE "http://gambitscheme.org"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\uninstall-gambit.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME} v${PRODUCT_VERSION}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

; MUI 1.67 compatible ------
!include "MUI.nsh"

!include "AddToPath.nsh"

; MUI Settings
!define MUI_ABORTWARNING
!define MUI_ICON "@ICON_FILE@"
!define MUI_UNICON "@UNICON_FILE@"

; Welcome page
!define MUI_WELCOMEPAGE_TEXT "-----------------------------------------------------------------------\r\n\r\nThe Gambit Scheme system is a complete, portable, efficient and reliable implementation of the Scheme programming language. For more information please visit\r\n\r\nhttp://gambitscheme.org\r\n\r\nand the Gambit chatroom https://gitter.im/gambit/gambit\r\n\r\n-----------------------------------------------------------------------\r\n\r\nThis wizard will guide you through the installation of ${PRODUCT_NAME} v${PRODUCT_VERSION} (${PRODUCT_VARIANT} variant).\r\n\r\nClick Next to continue\r\n"
!insertmacro MUI_PAGE_WELCOME

; Directory page
!insertmacro MUI_PAGE_DIRECTORY
; Instfiles page
!insertmacro MUI_PAGE_INSTFILES
; Finish page
!insertmacro MUI_PAGE_FINISH

; Uninstaller pages
!insertmacro MUI_UNPAGE_INSTFILES

; Language files
!insertmacro MUI_LANGUAGE "English"

; MUI end ------

Name "${PRODUCT_NAME} v${PRODUCT_VERSION}"
OutFile "@EXE_FILE@"
InstallDir "$PROGRAMFILES\${PRODUCT_NAME}\"
InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""
ShowInstDetails show
ShowUnInstDetails show

Function StrSlash
; Push $filenamestring (e.g. 'c:\this\and\that\filename.htm')
; Push "\"
; Call StrSlash
; Pop $R0
; ;Now $R0 contains 'c:/this/and/that/filename.htm'
  Exch $R3 ; $R3 = needle ("\" or "/")
  Exch
  Exch $R1 ; $R1 = String to replacement in (haystack)
  Push $R2 ; Replaced haystack
  Push $R4 ; $R4 = not $R3 ("/" or "\")
  Push $R6
  Push $R7 ; Scratch reg
  StrCpy $R2 ""
  StrLen $R6 $R1
  StrCpy $R4 "\"
  StrCmp $R3 "/" loop
  StrCpy $R4 "/"  
loop:
  StrCpy $R7 $R1 1
  StrCpy $R1 $R1 $R6 1
  StrCmp $R7 $R3 found
  StrCpy $R2 "$R2$R7"
  StrCmp $R1 "" done loop
found:
  StrCpy $R2 "$R2$R4"
  StrCmp $R1 "" done loop
done:
  StrCpy $R3 $R2
  Pop $R7
  Pop $R6
  Pop $R4
  Pop $R2
  Pop $R1
  Exch $R3
FunctionEnd

Section "MainSection" SEC01

  Var /GLOBAL instdir_short
  Var /GLOBAL instdir_short_slash

  StrCpy $9 "$INSTDIR"

  SetOverwrite try
  SetOutPath "$INSTDIR"
  File /r "C:\${PRODUCT_NAME}\v${PRODUCT_VERSION}"
  SetOutPath "$INSTDIR\v${PRODUCT_VERSION}\bin"
  File "binpatch.exe"
  SetOutPath "$INSTDIR"

  GetFullPathName /SHORT $1 "$INSTDIR"

  StrCpy $instdir_short "$1"

  ExecWait '$instdir_short\v${PRODUCT_VERSION}\bin\binpatch.exe "@DOS_SOURCE_DIR@\v${PRODUCT_VERSION}" "$instdir_short\v${PRODUCT_VERSION}" "$instdir_short\v${PRODUCT_VERSION}\bin\gsi.exe"' $0
  ExecWait '$instdir_short\v${PRODUCT_VERSION}\bin\binpatch.exe "@DOS_SOURCE_DIR@\v${PRODUCT_VERSION}" "$instdir_short\v${PRODUCT_VERSION}" "$instdir_short\v${PRODUCT_VERSION}\bin\gsc.exe"' $0
  ExecWait '$instdir_short\v${PRODUCT_VERSION}\bin\binpatch.exe "@DOS_SOURCE_DIR@\v${PRODUCT_VERSION}" "$instdir_short\v${PRODUCT_VERSION}" "$instdir_short\v${PRODUCT_VERSION}\bin\six.exe"' $0
  ExecWait '$instdir_short\v${PRODUCT_VERSION}\bin\binpatch.exe "@DOS_SOURCE_DIR@\v${PRODUCT_VERSION}" "$instdir_short\v${PRODUCT_VERSION}" "$instdir_short\v${PRODUCT_VERSION}\lib\libgambit.a"' $0
  ExecWait '$instdir_short\v${PRODUCT_VERSION}\bin\binpatch.exe "@DOS_SOURCE_DIR@\v${PRODUCT_VERSION}" "$instdir_short\v${PRODUCT_VERSION}" "$instdir_short\v${PRODUCT_VERSION}\lib\libgambit.lib"' $0
  ExecWait '$instdir_short\v${PRODUCT_VERSION}\bin\binpatch.exe "@DOS_SOURCE_DIR@\v${PRODUCT_VERSION}" "$instdir_short\v${PRODUCT_VERSION}" "$instdir_short\v${PRODUCT_VERSION}\lib\libgambit.dll"' $0

  Push "$instdir_short"
  Push "\"
  Call StrSlash
  Pop $1

  StrCpy $instdir_short_slash "$1"

  Push "$instdir_short\v${PRODUCT_VERSION}\bin"
  Call AddToPath

  Push "LIBRARY_PATH"
  Push "$instdir_short_slash/v${PRODUCT_VERSION}/lib"
  Call AddToEnvVar

  Push "CPATH"
  Push "$instdir_short_slash/v${PRODUCT_VERSION}/include"
  Call AddToEnvVar

  Push "LIB"
  Push "$instdir_short\v${PRODUCT_VERSION}\lib"
  Call AddToEnvVar

  Push "INCLUDE"
  Push "$instdir_short\v${PRODUCT_VERSION}\include"
  Call AddToEnvVar

  Delete '$INSTDIR\v${PRODUCT_VERSION}\bin\binpatch.exe'

  ; Create shortcuts

  CreateDirectory "$SMPROGRAMS\Gambit-C"
  CreateShortCut "$SMPROGRAMS\Gambit-C\Interpreter.lnk" "$INSTDIR\v${PRODUCT_VERSION}\bin\gsi.exe"
  CreateShortCut "$SMPROGRAMS\Gambit-C\Compiler.lnk" "$INSTDIR\v${PRODUCT_VERSION}\bin\gsc.exe"
  CreateShortCut "$SMPROGRAMS\Gambit-C\Documentation.lnk" "$INSTDIR\v${PRODUCT_VERSION}\doc\gambit-c.html"
  CreateShortCut "$SMPROGRAMS\Gambit-C\Uninstall.lnk" "$INSTDIR\v${PRODUCT_VERSION}\bin\uninstall-gambit.exe"

SectionEnd

Section -Post

  WriteUninstaller "$INSTDIR\v${PRODUCT_VERSION}\bin\uninstall-gambit.exe"

  WriteRegStr HKLM "${PRODUCT_DIR_REGKEY}" "" "$INSTDIR\"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayName" "$(^Name)"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\v${PRODUCT_VERSION}\bin\uninstall-gambit.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayIcon" "$INSTDIR\v${PRODUCT_VERSION}\bin\uninstall-gambit.exe"
  WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "DisplayVersion" "v${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}"

SectionEnd

Function un.onInit
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to completely remove ${PRODUCT_NAME} v${PRODUCT_VERSION} (${PRODUCT_VARIANT} variant) and all of its components?" IDYES +2
  Abort
FunctionEnd

Section "Uninstall"

  Var /GLOBAL instdir_short2

  ; How come the value of $INSTDIR here is not the same as in the main section?
  GetFullPathName /SHORT $1 "$INSTDIR\..\.."

  StrCpy $instdir_short2 "$1"

  IfFileExists "$instdir_short2\v${PRODUCT_VERSION}\bin\uninstall-gambit.exe" can_be_uninst has_been_uninst

  has_been_uninst:

  MessageBox MB_ICONINFORMATION|MB_OK "${PRODUCT_NAME} v${PRODUCT_VERSION} (${PRODUCT_VARIANT} variant) does not seem to be installed! ($instdir_short2\v${PRODUCT_VERSION}\bin\uninstall-gambit.exe does not exist)"
  Abort

  can_be_uninst:

  RMDir /r "$instdir_short2\v${PRODUCT_VERSION}"

  DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"
  DeleteRegKey HKLM "${PRODUCT_DIR_REGKEY}"

  Push "$instdir_short2\v${PRODUCT_VERSION}\bin"
  Call un.RemoveFromPath

  Delete "$SMPROGRAMS\Gambit-C\Interpreter.lnk"
  Delete "$SMPROGRAMS\Gambit-C\Compiler.lnk"
  Delete "$SMPROGRAMS\Gambit-C\Documentation.lnk"
  Delete "$SMPROGRAMS\Gambit-C\Uninstall.lnk"
  RMDir "$SMPROGRAMS\Gambit-C"
 
SectionEnd
