# Auto-generated by EclipseNSIS Script Wizard
# Sep 2, 2006 2:27:20 PM

Name jRAM
# Defines
!define REGKEY "SOFTWARE\$(^Name)"
!define VERSION 0.9
!define COMPANY MidnightJava
!define URL http://midnightjava.net

# Included files
!include Sections.nsh
!include "${NSISDIR}\Include\MUI.nsh"

# Interface settings
!define MUI_FINISHPAGE_NOAUTOCLOSE
!define MUI_UNFINISHPAGE_NOAUTOCLOSE



# Reserved Files
ReserveFile "${NSISDIR}\Plugins\StartMenu.dll"

# Variables
Var StartMenuGroup

# Installer pages
#Page license
#Page directory
#Page custom StartMenuGroupSelect "" ": Start Menu Folder"
#Page instfiles

#Page settings
!define MUI_PAGE_HEADER_TEXT Setup - jRAM
!define MUI_WELCOMEPAGE_TITLE "Welcome to the jRAM Setup Wizard"
!define MUI_WELCOMEPAGE_TEXT \
    "This will install jRAM version ${VERSION} on your computer.\n\n\\
     Click Next to continue, or Cancel to exit Setup."
!define MUI_LICENSEPAGE_TEXT_TOP "jRAM License"
!define MUI_LICENSEPAGE_CHECKBOX
!define MUI_LICENSEPAGE_CHECKBOX_TEXT "I agree"
!define MUI_DIRECTORYPAGE_TEXT_TOP "Select Destination Directory"
!define MUI_DIRECTORYPAGE_TEXT_DESTINATION \
    "Select the folder where you would like jRAM to be installed, then click Next."

!define MUI_STARTMENUPAGE_TEXT_TOP \
    "Select the Start Menu folder in which you would like Setup to create jRAM's \
    shortcuts, then click Next."
!define MUI_STARTMENUPAGE_DEFAULTFOLDER $SMPROGRAMS\$(^Name)
!define MUI_STARTMENUPAGE_REGISTRY_ROOT HKLM
!define MUI_STARTMENUPAGE_REGISTRY_KEY ${REGKEY}
!define MUI_STARTMENUPAGE_REGISTRY_VALUENAME StartMenuGroup
!define MUI_INSTFILESPAGE_FINISHHEADER_TEXT "Installation successful"
!define MUI_INSTFILESPAGE_ABORTHEADER_TEXT "Installation aborted"
!define MUI_FINISHPAGE_TITLE "Completing installation of jRAM"
!define MUI_FINISHPAGE_TEXT "Setup has finished installing jRAM on your computer. \
    The application may be launched by sleecting the installed icons.\n\n \
    Click Finish to exit Setup."
!define MUI_FINISHPAGE_BUTTON Finish
!define MUI_FINISHPAGE_RUN $INSTDIR\jram.exe
!define MUI_FINISHPAGE_RUN_TEXT "Run jRAM"
!define MUI_FINISHPAGE_SHOWREADME "$INSTDIR\readme.txt"
!define MUI_FINISHPAGE_SHOWREADME_TEXT "View the readme file"
!define MUI_UNCONFIRMPAGE_TEXT_TOP "Do you want to un-install jRAM?"

# Installer pages
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_LICENSE license.txt
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_STARTMENU install_page_1 $INSTDIR
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH

#Uninstaller pages
!insertmacro MUI_UNPAGE_WELCOME
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_FINISH

# Installer attributes
OutFile jram_setup.exe
InstallDir $PROGRAMFILES\jRAM
CRCCheck on
XPStyle on
Icon "${NSISDIR}\Contrib\Graphics\Icons\classic-install.ico"
ShowInstDetails show
AutoCloseWindow false
;LicenseData "..\..\JRAM Build\setup\license.txt"
;BGGradient 000080 000000 FFFFFF
VIProductVersion 1.0.0.0
VIAddVersionKey ProductName jRAM
VIAddVersionKey ProductVersion "${VERSION}"
VIAddVersionKey CompanyName "${COMPANY}"
VIAddVersionKey CompanyWebsite "${URL}"
VIAddVersionKey FileVersion ""
VIAddVersionKey FileDescription ""
VIAddVersionKey LegalCopyright ""
InstallDirRegKey HKLM "${REGKEY}" Path
UninstallIcon "${NSISDIR}\Contrib\Graphics\Icons\classic-uninstall.ico"
ShowUninstDetails show

