]> git.lyx.org Git - features.git/blob - development/Win32/packaging/installer/include/LyXUtils.nsh
installer: merge LyXWinInstaller with the new one, see my mail
[features.git] / development / Win32 / packaging / installer / include / LyXUtils.nsh
1 # This script contains the following functions:
2 #
3 # - LaTeXCheck (check installed LaTeX-system),
4 #    (only used by Small and Complete installer), uses:
5 #    StrPointer
6 #    StrPoint
7 #    RevStrPointer
8 #    RevStrPoint
9 #
10 # - un.DelAppPathSub and UnAppPreSuff,
11 #    (delete the folder ~\Documents and Settings\username\Application Data\LyX for all users), uses:
12 #    un.GetParentA
13 #    un.GetUsers
14 #    un.StrPoint
15 #    StrPointer
16 #    StrPoint
17 #    UnAppPreSuff
18 #
19 # - CheckAppPathPreferences (replaces the old install folder name with the new one in the preferences files)
20 #                           (used by Update installer), uses:
21 #    ReplaceLineContent
22 #
23 # - IsUserAdmin (checks if user is admin)
24 #
25 # - FileCheck (checks if a given file exists)
26 #
27 #--------------------------
28
29 !macro StrPointer FindStr SearchStr Pointer
30  # searches for a string/character (SearchStr) in another string (FindStr)
31  # and returns the number of the character in the FindStr where the SearchStr was found (Pointer)
32  # if nothing was found or the search is impossible the Pointer is set to -1
33  
34  StrLen $R2 ${SearchStr}
35  StrLen $R4 ${FindStr}
36  StrCpy $R5 0
37  ${if} $R2 == 0
38  ${orif} $R4 == 0
39   Goto NotFound
40  ${endif}
41  IntCmp $R4 $R2 loopA NotFound
42  loopA:
43   StrCpy $R3 ${FindStr} $R2 $R5
44   StrCmp $R3 ${SearchStr} Found
45   IntOp $R5 $R5 + 1
46   IntCmp $R4 $R5 loopA NotFound
47   Goto loopA
48  Found:
49   StrCpy ${Pointer} $R5
50   Goto done
51  NotFound:
52   StrCpy ${Pointer} "-1"
53  done:
54
55 !macroend
56  
57 #--------------------------------
58
59 Function StrPoint
60  !insertmacro StrPointer $String $Search $Pointer
61 FunctionEnd
62
63 #--------------------------------
64
65 !macro RevStrPointer FindStr SearchStr Pointer
66  # searches for a string/character (SearchStr) in another string (FindStr) in reverse order
67  # and returns the number of the character in the FindStr where the SearchStr was found (Pointer)
68  # if nothing was found or the search is impossible the Pointer is set to +1
69  
70  StrLen $R2 ${SearchStr}
71  StrLen $R4 ${FindStr}
72  ${if} $R2 == 0
73  ${orif} $R4 == 0
74   Goto NotFound
75  ${endif}
76  IntCmp $R4 $R2 loopA NotFound
77  StrCpy $R5 "-$R2"
78  loopA:
79   StrCpy $R3 ${FindStr} $R2 $R5
80   StrCmp $R3 ${SearchStr} Found
81   IntOp $R5 $R5 - 1
82   IntCmp "$R5" "-$R4" loopA NotFound
83   Goto loopA
84  Found:
85   StrCpy ${Pointer} $R5
86   Goto done
87  NotFound:
88   StrCpy ${Pointer} "+1"
89  done:
90
91 !macroend
92  
93 #--------------------------------
94
95  Function RevStrPoint
96   !insertmacro RevStrPointer $String $Search $Pointer
97  FunctionEnd
98
99 #--------------------------------
100
101 !macro AppPreSuff AppPre AppSuff
102  # the APPDATA path for a local user has for WinXP and 2000 the following structure:
103  # C:\Documents and Settings\username\Application Data
104  # for Win Vista the structure is:
105  # C:\Users\username\AppData\Roaming
106  # this macro saves the "C:\Documents and Settings\" substring into the variable "AppPre"
107  # and the "Application Data" substring into the variable "AppSuff"
108   
109   # switch temporarily to local user because the all users application data path is in
110   # Vista only C:\ProgramData 
111   SetShellVarContext current
112   StrCpy $String "$APPDATA"
113   Var /GLOBAL APPDATemp
114   StrCpy $APPDATemp "$APPDATA"
115   ${If} $MultiUser.Privileges == "Admin"
116    ${OrIf} $MultiUser.Privileges == "Power"
117     SetShellVarContext all # move back to all users
118   ${endif}
119   StrCpy $Search "\"
120   Call StrPoint # search for the first "\"
121   IntOp $Pointer $Pointer + 1 # jump after the "\"
122   StrCpy $String $String "" $Pointer # cut off the part before the first "\"
123   StrCpy $0 $Pointer
124   Call StrPoint # search for the second "\"
125   IntOp $0 $0 + $Pointer # $0 is now the pointer to the second "\" in the APPDATA string
126   StrCpy ${AppPre} $APPDATemp $0 # save the part before the second "\"
127   IntOp $Pointer $Pointer + 1 # jump after the "\"
128   StrCpy $String $String "" $Pointer # cut off the part before the second "\"
129   Call StrPoint # search for the third "\"
130   IntOp $Pointer $Pointer + 1 # jump after the "\"
131   StrCpy ${AppSuff} $String "" $Pointer # save the part after the third "\"
132
133 !macroend
134
135 #--------------------------------
136
137 Function GetParentA
138  # deletes a subfolder of the APPDATA path for all users
139  # used by the function "un.getUsers"
140
141   Exch $R0
142   Push $R1
143   Push $R2
144   Push $R3
145   StrCpy $R1 0
146   StrLen $R2 $R0
147   loop:
148    IntOp $R1 $R1 + 1
149    IntCmp $R1 $R2 get 0 get
150    StrCpy $R3 $R0 1 -$R1
151    StrCmp $R3 "\" get
152   Goto loop
153   get:
154    StrCpy $R0 $R0 -$R1
155    Pop $R3
156    Pop $R2
157    Pop $R1
158    Exch $R0
159    
160 FunctionEnd
161
162 #--------------------------------
163
164 Function GetUsers
165  # reads the subfolders of the "Documents and Settings" folder to get a list of the users
166
167   StrCpy $R3 ""
168   Push "$PROFILE"
169   Call GetParentA  
170   Pop $R2
171   StrCpy $R2 "$R2"
172   FindFirst $R0 $R1 "$R2\*"
173   StrCmp $R1 "" findend 0
174   findloop:
175    IfFileExists "$R2\$R1\*.*" 0 notDir
176    StrCmp $R1 "." notDir
177    StrCmp $R1 ".." notDir
178    StrCmp $R1 "All Users" notDir
179    StrCmp $R1 "Default User" notDir
180    StrCmp $R1 "All Users.WINNT" notDir
181    StrCmp $R1 "Default User.WINNT" notDir  
182   StrCpy $R3 "$R3|$R1"
183   notDir:
184    FindNext $R0 $R1
185    StrCmp $R1 "" findend 0
186   Goto findloop
187   findend:
188    FindClose $R0
189   
190 FunctionEnd
191
192 #--------------------------------
193
194 Function un.GetParentA
195  # deletes a subfolder of the APPDATA path for all users
196  # used by the function "un.getUsers"
197
198   Exch $R0
199   Push $R1
200   Push $R2
201   Push $R3
202   StrCpy $R1 0
203   StrLen $R2 $R0
204   loop:
205    IntOp $R1 $R1 + 1
206    IntCmp $R1 $R2 get 0 get
207    StrCpy $R3 $R0 1 -$R1
208    StrCmp $R3 "\" get
209   Goto loop
210   get:
211    StrCpy $R0 $R0 -$R1
212    Pop $R3
213    Pop $R2
214    Pop $R1
215    Exch $R0
216    
217 FunctionEnd
218
219 #--------------------------------
220
221 Function un.GetUsers
222  # reads the subfolders of the "Documents and Settings" folder to get a list of the users
223
224   StrCpy $R3 ""
225   Push "$PROFILE"
226   Call un.GetParentA
227   Pop $R2
228   StrCpy $R2 "$R2"
229   FindFirst $R0 $R1 "$R2\*"
230   StrCmp $R1 "" findend 0
231   findloop:
232    IfFileExists "$R2\$R1\*.*" 0 notDir
233    StrCmp $R1 "." notDir
234    StrCmp $R1 ".." notDir
235    StrCmp $R1 "All Users" notDir
236    StrCmp $R1 "Default User" notDir
237    StrCmp $R1 "All Users.WINNT" notDir
238    StrCmp $R1 "Default User.WINNT" notDir  
239   StrCpy $R3 "$R3|$R1"
240   notDir:
241    FindNext $R0 $R1
242    StrCmp $R1 "" findend 0
243   Goto findloop
244   findend:
245    FindClose $R0
246   
247 FunctionEnd
248
249 #--------------------------------
250
251 Function un.StrPoint
252  !insertmacro StrPointer $String $Search $Pointer
253 FunctionEnd
254
255 #--------------------------------
256
257 !macro UnAppPreSuff AppPre AppSuff
258  # the APPDATA path for a local user has for WinXP and 2000 the following structure:
259  # C:\Documents and Settings\username\Application Data
260  # for Win Vista the structure is:
261  # C:\Users\username\AppData\Roaming
262  # this macro saves the "C:\Documents and Settings\" substring into the variable "AppPre"
263  # and the "Application Data" substring into the variable "AppSuff"
264   
265   SetShellVarContext current # switch temoprarily to local user
266   StrCpy $String "$APPDATA"
267   StrCpy $APPDATemp "$APPDATA"
268   ${if} $MultiUser.Privileges == "Admin"
269   ${orif} $MultiUser.Privileges == "Power"
270    SetShellVarContext all # move back to all users
271   ${endif}
272   StrCpy $Search "\"
273   Call un.StrPoint # search for the first "\"
274   IntOp $Pointer $Pointer + 1 # jump after the "\"
275   StrCpy $String $String "" $Pointer # cut off the part before the first "\"
276   StrCpy $0 $Pointer
277   Call un.StrPoint # search for the second "\"
278   IntOp $0 $0 + $Pointer # $0 is now the pointer to the second "\" in the APPDATA string
279   StrCpy ${AppPre} $APPDATemp $0 # save the part before the second "\"
280   IntOp $Pointer $Pointer + 1 # jump after the "\"
281   StrCpy $String $String "" $Pointer # cut off the part before the second "\"
282   Call un.StrPoint # search for the third "\"
283   IntOp $Pointer $Pointer + 1 # jump after the "\"
284   StrCpy ${AppSuff} $String "" $Pointer # save the part after the third "\"
285
286 !macroend
287
288 #--------------------------------
289
290 Function un.DelAppPathSub
291  # deletes a subfolder of the APPDATA path for all users
292
293   # get list of all users
294   Push $R0
295   Push $R1
296   Push $R2
297   Push $R3
298   Call un.GetUsers
299   StrCpy $UserList $R3 "" 1 # cut off the "|" at the end of the list
300   Pop $R3
301   Pop $R2
302   Pop $R1
303   Pop $R0
304   
305   # the usernames in the list of all users is separated by "|"
306   loop:
307    StrCpy $String "$UserList"
308    StrCpy $Search "|"
309    Call un.StrPoint # search for the "|"
310    StrCmp $Pointer "-1" ready
311    StrCpy $0 $UserList $Pointer # $0 contains now the username
312    IntOp $Pointer $Pointer + 1 # jump after the "|"
313    StrCpy $UserList $UserList "" $Pointer # cut off the first username in the list
314    # generate the string for the current user
315    # AppPre and AppSuff are generated in the macro "AppPreSuff"
316    RMDir /r "$AppPre\$0\$AppSuff\$AppSubfolder" # delete the folder
317   Goto loop
318   ready:
319   StrCpy $0 $UserList
320   RMDir /r "$AppPre\$0\$AppSuff\$AppSubfolder" # delete the folder
321   
322 FunctionEnd
323
324 #--------------------------------
325
326 !macro FileCheck Result FileName FilePath
327  # checks if a file exists, returns "True" or "False"
328
329  Push $0
330  Push $1
331  StrCpy $0 ""
332  StrCpy $1 ""
333  FileOpen $0 "${Filepath}\${FileName}" r
334  ${if} $0 = ""
335   StrCpy $1 "False"
336  ${Else}
337   StrCpy $1 "True"
338  ${endif}
339  FileClose $0
340  StrCpy ${Result} $1
341  Pop $1
342  Pop $0
343
344 !macroend
345
346 #------------------------------------------
347
348 Function LaTeXCheck
349  # searches the string "$Search" in the string "$String" and extracts the path around it
350  # the extracted path is checked if the file "latex.exe" is in it
351
352    StartCheck:
353    StrLen $3 $String
354    Call StrPoint
355    ${if} $Pointer == "-1" # if nothing was found
356     StrCpy $PathLaTeX ""
357     Return
358    ${endif}
359    IntOp $3 $3 - $Pointer
360    StrCpy $4 $String $3 "-$3" # $4 is now the part behind the $Search string
361    StrCpy $String $String $Pointer # $String is now the part before the $Search string
362    StrCpy $Search ":" # search for the ":" after the first previous drive letter
363    Call RevStrPoint
364    IntOp $Pointer $Pointer - 1 # jump before the ":" to the drive letter
365    StrCpy $Pointer $Pointer "" 1 # cut of the "-" sign
366    StrCpy $PathLaTeX $String $Pointer "-$Pointer"
367    StrCpy $String $4
368    StrCpy $Search ";" # search for the following ";" that separates the different paths
369    Call StrPoint
370    ${if} $Pointer != "-1" # if something was found
371     StrCpy $String $String $Pointer
372    ${endif}
373    StrCpy $PathLaTeX "$PathLaTeX$String"
374    # check if the latex.exe exists in the $PathLaTeX folder
375    !insertmacro FileCheck $5 "latex.exe" "$PathLaTeX"
376    ${if} $5 == "False" # delete the entry with the wrong path to the latex.exe and try again
377     StrCpy $PathLaTeX ""
378     StrLen $3 $String
379     StrCpy $String $4 "" $3
380     ${if} $2 == "TeXLive"
381      StrCpy $Search "TeXLive"
382     ${else}
383      StrCpy $Search "miktex"
384     ${endif}
385     Goto StartCheck
386    ${endif}
387
388 FunctionEnd
389