;BreakAway Games
;Incident Commander Install Script
;Written by Frank Vivirito originally for NetSTRIKE, hacked for MidEast by Mark Seibert, modified again for Incident Commander by Steve Weaver

;--------------------------------
;Include Modern UI

!include "MUI.nsh"

;--------------------------------

;---- Installer Location----
!define Installer_Location "C:\Projects\Incident Commander\Installer 1.5"

;Interface Configuration

!define MUI_HEADERIMAGE
!define MUI_HEADERIMAGE_BITMAP "${Installer_Location}\IC_splash.bmp"
!define MUI_ABORTWARNING
!define MUI_ICON "${Installer_Location}\Setup.ico"
!define MUI_UNICON "${Installer_Location}\uninstall.ico"

;--------------------------------
;Declarations

!define PRODUCT_NAME "Incident Commander"

!define PRODUCT_VERSION "V 1.503"  ; PRODUCT VERSION GOES HERE - UPDATE THIS NUMBER appropriately WHEN MAKING A NEW INSTALLER.exe!!!!

!define PRODUCT_PUBLISHER "BreakAway ltd"
!define PRODUCT_WEB_SITE "http://www.incidentcommander.net"
!define PRODUCT_DIR_REGKEY "Software\Microsoft\Windows\CurrentVersion\App Paths\IC.exe"
!define PRODUCT_UNINST_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\${PRODUCT_NAME}"
!define PRODUCT_UNINST_ROOT_KEY "HKLM"

!define Product_Location "C:\Projects\Incident Commander\Game" ;; where we have the product for our development - the source
!define Documentation_Location "C:\Projects\Incident Commander\Game\User Manuals"

!define DirectX_Location "${Installer_Location}\DirectX 8.1b"
!define Common_Files_Location "C:\Program Files\Common Files"
!define MS_Self_Reg_DLLs_Location "C:\Projects\Incident Commander\Game\MS\Self Reg" ;; where the MS self registering DLLS are.



;--------------------------------
; Pages

!insertmacro MUI_PAGE_LICENSE "license.txt" ;; This puts up the license page
!insertmacro MUI_PAGE_COMPONENTS
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_UNPAGE_CONFIRM
!insertmacro MUI_UNPAGE_INSTFILES

;--------------------------------
;Languages
 
!insertmacro MUI_LANGUAGE "English"

;--------------------------------
;General

;; Name Sets the name of the installer. The name is usually simply the product name such as 'MyApp' or 'CrapSoft MyApp'.
Name "${PRODUCT_NAME} ${PRODUCT_VERSION}" ;;aka "Incident Commander V1.5"

;;Specifies the output file that the MakeNSIS should write the installer to. This is just the file that ;;contents of the installer.
OutFile "Installer.exe"

;;This attribute tells the installer to check a string in the registry, and use it for the install dir if
;;that string is valid. If this attribute
;;MakeNSIS writes, it doesn't affect the is present, it will override the InstallDir attribute if the registry key is valid,
;;otherwise it will fall back to the InstallDir default.
InstallDir "$PROGRAMFILES\Incident Commander"  ;; where the program gets installed to.
InstallDirRegKey HKLM "${PRODUCT_DIR_REGKEY}" ""

ShowInstDetails show
ShowUnInstDetails show

;--------------------------------
Section "Program" Section1

;code here that sets $ALREADY_INSTALLED to a non-zero value if the application is
 ;already installed. 

IfFileExists "$INSTDIR\IC.exe" 0 new_installation  ; fileexists
   ;; StrCpy $ALREADY_INSTALLED "1"
    MessageBox MB_YESNO "This will overwrite the existing Incident Commander Installation. Do you wish to continue?" IDYES old_installation
    Abort

  old_installation: ;; Get rid of the old files and directory structure and start fresh
                    ;; we won't bother with DLL's and shortcuts for now - I guess ideally we would run old uninstaller
                    ;; but I don't yet know how to sync up old so we don't have multiple threads
                    ;;  The answer is ExecWait - but don't know what to run to uninstall 1.0 plus it could be
                    ;; 1.5 instead - so this is really safeest for now. 8/11/06
                    
  Delete   "$INSTDIR\*.*"
; Remove directories
  RMDir /r "$INSTDIR"    ;; looks like it takes care of the sub dirs automatically with the /r switch
  
  new_installation:


;;Sets the output path ($OUTDIR) and creates it (recursively if necessary), if it does not exist.
;;Must be a full pathname, usually is just
;;$INSTDIR (you can specify $INSTDIR if you are lazy with a single "-").
 SetOutPath $INSTDIR