# Installer sections
Section -Main SEC0000
    SetOutPath $INSTDIR
    SetOverwrite on
    File "..\..\JRAM Build\setup\readme.txt"
    File "..\..\JRAM Build\setup\jram.jar"
    File "jram.exe"
    File "..\..\JRAM Build\setup\license.txt"
    SetOutPath $INSTDIR\help
    File /r "..\..\JRAM Build\setup\help\*"
    #SetOutPath $SMPROGRAMS\jRAM
    CreateDirectory $SMPROGRAMS\$StartMenuGroup
    SetOutPath $INSTDIR
    !insertmacro MUI_STARTMENU_WRITE_BEGIN install_page_1
        CreateShortcut "$SMPROGRAMS\$StartMenuGroup\$(^Name).lnk" $INSTDIR\jram.exe
        CreateShortcut "$SMPROGRAMS\$StartMenuGroup\license.lnk" $INSTDIR\license.txt
        CreateShortcut "$SMPROGRAMS\$StartMenuGroup\readme.lnk" $INSTDIR\readme.txt
        CreateShortcut "$SMPROGRAMS\$StartMenuGroup\error log.lnk" $INSTDIR\err.log
        CreateShortcut "$SMPROGRAMS\$StartMenuGroup\events log.lnk" $INSTDIR\events.log
    !insertmacro MUI_STARTMENU_WRITE_END
    #SetOutPath $QUICKLAUNCH
    CreateShortcut $QUICKLAUNCH\$(^Name).lnk $INSTDIR\jram.exe
    #SetOutPath $SMPROGRAMS\jRAM
    WriteRegStr HKLM "${REGKEY}\Components" Main 1
SectionEnd

Section -post SEC0001
    WriteRegStr HKLM "${REGKEY}" Path $INSTDIR
    WriteRegStr HKLM "${REGKEY}" StartMenuGroup $StartMenuGroup
    WriteRegStr HKLM "${REGKEY}" Version ${VERSION}
    WriteUninstaller $INSTDIR\jram_uninstall.exe
    SetOutPath $SMPROGRAMS\$StartMenuGroup
    CreateShortcut "$SMPROGRAMS\$StartMenuGroup\Uninstall $(^Name).lnk" $INSTDIR\jram_uninstall.exe
    WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" DisplayName "$(^Name)"
    WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" DisplayVersion "${VERSION}"
    WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" Publisher "${COMPANY}"
    WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" URLInfoAbout "${URL}"
    WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" DisplayIcon $INSTDIR\jram_uninstall.exe
    WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" UninstallString $INSTDIR\jram_uninstall.exe
    WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" NoModify 1
    WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" NoRepair 1
SectionEnd

# Macro for selecting uninstaller sections
!macro SELECT_UNSECTION SECTION_NAME UNSECTION_ID
    Push $R0
    ReadRegStr $R0 HKLM "${REGKEY}\Components" "${SECTION_NAME}"
    StrCmp $R0 1 0 next${UNSECTION_ID}
    !insertmacro SelectSection "${UNSECTION_ID}"
    GoTo done${UNSECTION_ID}
next${UNSECTION_ID}:
    !insertmacro UnselectSection "${UNSECTION_ID}"
done${UNSECTION_ID}:
    Pop $R0
!macroend

# Uninstaller sections
Section /o un.Main UNSEC0000
    Delete /REBOOTOK "$SMPROGRAMS\jRAM\readme.lnk"
    Delete /REBOOTOK "$SMPROGRAMS\jRAM\license.lnk"
    Delete /REBOOTOK "$SMPROGRAMS\jRAM\events log.lnk"
    Delete /REBOOTOK "$SMPROGRAMS\jRAM\error log.lnk"
    Delete /REBOOTOK $QUICKLAUNCH\jRAM.lnk
    Delete /REBOOTOK $SMPROGRAMS\jRAM\jRAM.lnk
    RmDir /r /REBOOTOK $INSTDIR\help
    Delete /REBOOTOK $INSTDIR\license.txt
    Delete /REBOOTOK $INSTDIR\jram.jar
    Delete /REBOOTOK $INSTDIR\jram.exe
    Delete /REBOOTOK $INSTDIR\readme.txt
    Delete /REBOOTOK $INSTDIR\err.log
    Delete /REBOOTOK $INSTDIR\events.log
    Delete /REBOOTOK "$INSTDIR\jram default settings.prop"
    Delete /REBOOTOK "$INSTDIR\project list.jram"
    DeleteRegValue HKLM "${REGKEY}\Components" Main
