;  Include 32/64 bit detection library
;--------------------------------
!include "winver.nsh"


;  Name constants for describing the app
;--------------------------------
!define AppName "Vermilion Desktop Composer"
!define AppVersion "0.10"
!define ShortName "Vermilion"
!define Vendor "IC Solutions"


;  Java path finding variables
;--------------------------------
!define JRE_VERSION "1.6.X"
var JREFound


;  Basic installer details setup
;--------------------------------
Name "${AppName}"
OutFile "vermilion setup.exe"
InstallDir "$PROGRAMFILES\${ShortName}"
RequestExecutionLevel admin


;  Pages
;--------------------------------
Page directory
Page custom CheckInstalledJRE
Page instfiles


; The stuff to install
;--------------------------------
Section ""

  StrCmp $JREFound "0" DoNotInstall 0
  StrCmp $JREFound "-1" DoNotInstall 0

  SetOutPath $INSTDIR
  File "defaultprefs.ufp"

  SetOutPath $INSTDIR\vermilion
  File "build\classes\vermilion\Audio*"
  File "build\classes\vermilion\Event*"
  File "build\classes\vermilion\Main*"
  File "build\classes\vermilion\PluginClass*"
  File "build\classes\vermilion\Simple*"
  File "build\classes\vermilion\SystemPlugin*"
  File "build\classes\vermilion\UsefulFile*"

  SetOutPath $INSTDIR\libraries
  File "libraries\*.jar"

  SetOutPath $INSTDIR\help
  File "help\*"

  SetOutPath $INSTDIR\songs
  File "songs\*.ufm"

  SetOutPath $INSTDIR\plugins
  File "plugins\*.jar"

  SetOutPath $INSTDIR\plugins\vermilion
  File "plugins\vermilion\*.class"

  WriteRegStr HKLM "SOFTWARE\${Vendor}\${ShortName}" "" $INSTDIR
 
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ShortName}" "DisplayName" "${AppName}"
  WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ShortName}" "UninstallString" '"$INSTDIR\uninstall.exe"'
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ShortName}" "NoModify" "1"
  WriteRegDWORD HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ShortName}" "NoRepair" "1"
 
  ; So the 'start in' directory is correct on the shortcuts
  SetOutPath $INSTDIR

  SetShellVarContext all
  CreateDirectory "$SMPROGRAMS\${AppName}"
  CreateShortCut "$SMPROGRAMS\${AppName}\Uninstall.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0
  CreateShortCut "$SMPROGRAMS\${AppName}\${ShortName}.lnk" "$JREFound" "-cp .;libraries\RelativeLayout.jar;libraries\JSSPF.jar vermilion.Main" "" 0
  
  WriteUninstaller "$INSTDIR\uninstall.exe"
  Return

DoNotInstall:
  MessageBox MB_OK "Sorry it didn't work. Try installing Java manually before installing the application."

SectionEnd


;  Uninstaller section
;--------------------------------
Section "Uninstall"

  SetShellVarContext all
 
  ; remove registry keys
  DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\${ShortName}"
  DeleteRegKey HKLM  "SOFTWARE\${Vendor}\${AppName}"
  ; remove shortcuts, if any.
  Delete "$SMPROGRAMS\${AppName}\*.*"
  RMDir "$SMPROGRAMS\${AppName}"
  ; remove files
  RMDir /r "$INSTDIR"
 
SectionEnd














;  'Page' to detect JRE and install if not found, from NSIS Wiki
;----------------------------------------------------------------
Function CheckInstalledJRE

  Push "${JRE_VERSION}"
  Call DetectJRE
  Pop $0
  StrCpy $JREFound $0

  StrCmp $0 "0" NoFound
  StrCmp $0 "-1" FoundOld
  Goto JREAlreadyInstalled
 
FoundOld:
  MessageBox MB_OK "Java was found on your computer, but it is too old. Newer Java will now install."
  Goto MustInstallJRE
 
NoFound:
  MessageBox MB_OK "Java was not found on your computer. Java will now install."
  Goto MustInstallJRE
 
MustInstallJRE:
  GetVersion::WindowsPlatformArchitecture
  Pop $R0
  MessageBox MB_OK "Detected that you are running $R0 bit Windows, will install Java to match."

  StrCmp $R0 "32" Install32 0
  StrCmp $R0 "64" Install64 0
  Goto InstallVerif

Install64:
  File /oname=$TEMP\jre_setup.exe "installer resources\jre-6u20-64.exe"
  Goto StartInstall

Install32:
  File /oname=$TEMP\jre_setup.exe "installer resources\jre-6u20-32.exe"
  Goto StartInstall

