;@+leo-ver=5-thin
;@+node:ekr.20101027115225.1494: * @thin nsi-boilerplate.txt
;@@language nsi

;@+<< prolog >>
;@+node:ekr.20101027115225.1495: ** << prolog >>

; Globals.
Var PythonDirectory
    ; Directory containing Pythonw.exe
    ; Set by onInit.  May be set in Python Directory page.

!define PythonExecutable "$PythonDirectory\pythonw.exe"

; Boilerplate
SetCompressor bzip2
Caption "${name} Installer"
AutoCloseWindow false 
SilentInstall normal
CRCCheck on
SetCompress auto
SetDatablockOptimize on
WindowIcon on
ShowInstDetails show
ShowUnInstDetails show

; Locations
Name "${name}"
OutFile "${target_file}"
InstallDir "$PROGRAMFILES\${name}-${version}"
; Just use the NSIS icon.
;Icon "${install_icon}"
;@-<< prolog >>
;@+<< pages >>
;@+node:ekr.20101027115225.1496: ** << pages >>
;@@language nsi

Var StartMenuFolder

; Define the TEXT_TOP for both the MUI_PAGE_DIRECTORY pages.
; "${s1a} ${s2} ${s3}" is the TEXT_TOP for the Install Location page.
; "${s2b} ${s2} ${s3}" is the TEXT_TOP for the Choose Python Folder page.
!define s1a "Setup will install ${name} in the following folder."
!define s1b "Setup will use the following folder as the Python location."
!define s2 "To install in a different folder, click Browse and select another folder."
!define s3 "Click next to continue."

!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE       ${license}
!insertmacro MUI_PAGE_COMPONENTS

; These are the defaults, but defined them here so the back button works.
!define MUI_PAGE_HEADER_TEXT "Choose Install Location"
!define MUI_PAGE_HEADER_SUBTEXT "Choose the folder in which to install ${name}"
!define MUI_DIRECTORYPAGE_TEXT_TOP "${s1a} ${s2} ${s3}"
!define MUI_DIRECTORYPAGE_TEXT_DESTINATION "Destination Folder"
!insertmacro MUI_PAGE_DIRECTORY

; It's so easy: just set these *before* creating another directory page!
!define MUI_PAGE_HEADER_TEXT "Choose Python Location"
!define MUI_PAGE_HEADER_SUBTEXT "Select the top-level Python directory"
!define MUI_DIRECTORYPAGE_TEXT_TOP "${s1b} ${s2} ${s3}"
!define MUI_DIRECTORYPAGE_TEXT_DESTINATION "Python Folder"
!define MUI_DIRECTORYPAGE_VARIABLE $PythonDirectory
!insertmacro MUI_PAGE_DIRECTORY

!insertmacro MUI_PAGE_STARTMENU     "Application" $StartMenuFolder
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

; ----- uninstaller pages -----

!insertmacro MUI_UNPAGE_WELCOME
; !insertmacro MUI_UNPAGE_LICENSE   ${license}
; !insertmacro MUI_UNPAGE_CONFIRM
; !insertmacro MUI_UNPAGE_COMPONENTS ; doesn't actually list components.
!insertmacro MUI_UNPAGE_DIRECTORY
!insertmacro MUI_UNPAGE_INSTFILES
!insertmacro MUI_UNPAGE_FINISH
;@-<< pages >>

; Language should follow pages.
!insertmacro MUI_LANGUAGE "English"

; The order of sections *does* matter slightly.
; It determines the order of items in the components dialog.
; It may also have some other very subtle effects.

;@+others
;@+node:ekr.20101027115225.1497: ** onInit

; Set PythonDirectory to the top-level Python path.
Function .onInit

    ReadRegStr $9 HKLM "SOFTWARE\Python\PythonCore\3.1\InstallPath" ""
    StrCmp $9 "" tryPython30 ok
tryPython30:
    ReadRegStr $9 HKLM "SOFTWARE\Python\PythonCore\3.0\InstallPath" ""
    StrCmp $9 "" tryPython27 ok
tryPython27:
    ReadRegStr $9 HKLM "SOFTWARE\Python\PythonCore\2.7\InstallPath" ""
    StrCmp $9 "" tryPython26 ok
tryPython26:
    ReadRegStr $9 HKLM "SOFTWARE\Python\PythonCore\2.6\InstallPath" ""
    StrCmp $9 "" oops ok
oops:
    MessageBox MB_OK "Python not found"
    StrCpy $PythonDirectory ""
    Goto done
ok:
    StrCpy $PythonDirectory $9
done:

FunctionEnd ; End .onInit
;@+node:ekr.20101027115225.1504: ** Section Leo

; This section is called "Leo" because it defines the "Leo" component.

Section "Leo" SEC01

