]> git.evergreen-ils.org Git - Hatch.git/blob - installer/windows/hatch.nsi
LP1860187: Set .properties Permissions Appropriately
[Hatch.git] / installer / windows / hatch.nsi
1 ;-----------------------------------------------------------------------
2 ; NSIS Installation Script                                              ;
3 ; Kyle Huckins                                                          ;
4 ; khuckins@catalystdevworks.com                                         ;
5 ; This will be a guide to working with NSIS to create an installer.     ;
6 ; It is a heavily commented practice installer, basically.              ;
7 ;                                                                       ;
8 ; Comments are designated by # and ;                                    ;
9 ; # tends to be at the beginning of a line, while ; is                  ;
10 ; primarilly used as end-of-line comments.  However it's                ;
11 ; common to use just ; for comments as well.                            ;
12 ;                                                                       ;
13 ;------------------------------------------------------------------------
14
15 ;==================
16 ; Basic Information
17 ;--------
18 ; Includes
19 !include "MUI2.nsh" ;This enables the use of ModernUI, and must go at the top
20 !include "x64.nsh"
21 !include "defines.nsh" ;Definitions for our variables
22 !include "LogicLib.nsh" ;Sparsly documented library, but necessary for any real logic
23 !include "nsDialogs.nsh" ;Need this to create custom pages
24 !include StrRep.nsh
25 !include ReplaceInFile.nsh
26 ;---------------------------------------------------------------
27
28 ; Add local copy of AccessControl plugin
29 !addplugindir .
30
31 ; Installer's filename
32 Outfile "${APPNAME}-Installer-${FULLVERSION}.exe"
33 RequestExecutionLevel admin
34
35
36 ;-----------------
37 ; Titlebar Content
38 Name "${APPNAME}"
39
40 ;==================================
41 ; Page system
42 !insertmacro MUI_PAGE_WELCOME
43 !define MUI_PAGE_CUSTOMFUNCTION_PRE ValidateInstall
44 !insertmacro MUI_PAGE_LICENSE "license.rtf" ;Loads licence.rtf to show license content
45 !insertmacro MUI_PAGE_DIRECTORY
46 !insertmacro MUI_PAGE_INSTFILES
47 !insertmacro MUI_PAGE_FINISH
48
49 !insertmacro MUI_LANGUAGE "English"
50
51 ;-------------------------------------
52 ; Code to verify if a user is an admin
53 !macro VerifyUserIsAdmin
54 UserInfo::GetAccountType
55 pop $0
56 ${If} $0 != "admin" ;Require admin rights
57     messageBox MB_OK|MB_ICONSTOP "Administrator rights required!"
58     setErrorLevel 740 ;ERROR_ELEVATION_REQUIRED
59     quit
60 ${EndIf}
61 !macroend
62
63 function .onInit
64     setShellVarContext all
65     !insertmacro VerifyUserIsAdmin
66     Return
67 functionEnd
68
69 ;-----------------
70 ;Check our version
71 function ValidateInstall
72     ; Hatch Version Check
73     ReadRegStr $1 HKLM "SOFTWARE\${COMPANYNAME}\${APPNAME}" "Version"
74     StrCmp $1 "" INSTALL
75     ${If} $1 S== ${FULLVERSION} ;Same version is installed
76         MessageBox MB_OKCANCEL|MB_ICONINFORMATION "You already have this version of ${APPNAME} installed.  You must uninstall the currently installed version to continue." /SD IDOK IDOK UNINSTALL IDCANCEL QUIT
77     ${ElseIf} $1 S> ${FULLVERSION} ;Older version is installed
78         MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION "You are tring to install an older version of ${APPNAME} than the one you currently have installed. You must uninstall the currently installed verion to continue." /SD IDOK IDOK UNINSTALL IDCANCEL QUIT
79     ${ElseIf} $1 S< ${FULLVERSION} ;Newer version is installed
80         MessageBox MB_OKCANCEL|MB_ICONINFORMATION "You have a previous version of ${APPNAME} installed. The previous version will be uninrtalled so installation can continue." /SD IDOK IDOK UNINSTALL IDCANCEL QUIT
81     ${EndIf}
82     UNINSTALL:
83         ReadRegStr $1 HKLM "SOFTWARE\${COMPANYNAME}\${APPNAME}" "Install Path"
84         ; Manually copying the uninstaller like this allows the use of the _? param which is required for ExecWait while also allowing all files to be deleted.
85         CopyFiles /SILENT "$1\Uninstall ${APPNAME}.exe" "$TEMP\Uninstall ${APPNAME}.exe"
86         ExecWait '"$TEMP\Uninstall ${APPNAME}.exe" /S _?=$1'
87         Delete /REBOOTOK "$TEMP\Uninstall ${APPNAME}.exe"
88         Goto INSTALL
89     QUIT:
90         Quit
91     INSTALL:
92         Return
93 functionEnd
94
95 ;--------------------------
96 ; Where our install files go
97 InstallDir "$PROGRAMFILES\${APPNAME}" ;This is where our variables come in.
98 ;----------------------------------------------
99
100 section "install"
101     ; Install directory files - keep these in the same directory
102     ; as the script before compiling.
103     SetOutPath $INSTDIR\ ;Sets output path to our InstallDir
104     File /r ..\..\java-jdk-win
105     File /r ..\..\javafx-sdk-win
106     File /r ..\..\lib
107     File /r ..\..\extension
108     File ..\..\hatch.bat
109     File ..\..\hatch.properties
110     File ..\..\logging.properties
111
112     ; Set path variable in org.ils_evergreen.hatch.*.json to $INSTDIR\hatch.bat
113     ${StrRep} '$0' '$INSTDIR' '\' '\\'
114     !insertmacro _ReplaceInFile  "$INSTDIR\extension\host\org.evergreen_ils.hatch.chrome.json" "/path/to/hatch.sh" "$0\\hatch.bat"
115     !insertmacro _ReplaceInFile  "$INSTDIR\extension\host\org.evergreen_ils.hatch.firefox.json" "/path/to/hatch.sh" "$0\\hatch.bat"
116     AccessControl::EnableFileInheritance "$INSTDIR\extension\host\org.evergreen_ils.hatch.chrome.json"
117     AccessControl::EnableFileInheritance "$INSTDIR\extension\host\org.evergreen_ils.hatch.firefox.json"
118
119     ; Set logging to be in ProgramData
120     SetShellVarContext all
121     ${IfNot} ${FileExists} "$APPDATA\${APPNAME}"
122         CreateDirectory "$APPDATA\${APPNAME}"
123     ${EndIf}
124     !insertmacro _ReplaceInFile  "$INSTDIR\hatch.properties" "#data.directory=/tmp/foo" "data.directory=C://ProgramData//${APPNAME}"
125     !insertmacro _ReplaceInFile  "$INSTDIR\logging.properties" "%h/.evergreen/hatch.log" "C://ProgramData//${APPNAME}//hatch.log"
126     AccessControl::EnableFileInheritance "$INSTDIR\hatch.properties"
127     AccessControl::EnableFileInheritance "$INSTDIR\logging.properties"
128
129     ; Registry info for Add/Remove Programs
130     WriteRegStr HKLM "SOFTWARE\${COMPANYNAME}\${APPNAME}" "Logging Path" $APPDATA
131     SetShellVarContext current
132     WriteRegStr HKLM "SOFTWARE\${COMPANYNAME}\${APPNAME}" "Install Path" $INSTDIR
133     WriteRegStr HKLM "SOFTWARE\${COMPANYNAME}\${APPNAME}" "Version" "${FULLVERSION}"
134     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "DisplayName" "${COMPANYNAME} - ${APPNAME} - ${DESCRIPTION}"
135     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "UninstallString" "$INSTDIR\Uninstall ${APPNAME}.exe"
136     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "QuietUninstallString" "$INSTDIR\Uninstall ${APPNAME}.exe /S"
137     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "InstallLocation" "$INSTDIR"
138     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "DisplayIcon" "$INSTDIR\logo.ico"
139     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "Publisher" "${COMPANYNAME}"
140     StrCmp "${HELPURL}" "" +2
141     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "HelpLink" "${HELPURL}"
142     StrCmp "${UPDATEURL}" "" +2
143     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "URLUpdateInfo" "${UPDATEURL}"
144     StrCmp "${ABOUTURL}" "" +2
145     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "URLInfoAbout" "${ABOUTURL}"
146     WriteRegStr HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "DisplayVersion" "${VERSIONMAJOR}.${VERSIONMINOR}.${VERSIONBUILD}"
147     WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "VersionMajor" ${VERSIONMAJOR}
148     WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "VersionMinor" ${VERSIONMINOR}
149     # There is no option for modifying or repairing the install
150     WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "NoModify" 1
151     WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "NoRepair" 1
152     # Set the INSTALLSIZE constant (!defined at the top of this script) so Add/Remove Programs can accurately report the size
153     WriteRegDWORD HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}" "EstimatedSize" ${INSTALLSIZE}
154
155     ; Connect Hatch to Chrome and install the Hatch extension from the Chrome Web Store
156     WriteRegStr HKLM "SOFTWARE\Google\Chrome\NativeMessagingHosts\org.evergreen_ils.hatch" "" "$INSTDIR\extension\host\org.evergreen_ils.hatch.chrome.json"
157     WriteRegStr HKLM "Software\Google\Chrome\Extensions\${EXTENSIONID}" "update_url" "${EXTENSION_UPDATEURL}"
158
159     ; Firefox won't check both the 32 and 64 bit views, so it's on us to put the key in the right place.
160     ; Firefox doesn't allow automatic installation of remote extensions either, so there's no (good) auto-install option here. :-/
161     ; A link should be added to the web client to simplify locating the FF extension. (Links to both would likely be a good practice.)
162     ${If} ${RunningX64}
163         SetRegView 64
164     ${EndIf}
165
166     WriteRegStr HKLM "SOFTWARE\Mozilla\NativeMessagingHosts\org.evergreen_ils.hatch" "" "$INSTDIR\extension\host\org.evergreen_ils.hatch.firefox.json"
167
168     ${If} ${RunningX64}
169         SetRegView 32
170     ${EndIf}
171
172     ; Uninstaller
173     writeUninstaller "$INSTDIR\Uninstall ${APPNAME}.exe"
174
175 SectionEnd
176
177
178 #############################
179 # Uninstaller code
180
181 function un.onInit
182     SetShellVarContext all
183
184     # Verify uninstaller
185     MessageBox MB_OKCANCEL "Permanently remove ${APPNAME}?" /SD IDOK IDOK next
186         Abort
187     next:
188     !insertmacro VerifyUserIsAdmin
189 functionEnd
190
191 section "uninstall"
192     # Remove the data log directory
193     SetShellVarContext all
194     ${If} ${FileExists} "$APPDATA\${APPNAME}"
195         RmDir /r /REBOOTOK "$APPDATA\${APPNAME}"
196     ${EndIf}
197     SetShellVarContext current
198
199     # Remove the actual files
200     Delete /REBOOTOK "$INSTDIR\hatch.bat"
201     Delete /REBOOTOK "$INSTDIR\hatch.properties"
202     Delete /REBOOTOK "$INSTDIR\logging.properties"
203     ; blindly using /r isn't ideal but the extreme unlikelyhood of there being \lib or \extension folders under $PROGRAMFILES makes it low risk.
204     RmDir /r /REBOOTOK "$INSTDIR\extension"
205     RmDir /r /REBOOTOK "$INSTDIR\lib"
206     RmDir /r /REBOOTOK "$INSTDIR\java-jdk-win"
207     RmDir /r /REBOOTOK "$INSTDIR\javafx-sdk-win"
208
209     # Delete uninstaller last
210     Delete /REBOOTOK "$INSTDIR\Uninstall ${APPNAME}.exe"
211
212     # Remove installation directory
213     RmDir /REBOOTOK "$INSTDIR"
214
215     # Remove uninstaller info from registry
216     DeleteRegKey HKLM "SOFTWARE\${COMPANYNAME}\${APPNAME}"
217     DeleteRegKey HKLM "SOFTWARE\${COMPANYNAME}"
218     DeleteRegKey HKLM "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\${APPNAME}"
219     DeleteRegKey HKLM "SOFTWARE\Google\Chrome\NativeMessagingHosts\org.evergreen_ils.hatch"
220     DeleteRegKey HKLM "SOFTWARE\Google\Chrome\Extensions\${EXTENSIONID}"
221
222     ${If} ${RunningX64}
223         SetRegView 64
224     ${EndIf}
225
226     DeleteRegKey HKLM "SOFTWARE\Mozilla\NativeMessagingHosts\org.evergreen_ils.hatch"
227
228     ${If} ${RunningX64}
229         SetRegView 32
230     ${EndIf}
231
232     IfRebootFlag 0 Done
233     MessageBox MB_YESNO "A reboot is required to finish the installation. Do you wish to reboot now?" /SD IDNO IDNO Done
234     Reboot
235
236     Done:
237
238 sectionEnd