StartInstall:
  DetailPrint "Launching JRE setup"
;  ExecWait '"$TEMP\jre_setup.exe" /s /v\"/qn REBOOT=Suppress JAVAUPDATE=0 WEBSTARTICON=0\"' $0
  ExecWait '"$TEMP\jre_setup.exe" REBOOT=Suppress' $0
  DetailPrint "Setup finished"
  Delete "$TEMP\jre_setup.exe"
  StrCmp $0 "0" InstallVerif 0
  Goto ExitInstallJRE
 
InstallVerif:
  DetailPrint "Checking the JRE Setup's outcome"
  Push "${JRE_VERSION}"
  Call DetectJRE  
  Pop $0
  StrCpy $JREFound $0

  StrCmp $0 "0" ExitInstallJRE 0
  StrCmp $0 "-1" ExitInstallJRE 0
  Goto JavaExeVerif
 
JavaExeVerif:
  IfFileExists $0 JREAlreadyInstalled 0
  Goto ExitInstallJRE

ExitInstallJRE:
  MessageBox MB_OK "Installing Java failed, the installation will now abort."
  Abort
  Return
 
JREAlreadyInstalled:
  Return
 
FunctionEnd


 
;  Function for finding the current JRE version, from NSIS Wiki
; Returns: 0 - JRE not found, -1 - JRE found but too old,
; Otherwise - Path to JAVA EXE DetectJRE.
;----------------------------------------------------------------
Function DetectJRE
  Exch $0	; Get version requested  
		; Now the previous value of $0 is on the stack, and the asked for version of JDK is in $0
  Push $1	; $1 = Java version string (ie 1.5.0)
  Push $2	; $2 = Javahome
  Push $3	; $3 and $4 are used for checking the major/minor version of java
  Push $4
  ; MessageBox MB_OK "Detecting JRE"
  ReadRegStr $1 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" "CurrentVersion"
  ; MessageBox MB_OK "Read : $1"
  StrCmp $1 "" DetectTry2
  ReadRegStr $2 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment\$1" "JavaHome"
  ; MessageBox MB_OK "Read 3: $2"
  StrCmp $2 "" DetectTry2
  Goto GetJRE
 
DetectTry2:
  ReadRegStr $1 HKLM "SOFTWARE\JavaSoft\Java Development Kit" "CurrentVersion"
  ; MessageBox MB_OK "Detect Read : $1"
  StrCmp $1 "" NoFound
  ReadRegStr $2 HKLM "SOFTWARE\JavaSoft\Java Development Kit\$1" "JavaHome"
  ; MessageBox MB_OK "Detect Read 3: $2"
  StrCmp $2 "" NoFound
 
GetJRE:
; $0 = version requested. $1 = version found. $2 = javaHome
  IfFileExists "$2\bin\javaw.exe" 0 NoFound
  StrCpy $3 $0 1			; Get major version. Example: $1 = 1.5.0, now $3 = 1
  StrCpy $4 $1 1			; $3 = major version requested, $4 = major version found
;  MessageBox MB_OK "Want $3 , found $4"
  IntCmp $4 $3 0 FoundOld FoundNew
  StrCpy $3 $0 1 2
  StrCpy $4 $1 1 2			; Same as above. $3 is minor version requested, $4 is minor version installed
;  MessageBox MB_OK "Want $3 , found $4" 
  IntCmp $4 $3 FoundNew FoundOld FoundNew
;  StrCpy $3 $0 2 4
;  StrCpy $4 $1 2 4			; Same as above. $3 is release requested, $4 is release installed
;  MessageBox MB_OK "Want $3 , found $4" 
;  IntCmp $4 $3 FoundNew FoundOld FoundNew
 
NoFound:
  Push "0"
  Goto DetectJREEnd
 
FoundOld:
  Push "-1"
  Goto DetectJREEnd  

FoundNew:
  Push "$2\bin\javaw.exe"
  Goto DetectJREEnd

DetectJREEnd:
  ; Top of stack is return value, then r4,r3,r2,r1
  Exch   ; => r4,rv,r3,r2,r1,r0
  Pop $4 ; => rv,r3,r2,r1r,r0
  Exch   ; => r3,rv,r2,r1,r0
  Pop $3 ; => rv,r2,r1,r0
  Exch   ; => r2,rv,r1,r0
  Pop $2 ; => rv,r1,r0
  Exch   ; => r1,rv,r0
  Pop $1 ; => rv,r0
  Exch   ; => r0,rv
  Pop $0 ; => rv 
FunctionEnd
