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