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