SectionEnd

Section un.post UNSEC0001
    DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)"
    Delete /REBOOTOK "$SMPROGRAMS\$StartMenuGroup\Uninstall $(^Name).lnk"
    Delete /REBOOTOK $INSTDIR\jram_uninstall.exe
    DeleteRegValue HKLM "${REGKEY}" StartMenuGroup
    DeleteRegValue HKLM "${REGKEY}" Path
    DeleteRegValue HKLM "${REGKEY}" Version
    DeleteRegKey /IfEmpty HKLM "${REGKEY}\Components"
    DeleteRegKey /IfEmpty HKLM "${REGKEY}"
    RmDir /REBOOTOK $SMPROGRAMS\$StartMenuGroup
    RmDir /REBOOTOK $INSTDIR
SectionEnd

# Installer functions
Function StartMenuGroupSelect
    Push $R1
    StartMenu::Select /autoadd /text "Select the Start Menu folder in which you would like to create the program's shortcuts:" /lastused $StartMenuGroup jRAM
    Pop $R1
    StrCmp $R1 success success
    StrCmp $R1 cancel done
    MessageBox MB_OK $R1
    Goto done
success:
    Pop $StartMenuGroup
done:
    Pop $R1
FunctionEnd

Function .onGUIInit
    InitPluginsDir
    call checkForJRE
    call checkjRAMVersion
FunctionEnd

# Uninstaller functions
Function un.onInit
    ReadRegStr $INSTDIR HKLM "${REGKEY}" Path
    ReadRegStr $StartMenuGroup HKLM "${REGKEY}" StartMenuGroup
    !insertmacro SELECT_UNSECTION Main ${UNSEC0000}
FunctionEnd

#abort install if no JRE is present
Function checkForJRE
    Push $R0
    Push $R1
 
    ;look for JRE bundled with app
    ClearErrors
    StrCpy $R0 "$EXEDIR\jre\bin\javaw.exe"
    IfFileExists $R0 jreOK
    StrCpy $R0 ""
 
    ;look in directory pointed to by java_home env variable
    ClearErrors
    ReadEnvStr $R0 "JAVA_HOME"
    StrCpy $R0 "$R0\bin\javaw.exe"
    IfErrors checkRegistry
    IfFileExists $R0 JreFound
 
  checkRegistry:
    ClearErrors
    ReadRegStr $R1 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" "CurrentVersion"
    ReadRegStr $R0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment\$R1" "JavaHome"
    StrCpy $R0 "$R0\bin\javaw.exe"
    IfErrors checkLocal
    IfFileExists $R0 JreFound
  
  checkLocal:
    StrCpy $R0 "javaw.exe"
    IfFileExists $R0 JreFound
  
    MessageBox MB_OK|MB_ICONSTOP "jRAM requires a Java Runtime Environment (JRE) Version 1.4 or later. A JRE\
                     was not found on this system. You may download a free JRE from\
                     http://java.sun.com or download the jRAM setup file that is bundled\
                     with a JRE (http://midnightjava.net/jram/jram_setup_jre.exe).\
                     The installation will now be aborted"
    abort
        
  JreFound:
    ;verify jre version is 1.4 or later
    ReadRegStr $R1 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" "CurrentVersion"
    strCpy $R1 $R1 1 2
    intCmp $R1 4 jreOK 0 jreOK
    MessageBox MB_OK|MB_ICONQUESTION "jRAM requires a Java Runtime Environment (JRE) Version 1.4 or later.\
                          The installer detected an installed JRE earlier than version 1.4\
                          You may download a free JRE from http://java.sun.com or download\
                          the jRAM setup file that is bundled with a JRE\
                          (http://midnightjava.net/jram/jram_setup_jre.exe).\
                          The installation will now be aborted"
    abort
  jreOK:
    Pop $R1
    Pop $R0
FunctionEnd

Function checkjRAMVersion

    Push $R0
    
    ReadRegStr $R0 HKLM "${REGKEY}" Version
    ifErrors nojRAMRunning
    StrCmp $R0 ${VERSION} 0 nojRAMRunning
    MessageBox MB_OK|MB_ICONSTOP "According to the Windows Registry, Version ${VERSION} of jRAM \
                                  is already installed on this machine.$\n\
                                  Please un-install the existing instance before proceeding \
                                  with this installation."
    abort
    
  nojRAMRunning:
    Pop $R0
    return

FunctionEnd