;; // since we bring over the Game Folder - don't need to explicitly copy these guys - but like to see the important ones listed
;; -- since script will fail if the fall explicitly called for is not found - that's a good thing.
;; // Need to make sure latest IC.exe - release version is put into the Game Folder!!!!
File "${Product_Location}\IC.exe"
File "${Product_Location}\IC_AreaMapEditor.exe"
File "${Product_Location}\IC_EventsEditor.exe"
File "${Product_Location}\IC_TacMapEditor.exe"
File "${Product_Location}\IC.ini"
File "${Product_Location}\Mss32.dll"
File "${Product_Location}\msvcp71.dll"
File "${Product_Location}\msvcr71.dll"
File "${Product_Location}\MFC71.dll"
File "${Product_Location}\zlibwapi.dll"

  SetOutPath $INSTDIR
;;Adds file(s) to be extracted to the current output path ($OUTDIR).
File /r "${Product_Location}\*.*"    ;;;  get our 'game' dir files   /r for recursive

;; clean these out in case our development game dir had leftovers - will always have empty.txt file to fool perforce
Delete "$INSTDIR\GameFromHost\*.*"
Delete "$INSTDIR\ScenarioFromHost\Maps\*.*"
Delete "$INSTDIR\ScenarioFromHost\*.*"

  SetOutPath "$INSTDIR\Additional Materials"
File  "${Installer_Location}\Additional Materials\*.*"


  SetOutPath "${MS_Self_Reg_DLLs_Location}"
;; Self registering Dlls for MS - other ones get picked up with File /r product_location above
File "${Product_Location}\MS\Self Reg\Mfc42.dll"
File "${Product_Location}\MS\Self Reg\OLEAUT32.dll"
File "${Product_Location}\MS\Self Reg\OLEPRO32.dll"

RegDLL  "${Product_Location}\MS\Self Reg\Mfc42.dll"
RegDLL  "${Product_Location}\MS\Self Reg\OLEAUT32.dll"
RegDLL  "${Product_Location}\MS\Self Reg\OLEPRO32.dll"

  SetOutPath $INSTDIR
CreateShortCut "$DESKTOP\Incident Commander.lnk" "$INSTDIR\IC.exe"

CreateDirectory "$SMPROGRAMS"
CreateDirectory "$SMPROGRAMS\Incident Commander"

 SetOutPath $INSTDIR
;; Keep these - this sets up the Program Files list - don't know how to nest these yet
;; Old was Scenario Tools - Area Map Editor, Events Editor, Tactical Map Editor
;; Old was Incident Commander Simulator
;; Old was Incident Commander User Manuals

CreateShortCut "$SMPROGRAMS\Incident Commander\Incident Commander.lnk" 	"$INSTDIR\IC.exe"
CreateShortCut "$SMPROGRAMS\Incident Commander\User Manual.lnk" 	"$INSTDIR\User Manuals\IC User Manual.pdf"
CreateShortCut "$SMPROGRAMS\Incident Commander\Area Map Editor.lnk" 	"$INSTDIR\IC_AreaMapEditor.exe"
CreateShortCut "$SMPROGRAMS\Incident Commander\Events Editor.lnk" 	"$INSTDIR\IC_EventsEditor.exe"
CreateShortCut "$SMPROGRAMS\Incident Commander\Tactical Map Editor.lnk" "$INSTDIR\IC_TacMapEditor.exe"

;;CreateShortCut "$SMPROGRAMS\Website.lnk" "$INSTDIR\${PRODUCT_NAME}.url"

SectionEnd

;--------------------------------
Section "Documentation" Section2

 SetOutPath "$INSTDIR\User Manuals"
File "${Documentation_Location}\IC Editors Instruction Manual.pdf"
File "${Documentation_Location}\IC User Manual.pdf"

SectionEnd

;--------------------------------
Section -DirectX_Install

 ;; setoutput path and
 SetOutPath "$INSTDIR\DirectX 8.1b\Win2K"
 ;; // when do I need '{''}' ????     ;; would be nice to remove THE INSTALL FOLDER FOR DIRECTX WHEN DONE WITH INSTALLATION - but ok to leave
File "${DirectX_Location}\Win2K\DX81NTeng.exe"   ;;

 SetOutPath "$INSTDIR\DirectX 8.1b\Win9X"
File "${DirectX_Location}\Win9X\DX81eng.exe"   ;;
; GetWindowsVersion
 ;
 ; Returns on top of stack
 ;
 ; Windows Version (95, 98, ME, NT x.x, 2000, XP, 2003)
 ; or '' (Unknown Windows Version)
 ;
 ; Usage:
 ;   Call GetWindowsVersion
 ;   Pop $R0
 ;   ; at this point $R0 is "NT 4.0" or whatnot