; ---- Start of manifest-related data...

!include nsi-install-files.txt

; ---- End of manifest-related data...

; End of "Leo" SEC01
SectionEnd
;@+node:ekr.20101027115225.1499: ** Section File Association

Section "${ext} File Association" SEC02

    SectionIn 1 2 3 4

    ; back up old value of extension in case some other program was using it
    ;;ReadRegStr $1 HKCR "${ext}" ""
    ;; StrCmp $1 "" Label1
    ;; StrCmp $1 "${name}File" Label1
    ;; WriteRegStr HKCR "${ext}" "backup_val" $1
;; Label1:

    WriteRegStr HKCR "${ext}" "" "${name}File"
    WriteRegStr HKCR "${name}File" "" "${name} File"
    WriteRegStr HKCR "${name}File\shell" "" "open"
    WriteRegStr HKCR "${name}File\DefaultIcon" "" '"$INSTDIR\${app_icon}"'
    WriteRegStr HKCR "${name}File\shell\open\command" "" \
        '"${PythonExecutable}" "$INSTDIR\launchLeo.py %*"'

SectionEnd
;@+node:ekr.20101027115225.1498: ** Section Desktop Shortcut

Section "${name} Desktop Shortcut" SEC03

  ; Apparently, this sets the "Start in folder" box!!!"
  SetOutPath "$INSTDIR\leo"

  ; CreateShortcut.
  ; link.lnk
  ; target.file
  ; [parameters]
  ; [icon.file] [icon_index_number]
  ;; [start_options] one of: SW_SHOWNORMAL, SW_SHOWMAXIMIZED, SW_SHOWMINIMIZED, or "". 
  ;; [keyboard_shortcut]

  CreateShortCut "$DESKTOP\${name}.lnk" \
    '"${PythonExecutable}"' \
    '"$INSTDIR\launchLeo.py"' \
    '"$INSTDIR\${app_icon}"' 0

SectionEnd
;@+node:ekr.20101027115225.1500: ** Section Start Menu

Section "${name} Start Menu" SEC04

  CreateDirectory "$SMPROGRAMS\${name}"

  CreateShortCut "$SMPROGRAMS\${name}\${name}.lnk" \
        '"${PythonExecutable}"' \
        '"$INSTDIR\launchLeo.py"' \ 
        '"$INSTDIR\${app_icon}"' 0

  CreateShortCut "$SMPROGRAMS\${name}\Uninstall.lnk" "$INSTDIR\uninst.exe"

SectionEnd
;@+node:ekr.20101027115225.1501: ** Section -Post

Section -Post

  WriteRegStr HKLM ${leo_hklm} "" "$INSTDIR"
  WriteUninstaller "$INSTDIR\uninst.exe"
  WriteRegStr HKLM "${uninst_key}" "DisplayName" "${name}-${version} (remove only)"
  WriteRegStr HKLM "${uninst_key}" "UninstallString" "$INSTDIR\uninst.exe"
  WriteRegStr HKLM "${uninst_key}" "DisplayVersion" "${version}"
  WriteRegStr HKLM "${uninst_key}" "URLInfoAbout" "${site}"
  WriteRegStr HKLM "${uninst_key}" "Publisher" "${publisher}"

SectionEnd
;@+node:ekr.20101027115225.1503: ** Section Uninstall

Section Uninstall

    DeleteRegKey HKLM "${leo_hklm}"

    ; Delete files & directories only if we own them!
    ;; ReadRegStr $1 HKCR "${ext}" ""
    ;; StrCmp $1 "${name}File" 0 FinishUninstall

    ; Delete files & directories only if there is no backup_val key.
    ;; ReadRegStr $1 HKCR "${ext}" "backup_val"
    ;; StrCmp $1 "" 0 DeleteKeysAndFiles
    DeleteRegKey HKCR "${ext}"
    DeleteRegKey HKCR "${name}File"

    ; Goto FinishUninstall
; DeleteKeysAndFiles:
   ;; WriteRegStr HKCR "${ext}" "" $1
   ;; DeleteRegValue HKCR "${ext}" "backup_val"

; ---- Start of manifest-related data...

!include nsi-uninstall-files.txt

; ---- End of manifest related data.

; FinishUninstall:

    Delete "$SMPROGRAMS\${name}\Uninstall.lnk"
    Delete "$SMPROGRAMS\${name}.lnk"
    RMDir  "$SMPROGRAMS\${name}-${version}"

    Delete "$DESKTOP\${name}.lnk"

    ; "Software\Microsoft\Windows\CurrentVersion\Uninstall\leo"
    DeleteRegKey HKLM "${uninst_key}" 
    SetAutoClose false

SectionEnd ; end Uninstall section
;@-others
;@-leo
