]> git.lyx.org Git - lyx.git/blob - development/Win32/packaging/installer/LyXWinInstaller/LyXUtils.nsh
Changed comment character from ';' to '#*
[lyx.git] / development / Win32 / packaging / installer / LyXWinInstaller / LyXUtils.nsh
1 # This script contains the following functions:
2 #
3 # - LaTeXCheck (check installed LaTeX-system), uses:
4 #    StrPointer
5 #    StrPoint
6 #    RevStrPointer
7 #    RevStrPoint
8 #
9 # - un.DelAppPathSub and UnAppPreSuff, (delete the folder ~\Documents and Settings\username\Application Data\LyX for all users), uses:
10 #    un.GetParentA
11 #    un.GetUsers
12 #    un.StrPoint
13 #    StrPointer
14 #    StrPoint
15 #    UnAppPreSuff
16 #
17 # - CreateAppPathSub and AppPreSuff, (creates the folder ~\Documents and Settings\username\Application Data\LyX for all users), uses:
18 #    GetParentA
19 #    GetUsers
20 #    StrPointer
21 #    StrPoint
22 #    UnAppPreSuff
23 #
24 # - CheckAppPathPreferences (replaces the old install folder name with the new one in the preferences files)
25 #                           (used by Update installer), uses:
26 #    ReplaceLineContent
27 #
28 # - IsUserAdmin (checks if user is admin)
29 #
30 # - FileCheck (checks if a given file exists)
31 #
32 #--------------------------
33
34 !macro StrPointer FindStr SearchStr Pointer
35  # searches for a string/character (SearchStr) in another string (FindStr)
36  # and returns the number of the character in the FindStr where the SearchStr was found (Pointer)
37  # if nothing was found or the search is impossible the Pointer is set to -1
38  
39  StrLen $R2 ${SearchStr}
40  StrLen $R4 ${FindStr}
41  StrCpy $R5 0
42  ${if} $R2 == 0
43  ${orif} $R4 == 0
44   Goto NotFound
45  ${endif}
46  IntCmp $R4 $R2 loopA NotFound
47  loopA:
48   StrCpy $R3 ${FindStr} $R2 $R5
49   StrCmp $R3 ${SearchStr} Found
50   IntOp $R5 $R5 + 1
51   IntCmp $R4 $R5 loopA NotFound
52   Goto loopA
53  Found:
54   StrCpy ${Pointer} $R5
55   Goto done
56  NotFound:
57   StrCpy ${Pointer} "-1"
58  done:
59
60 !macroend
61  
62 #--------------------------------
63
64 Function StrPoint
65  !insertmacro StrPointer $String $Search $Pointer
66 FunctionEnd
67
68 #--------------------------------
69
70 !macro RevStrPointer FindStr SearchStr Pointer
71  # searches for a string/character (SearchStr) in another string (FindStr) in reverse order
72  # and returns the number of the character in the FindStr where the SearchStr was found (Pointer)
73  # if nothing was found or the search is impossible the Pointer is set to +1
74  
75  StrLen $R2 ${SearchStr}
76  StrLen $R4 ${FindStr}
77  ${if} $R2 == 0
78  ${orif} $R4 == 0
79   Goto NotFound
80  ${endif}
81  IntCmp $R4 $R2 loopA NotFound
82  StrCpy $R5 "-$R2"
83  loopA:
84   StrCpy $R3 ${FindStr} $R2 $R5
85   StrCmp $R3 ${SearchStr} Found
86   IntOp $R5 $R5 - 1
87   IntCmp "$R5" "-$R4" loopA NotFound
88   Goto loopA
89  Found:
90   StrCpy ${Pointer} $R5
91   Goto done
92  NotFound:
93   StrCpy ${Pointer} "+1"
94  done:
95
96 !macroend
97  
98 #--------------------------------
99
100 Function RevStrPoint
101  !insertmacro RevStrPointer $String $Search $Pointer
102 FunctionEnd
103
104 #--------------------------------
105
106 !macro AppPreSuff AppPre AppSuff
107  # the APPDATA path has always the following structure:
108  # C:\Documents and Settings\username\Application Data
109  # this macro saves the "C:\Documents and Settings\" substring into the variable "AppPre"
110  # and the "Application Data" substring into the variable "AppSuff"
111   
112   StrCpy $String "$APPDATA"
113   StrCpy $Search "\"
114   Call StrPoint # search for the first "\"
115   IntOp $Pointer $Pointer + 1 # jump after the "\"
116   StrCpy $String $String "" $Pointer # cut off the part before the first "\"
117   StrCpy $0 $Pointer
118   Call StrPoint # search for the second "\"
119   IntOp $0 $0 + $Pointer # $0 is now the pointer to the second "\" in the APPDATA string
120   StrCpy ${AppPre} $APPDATA $0 # save the part before the second "\"
121   IntOp $Pointer $Pointer + 1 # jump after the "\"
122   StrCpy $String $String "" $Pointer # cut off the part before the second "\"
123   Call StrPoint # search for the third "\"
124   IntOp $Pointer $Pointer + 1 # jump after the "\"
125   StrCpy ${AppSuff} $String "" $Pointer # save the part after the third "\"
126
127 !macroend
128
129 #--------------------------------
130
131 Function GetParentA
132  # deletes a subfolder of the APPDATA path for all users
133  # used by the function "un.getUsers"
134
135   Exch $R0
136   Push $R1
137   Push $R2
138   Push $R3
139   StrCpy $R1 0
140   StrLen $R2 $R0
141   loop:
142    IntOp $R1 $R1 + 1
143    IntCmp $R1 $R2 get 0 get
144    StrCpy $R3 $R0 1 -$R1
145    StrCmp $R3 "\" get
146   Goto loop
147   get:
148    StrCpy $R0 $R0 -$R1
149    Pop $R3
150    Pop $R2
151    Pop $R1
152    Exch $R0
153    
154 FunctionEnd
155
156 #--------------------------------
157
158 Function GetUsers
159  # reads the subfolders of the "Documents and Settings" folder to get a list of the users
160
161   StrCpy $R3 ""
162   Push "$PROFILE"
163   Call GetParentA  
164   Pop $R2
165   StrCpy $R2 "$R2"
166   FindFirst $R0 $R1 "$R2\*"
167   StrCmp $R1 "" findend 0
168   findloop:
169    IfFileExists "$R2\$R1\*.*" 0 notDir
170    StrCmp $R1 "." notDir
171    StrCmp $R1 ".." notDir
172    StrCmp $R1 "All Users" notDir
173    StrCmp $R1 "Default User" notDir
174    StrCmp $R1 "All Users.WINNT" notDir
175    StrCmp $R1 "Default User.WINNT" notDir  
176   StrCpy $R3 "$R3|$R1"
177   notDir:
178    FindNext $R0 $R1
179    StrCmp $R1 "" findend 0
180   Goto findloop
181   findend:
182    FindClose $R0
183   
184 FunctionEnd
185
186 #--------------------------------
187
188 Function un.GetParentA
189  # deletes a subfolder of the APPDATA path for all users
190  # used by the function "un.getUsers"
191
192   Exch $R0
193   Push $R1
194   Push $R2
195   Push $R3
196   StrCpy $R1 0
197   StrLen $R2 $R0
198   loop:
199    IntOp $R1 $R1 + 1
200    IntCmp $R1 $R2 get 0 get
201    StrCpy $R3 $R0 1 -$R1
202    StrCmp $R3 "\" get
203   Goto loop
204   get:
205    StrCpy $R0 $R0 -$R1
206    Pop $R3
207    Pop $R2
208    Pop $R1
209    Exch $R0
210    
211 FunctionEnd
212
213 #--------------------------------
214
215 Function un.GetUsers
216  # reads the subfolders of the "Documents and Settings" folder to get a list of the users
217
218   StrCpy $R3 ""
219   Push "$PROFILE"
220   Call un.GetParentA  
221   Pop $R2
222   StrCpy $R2 "$R2"
223   FindFirst $R0 $R1 "$R2\*"
224   StrCmp $R1 "" findend 0
225   findloop:
226    IfFileExists "$R2\$R1\*.*" 0 notDir
227    StrCmp $R1 "." notDir
228    StrCmp $R1 ".." notDir
229    StrCmp $R1 "All Users" notDir
230    StrCmp $R1 "Default User" notDir
231    StrCmp $R1 "All Users.WINNT" notDir
232    StrCmp $R1 "Default User.WINNT" notDir  
233   StrCpy $R3 "$R3|$R1"
234   notDir:
235    FindNext $R0 $R1
236    StrCmp $R1 "" findend 0
237   Goto findloop
238   findend:
239    FindClose $R0
240   
241 FunctionEnd
242
243 #--------------------------------
244
245 Function un.StrPoint
246  !insertmacro StrPointer $String $Search $Pointer
247 FunctionEnd
248
249 #--------------------------------
250
251 !macro UnAppPreSuff AppPre AppSuff
252  # the APPDATA path has always the following structure:
253  # C:\Documents and Settings\username\Application Data
254  # this macro saves the "C:\Documents and Settings\" substring into the variable "AppPre"
255  # and the "Application Data" substring into the variable "AppSuff"
256   
257   StrCpy $String "$APPDATA"
258   StrCpy $Search "\"
259   Call un.StrPoint # search for the first "\"
260   IntOp $Pointer $Pointer + 1 # jump after the "\"
261   StrCpy $String $String "" $Pointer # cut off the part before the first "\"
262   StrCpy $0 $Pointer
263   Call un.StrPoint # search for the second "\"
264   IntOp $0 $0 + $Pointer # $0 is now the pointer to the second "\" in the APPDATA string
265   StrCpy ${AppPre} $APPDATA $0 # save the part before the second "\"
266   IntOp $Pointer $Pointer + 1 # jump after the "\"
267   StrCpy $String $String "" $Pointer # cut off the part before the second "\"
268   Call un.StrPoint # search for the third "\"
269   IntOp $Pointer $Pointer + 1 # jump after the "\"
270   StrCpy ${AppSuff} $String "" $Pointer # save the part after the third "\"
271
272 !macroend
273
274 #--------------------------------
275
276 Function un.DelAppPathSub
277  # deletes a subfolder of the APPDATA path for all users
278
279   # get list of all users
280   Push $R0
281   Push $R1
282   Push $R2
283   Push $R3
284   Call un.GetUsers
285   StrCpy $UserList $R3 "" 1 # cut off the "|" at the end of the list
286   Pop $R3
287   Pop $R2
288   Pop $R1
289   Pop $R0
290   
291   # the usernames in the list of all users is separated by "|"
292   loop:
293    StrCpy $String "$UserList"
294    StrCpy $Search "|"
295    Call un.StrPoint # search for the "|"
296    StrCmp $Pointer "-1" ready
297    StrCpy $0 $UserList $Pointer # $0 contains now the username
298    IntOp $Pointer $Pointer + 1 # jump after the "|"
299    StrCpy $UserList $UserList "" $Pointer # cut off the first username in the list
300    # generate the string for the current user
301    # AppPre and AppSuff are generated in the macro "AppPreSuff"
302    StrCpy $AppPath "$AppPre\$0\$AppSuff\${PRODUCT_SUBFOLDER}"
303    RMDir /r $AppPath # delete the folder
304   Goto loop
305   ready:
306   StrCpy $0 $UserList
307   StrCpy $AppPath "$AppPre\$0\$AppSuff\${PRODUCT_SUBFOLDER}"
308   RMDir /r $AppPath # delete the folder
309   
310 FunctionEnd
311
312 #--------------------------------
313
314 Function CreateAppPathSub
315  # creates a subfolder of the APPDATA path for all users
316
317   # get folder names
318   !insertmacro AppPreSuff $AppPre $AppSuff
319
320   # get list of all users
321   Push $R0
322   Push $R1
323   Push $R2
324   Push $R3
325   Call GetUsers
326   StrCpy $UserList $R3 "" 1 # cut off the "|" at the end of the list
327   Pop $R3
328   Pop $R2
329   Pop $R1
330   Pop $R0
331   
332   # the usernames in the list of all users is separated by "|"
333   loop:
334    StrCpy $String "$UserList"
335    StrCpy $Search "|"
336    Call StrPoint # search for the "|"
337    StrCmp $Pointer "-1" ready # the loop is finished when no "|" could be found
338    StrCpy $0 $UserList $Pointer # $0 contains now the username
339    IntOp $Pointer $Pointer + 1 # jump after the "|"
340    StrCpy $UserList $UserList "" $Pointer # cut off the first username in the list
341    # generate the string for the current user
342    # AppPre and AppSuff are generated in the macro "AppPreSuff"
343    StrCpy $AppPath "$AppPre\$0\$AppSuff\${PRODUCT_SUBFOLDER}"
344    CreateDirectory $AppPath # create the folder
345    CopyFiles "$INSTDIR\Resources\session" "$AppPath"
346   Goto loop
347   ready:
348   # now do the same for the last user name
349   StrCpy $0 $UserList
350   StrCpy $AppPath "$AppPre\$0\$AppSuff\${PRODUCT_SUBFOLDER}"
351   CreateDirectory $AppPath # create the folder
352   CopyFiles "$INSTDIR\Resources\session" "$AppPath"
353   Delete "$INSTDIR\Resources\session" # delete the session file in the INSTDIR because it is unneeded there
354   
355 FunctionEnd
356
357 #--------------------------------
358
359 Function ReplaceLineContent
360  # replaces "$OldString" with "LyX $NewString"
361
362  ${WordReplace} '$R9' "$OldString" "$NewString" "+" '$R9' # macro from WordFunc.nsh
363  Push $0
364  
365 FunctionEnd
366
367 #--------------------------------
368
369 Function CheckAppPathPreferences
370  # replaces a string "$OldString" with "$NewString" in a file "$FileName"
371
372   # get folder names
373   !insertmacro AppPreSuff $AppPre $AppSuff
374
375   # get list of all users
376   Push $R0
377   Push $R1
378   Push $R2
379   Push $R3
380   Call GetUsers
381   StrCpy $UserList $R3 "" 1 # cut off the "|" at the end of the list
382   Pop $R3
383   Pop $R2
384   Pop $R1
385   Pop $R0
386   
387   # the usernames in the list of all users is separated by "|"
388   loopPrefs:
389    StrCpy $String "$UserList"
390    StrCpy $Search "|"
391    Call StrPoint # search for the "|"
392    StrCmp $Pointer "-1" ready # the loop is finished when no "|" could be found
393    StrCpy $0 $UserList $Pointer # $0 contains now the username
394    IntOp $Pointer $Pointer + 1 # jump after the "|"
395    StrCpy $UserList $UserList "" $Pointer # cut off the first username in the list
396    # generate the string for the current user
397    # AppPre and AppSuff are generated in the macro "AppPreSuff"
398    StrCpy $AppPath "$AppPre\$0\$AppSuff\${PRODUCT_SUBFOLDER}"
399    # read the preferences file to test if it exists
400    FileOpen $R1 "$AppPath\$FileName" r
401    IfErrors doneA
402    FileClose $R1
403    # search for "$OldString" and replace it with "$NewString"
404    ${LineFind} "$AppPath\$FileName" "" "1:-1" "ReplaceLineContent" # macro from TextFunc.nsh # calls Function ReplaceLineContent
405    doneA:
406   Goto loopPrefs
407   ready:
408   # now do the same for the last user name
409   StrCpy $0 $UserList
410   StrCpy $AppPath "$AppPre\$0\$AppSuff\${PRODUCT_SUBFOLDER}"
411   FileOpen $R1 "$AppPath\$FileName" r
412   IfErrors doneB
413   FileClose $R1
414   ${LineFind} "$AppPath\$FileName" "" "1:-1" "ReplaceLineContent"
415   doneB:
416   
417 FunctionEnd
418
419 #--------------------------------
420
421 !macro IsUserAdmin Result Name
422
423  ClearErrors
424  UserInfo::GetName
425  IfErrors Win9x
426  Pop $0
427  StrCpy ${Name} $0
428  UserInfo::GetAccountType
429  Pop $1
430  ${if} $1 == "Admin"
431   StrCpy ${Result} "yes"
432  ${else}
433   StrCpy ${Result} "no"
434  ${endif}
435  Goto done
436
437  Win9x:
438   StrCpy ${Result} "yes"
439  done:
440
441 !macroend
442
443 #--------------------------------
444
445 !macro FileCheck Result FileName FilePath
446  # checks if a file exists, returns "True" or "False"
447
448  Push $0
449  Push $1
450  StrCpy $0 ""
451  StrCpy $1 ""
452  FileOpen $0 "${Filepath}\${FileName}" r
453  ${if} $0 = ""
454   StrCpy $1 "False"
455  ${Else}
456   StrCpy $1 "True"
457  ${endif}
458  FileClose $0
459  StrCpy ${Result} $1
460  Pop $1
461  Pop $0
462
463 !macroend
464
465 #------------------------------------------
466
467 Function LaTeXCheck
468  # searches the string "$Search" in the string "$String" and extracts the path around it
469  # the extracted path is checked if the file "latex.exe" is in it
470
471    StartCheck:
472    StrLen $3 $String
473    Call StrPoint
474    ${if} $Pointer == "-1" # if nothing was found
475     StrCpy $LatexPath ""
476     Return
477    ${endif}
478    IntOp $3 $3 - $Pointer
479    StrCpy $4 $String $3 "-$3" # $4 is now the part behind the $Search string
480    StrCpy $String $String $Pointer # $String is now the part before the $Search string
481    StrCpy $Search ":" # search for the ":" after the first previous drive letter
482    Call RevStrPoint
483    IntOp $Pointer $Pointer - 1 # jump before the ":" to the drive letter
484    StrCpy $Pointer $Pointer "" 1 # cut of the "-" sign
485    StrCpy $LatexPath $String $Pointer "-$Pointer"
486    StrCpy $String $4
487    StrCpy $Search ";" # search for the following ";" that separates the different paths
488    Call StrPoint
489    ${if} $Pointer != "-1" # if something was found
490     StrCpy $String $String $Pointer
491    ${endif}
492    StrCpy $LatexPath "$LatexPath$String"
493    # check if the latex.exe exists in the $LatexPath folder
494    !insertmacro FileCheck $5 "latex.exe" "$LatexPath"
495    ${if} $5 == "False" # delete the entry with the wrong path to the latex.exe and try again
496     StrCpy $LatexPath ""
497     StrLen $3 $String
498     StrCpy $String $4 "" $3
499     ${if} $2 == "TeXLive"
500      StrCpy $Search "TeXLive"
501     ${else}
502      StrCpy $Search "miktex"
503     ${endif}
504     Goto StartCheck
505    ${endif}
506
507 FunctionEnd
508