Call GetWindowsVersion 

 StrCmp $R0 '98' Win9X
 StrCmp $R0 '95' Win9X
 ;; fall thru to Win2K install if not Win 98

	exec '"$INSTDIR\DirectX 8.1b\Win2K\DX81NTeng.exe"'
 	Goto done_dx_install 
 
;; windows 98 version- use other version of direct x to install.

 Win9X:
	exec '"$INSTDIR\DirectX 8.1b\Win9X\DX81eng.exe"'

 done_dx_install:

SectionEnd 


;--------------------------------
Section -MapObjects_Install ;; // TBD --- NEED TO ADD THIS TO UNINSTALL ALSO - IF WE CAN REMOVE THEM SAFELY 

;; setoutput path -- these won't be put in INSTALLDIR - go to common files\ESRI location
 SetOutPath "${Common_Files_Location}\ESRI"

;; non self-reg dlls for mapobject
File  "${Product_Location}\ESRI\Sg.dll"
File  "${Product_Location}\ESRI\Pe.dll"
File  "${Product_Location}\ESRI\AFLT20.dll"
;; self-reg dlls for mapobject - need to add for registering - needs to load file before you can register it
File  "${Product_Location}\ESRI\Self Reg\ShapeLT20.dll"
File  "${Product_Location}\ESRI\Self Reg\MOLT20.ocx"

RegDLL "${Common_Files_Location}\ESRI\ShapeLT20.dll"
RegDLL "${Common_Files_Location}\ESRI\MOLT20.ocx"


SectionEnd


;--------------------------------
Section -Remove_Files

;; Writes the uninstaller to the filename (and optionally path) specified. Only valid from within an 
;;install section or function, and requires that you have an uninstall section in your script. 
;;See also Uninstall configuration. You can call this one or more times to write out one or more copies of the uninstaller.
  WriteUninstaller "$INSTDIR\Uninstaller.exe"

   WriteRegStr ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}" "UninstallString" "$INSTDIR\Uninstaller.exe" ;; Don't think I need this but ...

SectionEnd

;--------------------------------
Section -Last_Cut
 SetOutPath $INSTDIR
  CreateShortCut "$SMPROGRAMS\Incident Commander\Uninstall.lnk" "$INSTDIR\Uninstaller.exe"

SectionEnd

;--------------------------------
;Uninstall dialog

Function un.onUninstSuccess
  HideWindow
  MessageBox MB_ICONINFORMATION|MB_OK "Incident Commander was successfully removed from your computer."
FunctionEnd

Function un.onInit
  MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to completely remove Incident Commander and all of its components?" IDYES +2
  Abort
FunctionEnd

;--------------------------------
;Descriptions

  ;Language strings
LangString DESC_Section1 ${LANG_ENGLISH} "Installation of Incident Commander Program Files"
LangString DESC_Section2 ${LANG_ENGLISH} "Installation of Incident Commander documentation and user manuals"

;Assign language strings to sections

!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
  !insertmacro MUI_DESCRIPTION_TEXT ${Section1} $(DESC_Section1)
  !insertmacro MUI_DESCRIPTION_TEXT ${Section2} $(DESC_Section2)
!insertmacro MUI_FUNCTION_DESCRIPTION_END
 
;--------------------------------

Section Uninstall
  
; Remove files directories and shortcuts ;; the Delete $INSTDIR\*.* should kill it all but
; I like seeing the important things we load and get rid of

Delete "$INSTDIR\ErrorLog.html"
Delete "$INSTDIR\IC.exe"
Delete "$INSTDIR\IC.ini"
Delete "$INSTDIR\Mss32.dll"
Delete "$INSTDIR\msvcp71.dll"
Delete "$INSTDIR\msvcr71.dll"
Delete "$INSTDIR\zlibwapi.dll"

;; Delete MapObject LT files - installed in C:\Program Files\Common Files\ESRI 

;; // MAPOBJECT DLLS delete - issue UnRegDll for self-registering ones

UnRegDLL  "$INSTDIR\MS\Self Reg\Mfc42.dll"
UnRegDLL  "$INSTDIR\MS\Self Reg\OLEAUT32.dll"
UnRegDLL  "$INSTDIR\MS\Self Reg\OLEPRO32.dll"

;; Self registering Dlls for MS - other non-self reg dlls get picked up with Delete File /r .. INSTDIR below
Delete  "$INSTDIR\MS\Self Reg\Mfc42.dll"
Delete  "$INSTDIR\MS\Self Reg\OLEAUT32.dll"
Delete  "$INSTDIR\MS\Self Reg\OLEPRO32.dll"

;;UnRegDLL "${Common_Files_Location}\ESRI\ShapeLT20.dll" //LEAVING THESE ALL IN IN CASE SHARED with OTHER ESRI APPS
;;UnRegDLL "${Common_Files_Location}\ESRI\MOLT20.ocx"

;;Delete   "${Common_Files_Location}\ESRI\ShapeLT20.dll" ;; /REBOOTOK and Sleep lame attempt to allow loading of DLLs during UnRegDll
;;Delete   "${Common_Files_Location}\ESRI\MOLT20.ocx"    ;; but nothing seems to work - so will leave stuff in registry - should be OK

;;Delete  "${Common_Files_Location}\ESRI\Sg.dll"
;;Delete  "${Common_Files_Location}\ESRI\Pe.dll"
;;Delete  "${Common_Files_Location}\ESRI\AFLT20.dll"

;; want to do the DLL work before we issue the Delete call so DLLs still there to UnRegDLL on ..

Delete 	"$INSTDIR\*.*"

Delete  "$INSTDIR\Uninstaller.exe" ;; can be done right away according to help - event before other file deletes
			   ;;  - running copy of uninstaller is a temp version

; Remove directories
RMDir /r "$INSTDIR"    ;; looks like it takes care of the sub dirs automatically with the /r switch


; Remove shortcuts

Delete "$DESKTOP\Incident Commander.lnk"

Delete "$SMPROGRAMS\Incident Commander\Incident Commander.lnk" 
Delete "$SMPROGRAMS\Incident Commander\User Manual.lnk" 		
Delete "$SMPROGRAMS\Incident Commander\Area Map Editor.lnk" 
Delete "$SMPROGRAMS\Incident Commander\Events Editor.lnk" 
Delete "$SMPROGRAMS\Incident Commander\Tactical Map Editor.lnk" 	


Delete "$SMPROGRAMS\Incident Commander\Uninstall.lnk"
Delete "$SMPROGRAMS\Incident Commander"
; Delete "$SMPROGRAMS\BreakAwayLtd\Website.lnk" "


RMDir "$SMPROGRAMS\Incident Commander"
; Remove registry garbage
DeleteRegKey ${PRODUCT_UNINST_ROOT_KEY} "${PRODUCT_UNINST_KEY}"


; SetAutoClose true

SectionEnd



; GetWindowsVersion
 ;
 ; Based on Yazno's function, http://yazno.tripod.com/powerpimpit/
 ; Updated by Joost Verburg
 ;
 ; Returns on top of stack
 ;
 ; Windows Version (95, 98, ME, NT x.x, 2000, XP, 2003)
 ; or  '' (Unknown Windows Version)
 ;
 ; Usage:
 ;   Call GetWindowsVersion
 ;   Pop $R0
 ;   ; at this point $R0 is "NT 4.0" or whatnot
 
 Function GetWindowsVersion ;; used to see which OS is running so we can see which version of DirectX we should install.
 
   Push $R0
   Push $R1
 
   ClearErrors
 
   ReadRegStr $R0 HKLM \
   "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion

   IfErrors 0 lbl_winnt
   
   ; we are not NT
   ReadRegStr $R0 HKLM \
   "SOFTWARE\Microsoft\Windows\CurrentVersion" VersionNumber
 
   StrCpy $R1 $R0 1
   StrCmp $R1 '4' 0 lbl_error
 
   StrCpy $R1 $R0 3
 
   StrCmp $R1 '4.0' lbl_win32_95
   StrCmp $R1 '4.9' lbl_win32_ME lbl_win32_98
 
   lbl_win32_95:
     StrCpy $R0 '95'
   Goto lbl_done
 
   lbl_win32_98:
     StrCpy $R0 '98'
   Goto lbl_done
 
   lbl_win32_ME:
     StrCpy $R0 'ME'
   Goto lbl_done
 
   lbl_winnt:
 
   StrCpy $R1 $R0 1
 
   StrCmp $R1 '3' lbl_winnt_x
   StrCmp $R1 '4' lbl_winnt_x
 
   StrCpy $R1 $R0 3
 
   StrCmp $R1 '5.0' lbl_winnt_2000
   StrCmp $R1 '5.1' lbl_winnt_XP
   StrCmp $R1 '5.2' lbl_winnt_2003 lbl_error
 
   lbl_winnt_x:
     StrCpy $R0 "NT $R0" 6
   Goto lbl_done
 
   lbl_winnt_2000:
     Strcpy $R0 '2000'
   Goto lbl_done
 
   lbl_winnt_XP:
     Strcpy $R0 'XP'
   Goto lbl_done
 
   lbl_winnt_2003:
     Strcpy $R0 '2003'
   Goto lbl_done
 
   lbl_error:
     Strcpy $R0 ''
   lbl_done:
 
   Pop $R1
   Exch $R0
 
 FunctionEnd







