]> git.lyx.org Git - features.git/blob - development/Win32/packaging/installer2/src/main.nsh
Wininstaller2: copy user preferences from previous minor release
[features.git] / development / Win32 / packaging / installer2 / src / main.nsh
1 #-------------------------------------------------------------
2 # - - - - - - - - - - - - Preamble - - - - - - - - - - - - - -
3 #-------------------------------------------------------------
4
5   # Do a Cyclic Redundancy Check to make sure the installer
6   # was not corrupted by the download.
7   CRCCheck force
8   # Make the installer as small as possible
9   SetCompressor /SOLID lzma
10   # Make it a Unicode installer
11   Unicode true
12   # Enable support for high DPI resolution
13   ManifestDPIAware true
14
15   Name "LyX ${APP_VERSION}" # Name of the Program to be displayed during installation
16   BrandingText " " # Remove branding text ('Nullsoft Install System vX.XX')
17
18   # ===== Variables =====
19
20     Var LatexPath # Used to store path to "latex.exe"
21     Var StartMenuFolder # Used to store the start menu folder
22     Var pathPrefix
23
24     # FIXME Variables needed due to "old" set of language strings, delete
25     Var LaTeXInstalled
26     Var LaTeXName
27     Var OldVersionNumber
28     Var AppPre
29     Var AppSuff
30     # R3
31
32     # FIXME Compile time constants needed due to "old" set of language strings, delete
33     !define APP_SERIES_KEY2 ${APP_VERSION}
34     !define APP_DIR_USERDATA "LyX${VERSION_MAJOR}.${VERSION_MINOR}"
35
36   # ========= Multiuser settings =========
37
38     !define MULTIUSER_MUI # Multiuser uses modern UI 2
39     !define MULTIUSER_EXECUTIONLEVEL Highest # Can install with any privileges (admin, user)
40     !if ${APP_ARCHITECTURE} = 64
41       !define MULTIUSER_USE_PROGRAMFILES64 # Install 64 bit program in "Program files" instead of "Program files (x86)"
42     !endif
43
44     !define MULTIUSER_INSTALLMODE_COMMANDLINE # Allow setting installmode in commandline (/AllUsers or /CurrentUser)
45
46     # Set default install mode based on a non empty registry entry (if exists in HKLM, then all users, if HKCU then current user)
47     !define MULTIUSER_INSTALLMODE_DEFAULT_REGISTRY_KEY "SOFTWARE\LyX"
48     !define MULTIUSER_INSTALLMODE_DEFAULT_REGISTRY_VALUENAME "InstDir${VERSION_MAJOR}${VERSION_MINOR}"
49
50     !define MULTIUSER_INSTALLMODE_INSTDIR "LyX ${VERSION_MAJOR}.${VERSION_MINOR}" # Default install dir if not over-installing
51
52     # Retrieve default install location from following registry entry (HKCU/HKLM  is chosen automatically)
53     !define MULTIUSER_INSTALLMODE_INSTDIR_REGISTRY_KEY "SOFTWARE\LyX"
54     !define MULTIUSER_INSTALLMODE_INSTDIR_REGISTRY_VALUENAME "InstDir${VERSION_MAJOR}${VERSION_MINOR}"
55
56     !include MultiUser.nsh # Header file: Installer configuration for multi-user Windows environments
57     # When MULTIUSER_INSTALLMODE_COMMANDLINE is defined, MultiUser.nsh defines ${StrStr} and ${UnStrStr} functions
58
59   # ============= Modern UI 2 general settings ===============
60
61     #!include MUI2.nsh # included in MultiUser # Header file for creating modern looking installer
62     !define MUI_ABORTWARNING # Show a message box with a warning when the user wants to close the installer.
63     !define MUI_ICON "..\installer\icons\lyx.ico" # FIXME path # The icon for the installer.
64     !define MUI_UNICON "..\installer\icons\lyx.ico" # FIXME path # The icon for the uninstaller
65     !define MUI_HEADERIMAGE # Display an image on the header of the page.
66     !define MUI_HEADERIMAGE_BITMAP "..\installer\graphics\header.bmp" # FIXME path # Header image source
67     !define MUI_HEADERIMAGE_RIGHT # Display header image on the right and not left
68     !define MUI_WELCOMEFINISHPAGE_BITMAP "..\installer\graphics\wizard.bmp" # FIXME path # Source for image on welcome and finish page of the installer
69     !define MUI_UNWELCOMEFINISHPAGE_BITMAP "..\installer\graphics\wizard.bmp" # FIXME path # Source for image on welcome and finish page of the uninstaller
70
71   # ======== Pages definitions ==========
72
73     # Installer
74
75     !define MUI_WELCOMEPAGE_TEXT $(TEXT_WELCOME) # Use custom welcome text
76     !insertmacro MUI_PAGE_WELCOME # Welcome page
77
78     !insertmacro MUI_PAGE_LICENSE "..\installer\license.rtf" # FIXME path # License page
79
80     !define MUI_PAGE_CUSTOMFUNCTION_PRE PrepareShellCTX # Prepare shell context before the multiuser page is created, so that the installer searches for default install folder in the right context (x64/x32)
81     !insertmacro MULTIUSER_PAGE_INSTALLMODE # Multiuser page
82
83     !define MUI_PAGE_CUSTOMFUNCTION_LEAVE VerifyInstDir # Custom leave-function for directory page to verify installdir
84     !insertmacro MUI_PAGE_DIRECTORY # Directory page (where to install)
85
86     !define MUI_PAGE_CUSTOMFUNCTION_PRE DefaultDesktopFileAssoc # Custom pre-function for components page to search for already present dictionaries (in dictionaries.nsh)
87     !insertmacro MUI_PAGE_COMPONENTS # Components page (what to install)
88
89     # Default startmenu folder if registry contains no information from a previous installment, same as the default install dir
90     !define MUI_STARTMENUPAGE_DEFAULTFOLDER "${MULTIUSER_INSTALLMODE_INSTDIR}"
91     # Define registry entry to store the startmenu folder, also reads this entry in as default startmenu folder in case of over-installing
92     !define MUI_STARTMENUPAGE_REGISTRY_ROOT "SHCTX" # Context dependent on what the user chooses on multiuser page
93     !define MUI_STARTMENUPAGE_REGISTRY_KEY "SOFTWARE\LyX"
94     !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "SMDir${VERSION_MAJOR}${VERSION_MINOR}"
95     !define MUI_PAGE_CUSTOMFUNCTION_PRE PrepareShellCTX # Prepare shell context before the startmenu page is created, so that the installer searches for default startmenu folder in the right context
96     !define MUI_PAGE_CUSTOMFUNCTION_SHOW RetrieveSMState # Custom show-function for startmenu page to disable startmenu dir creation in case of over-installment where previously disabled
97     !insertmacro MUI_PAGE_STARTMENU startmenuPage $StartMenuFolder # Page to choose start menu folder entry, the folder is stored in VARIABLE
98
99     # Latexfolder page
100     !include "src\CustomPages\LaTeXFolder.nsdinc"
101     Page custom LatexFolder_Show LatexFolder_Leave
102
103     !insertmacro MUI_PAGE_INSTFILES # Progress page
104
105     !define MUI_FINISHPAGE_SHOWREADME # Showreadme-checkbox appears
106     !define MUI_FINISHPAGE_SHOWREADME_NOTCHECKED # Showreadme-checkbox disabled by default
107     !define MUI_FINISHPAGE_SHOWREADME_FUNCTION StartLyX # Custom function for the showreadme-checkbox (start latex instead of show readme)
108     !define MUI_FINISHPAGE_SHOWREADME_TEXT $(FinishPageRun) # Custom text for the label on the showreadme-checkbox
109     !define MUI_FINISHPAGE_LINK $(TEXT_FINISH_WEBSITE) # Text for the link on the bottom of finish page
110     !define MUI_FINISHPAGE_LINK_LOCATION "https://www.lyx.org/" # Website opens by clicking on the link on finish page
111     !insertmacro MUI_PAGE_FINISH # Finish page
112
113     # Uninstaller
114
115     !insertmacro MUI_UNPAGE_WELCOME # Welcome page
116     !insertmacro MUI_UNPAGE_CONFIRM # Confirmation page
117
118     !define MUI_PAGE_CUSTOMFUNCTION_PRE un.ComponentsPre # Custom function for components page to prepare strings for the description of a section
119     !insertmacro MUI_UNPAGE_COMPONENTS # Components page
120
121     !insertmacro MUI_UNPAGE_INSTFILES # Progress page
122     !insertmacro MUI_UNPAGE_FINISH # Finish page
123
124   # ======= Other includes =======
125
126     !include "src\lang\TranslatedLanguages.nsh" # languages strings
127
128     #!include LogicLib.nsh # included in MUI2 # Allows using logic commands (such as ${If}..${Else}..${EndIf})
129     #!include LangFile.nsh # included in MUI2 # Header file to create language files that can be included with a single command.
130     !include x64.nsh # Header file to check if target system is 64 bit or not with ${RunningX64}, also defines ${DisableX64FSRedirection} and ${EnableX64FSRedirection}
131     !include NSISList.nsh # Header file to create and work with lists in NSIS (plugin)
132
133     #!include StrFunc.nsh # included in MultiUser # Provides some additional string functions
134     ${StrRep} # Define this function from StrFunc.nsh for further use
135     ${StrStrAdv} # Define this function from StrFunc.nsh for further use
136     ${UnStrStrAdv} # Define this function from StrFunc.nsh for further use
137     ${UnStrRep} # Define this function from StrFunc.nsh for further use
138
139   # ========= Installer Attributes ===========
140
141     VIProductVersion "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION}.${VERSION_BUILD}" # Must be X.X.X.X format
142     VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductName" "LyX"
143     VIAddVersionKey /LANG=${LANG_ENGLISH} "ProductVersion" "LyX ${APP_VERSION} installer"
144     VIAddVersionKey /LANG=${LANG_ENGLISH} "FileVersion" "${APP_VERSION}"
145     VIAddVersionKey /LANG=${LANG_ENGLISH} "FileDescription" "LyX - The Document Processor"
146     VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalCopyright" "LyX is Copyright © 1995 by Matthias Ettrich, 1995-${COPYRIGHT_YEAR} by the LyX Team"
147     VIAddVersionKey /LANG=${LANG_ENGLISH} "CompanyName" "LyX Team"
148     VIAddVersionKey /LANG=${LANG_ENGLISH} "LegalTrademarks" ""
149
150 #-------------------------------------------------------------
151 # - - - - - - - - - - Helper Functions - - - - - - - - - - - -
152 #-------------------------------------------------------------
153
154   # Forward declarations
155   
156   !macro SearchAllRegistry resultVar resultStateIdx resultRegIdx subKey searchFor startStateIdx startRegIdx
157     Push ${subKey}
158     Push ${searchFor}
159     Push ${startStateIdx}
160     Push ${startRegIdx}
161     Call SearchAllRegistry
162     Pop ${resultStateIdx}
163     Pop ${resultRegIdx}
164     Pop ${resultVar}
165   !macroend
166   
167   !define SearchAllRegistry "!insertmacro SearchAllRegistry"
168   
169   !macro SearchRegistry resultVar resultRegIdx subKey searchFor startRegIdx
170     Push ${subKey}
171     Push ${searchFor}
172     Push ${startRegIdx}
173     Call SearchRegistry
174     Pop ${resultRegIdx}
175     Pop ${resultVar}
176   !macroend
177   
178   !define SearchRegistry "!insertmacro SearchRegistry"
179   
180   # Functions
181   
182   Function SearchAllRegistry
183     Exch $9 # startRegIdx
184     Exch
185     Exch $8 # startStateIdx
186     Exch 2
187     Exch $7 # searchFor
188     Exch 3
189     Exch $6 # subKey
190     Push $5
191   
192     ${Do}
193       ${Select} $8
194         ${Case} 0
195           SetShellVarContext all
196           ${If} ${RunningX64}
197             SetRegView 64
198           ${Else}
199             IntOp $8 $8 + 1
200           ${EndIf}
201         ${Case} 1
202           SetShellVarContext all
203           ${If} ${RunningX64}
204             SetRegView 32
205           ${EndIf}
206         ${Case} 2
207           SetShellVarContext current
208           ${If} ${RunningX64}
209             SetRegView 64
210           ${Else}
211             IntOp $8 $8 + 1
212           ${EndIf}
213         ${Case} 3
214           SetShellVarContext current
215           ${If} ${RunningX64}
216             SetRegView 32
217           ${EndIf}
218       ${EndSelect}
219       ${SearchRegistry} $5 $9 $6 $7 $9
220       ${If} $5 != ""
221         ${Break}
222       ${EndIf}
223       IntOp $8 $8 + 1
224       StrCpy $9 0
225     ${LoopUntil} $8 >= 4
226   
227     Exch $5 # resultVar
228     Exch 4
229     Pop $7
230     Pop $6
231     Exch $9 # resultRegIdx
232     Exch
233     Exch $8 # resultStateIdx
234   FunctionEnd
235   
236   Function SearchRegistry
237     Exch $9 # startRegIdx
238     Exch
239     Exch $8 # searchFor
240     Exch 2
241     Exch $7 #subKey
242     Push $6
243     Push $5
244   
245     ${Do}
246       EnumRegKey $6 SHCTX $7 $9
247       ${StrStr} $5 $6 $8
248       IntOp $9 $9 + 1
249       ${If} $5 != ""
250         ${Break}
251       ${EndIf}
252     ${LoopUntil} $6 == ""
253   
254     Exch $5 # resultVar
255     Exch 4
256     Pop $8
257     Pop $6
258     Pop $7
259     Exch $9 # resultRegIdx
260   FunctionEnd
261   
262   !macro PrepareShellCTX
263     ${if} $MultiUser.InstallMode == "CurrentUser"
264       SetShellVarContext current
265     ${Else}
266       SetShellVarContext all
267     ${EndIf}
268     ${If} ${RunningX64}
269       SetRegView ${APP_ARCHITECTURE}
270     ${EndIf}
271   !macroend
272
273   Function PrepareShellCTX
274     !insertmacro PrepareShellCTX
275   FunctionEnd
276
277   Function un.PrepareShellCTX
278     !insertmacro PrepareShellCTX
279   FunctionEnd
280
281 #-------------------------------------------------------------
282 # - - - - - - - - - - - - Sections - - - - - - - - - - - - - -
283 #-------------------------------------------------------------
284
285 # ============ Installer Page Functions ============
286
287 Function .onInit # Callback function, called at the very beginning, when user double clicks on the installer
288
289   !if ${APP_ARCHITECTURE} = 64 # If 64 bit installer
290     # Abort if not 64 bit Windows
291     ${IfNot} ${RunningX64}
292       MessageBox MB_OK|MB_ICONSTOP "Cannot install a 64 bit application on 32 bit Windows, please use the 32 bit installer" /SD IDOK
293       Quit
294     ${EndIf}
295   !EndIf
296
297   # Check Windows version
298   ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion
299   ${if} $0 S<= "6.0" # Atleast Win7 is needed
300     MessageBox MB_OK|MB_ICONSTOP "LyX ${APP_VERSION} requires Windows 7 or newer." /SD IDOK
301     Quit
302   ${endif}
303
304   Call PrepareShellCTX # MULTIUSER_INIT should search in the right registry view
305   !insertmacro MULTIUSER_INIT # Verify multiuser privileges
306
307   # Check if the same version of LyX is installed anywhere (admin/user, x64/x32) on the computer
308   StrCpy $1 0
309   StrCpy $2 0
310   ${DoUntil} $1 >= 4
311     ${SearchAllRegistry} $0 $1 $2 "SOFTWARE\LyX" ${APP_VERSION_DOTLESS} $1 $2 # Helper Function from above
312     ${If} $0 == ${APP_VERSION_DOTLESS}
313       ReadRegStr $0 SHCTX "SOFTWARE\LYX\$0" ""
314       ${If} ${FileExists} "$0\Uninstall-LyX.exe"
315         ${IfCmd} MessageBox MB_YESNO|MB_DEFBUTTON2|MB_ICONEXCLAMATION "$(AlreadyInstalled)" /SD IDNO IDNO ${||} Quit ${|}
316       ${Else}
317         DeleteRegKey SHCTX "SOFTWARE\LYX\$0"
318       ${EndIf}
319     ${EndIf}
320   ${Loop} 
321
322   # Check if a newer version of the same LyX series is installed anywhere (admin/user, x64/x32) on the computer
323   StrCpy $1 0
324   StrCpy $2 0
325   ${DoUntil} $1 >= 4
326     ${SearchAllRegistry} $R9 $1 $2 "SOFTWARE\LyX" "${VERSION_MAJOR}${VERSION_MINOR}" $1 $2 # Helper Function from above
327     StrCpy $OldVersionNumber $R9 # FIXME remove
328     ${If} $R9 S> ${APP_VERSION_DOTLESS} # $R9 is used in $(NewerInstalled)
329       ReadRegStr $0 SHCTX "SOFTWARE\LYX\$0" ""
330       ${If} ${FileExists} "$0\Uninstall-LyX.exe"
331         MessageBox MB_OK|MB_ICONSTOP "$(NewerInstalled)" /SD IDOK
332         Quit
333       ${Else}
334         DeleteRegKey SHCTX "SOFTWARE\LYX\$0"
335       ${EndIf}
336     ${EndIf}
337   ${Loop}
338 FunctionEnd
339
340 Function CheckIfRunning # Check that LyX in $INSTDIR is not currently running, called from Function VerifyInstDir and Section -CheckSilent (if silentinstall)
341   ${If} ${RunningX64}
342     ${DisableX64FSRedirection} # We need the following process to be 64 bit on 64 bit system
343   ${EndIf}
344   nsExec::ExecToStack "powershell (Get-Process LyX).Path"
345   Pop $0 # Exit code
346   Pop $0 # Result string
347   ${If} ${RunningX64}
348     ${EnableX64FSRedirection} # Need to be anabled asap or installer might crash
349   ${EndIf}
350   ${StrStr} $0 $0 "$INSTDIR\bin\LyX.exe"
351   ${If} $0 != ""
352     MessageBox MB_OK|MB_ICONSTOP "$(UnInstallRunning)" /SD IDOK
353     Abort # Abort leaving the page (when called from the page callback) / Abort install (when called from the section)
354   ${EndIf}
355 FunctionEnd
356
357 Function VerifyInstDir # Custom Function, called when leaving directory page
358   # if the $INSTDIR does not contain "LyX" we must add a subfolder to avoid that LyX will e.g.
359   # be installed directly to "C:\Program Files" - the uninstaller will then delete the whole
360   # "C:\Program Files" directory
361   ${StrStr} $0 $INSTDIR LyX
362   ${If} $0 == ""
363     StrCpy $INSTDIR "$INSTDIR\${MULTIUSER_INSTALLMODE_INSTDIR}" # Append e.g. "LyX 2.3" to $INSTDIR
364     ${NSD_SetText} $mui.DirectoryPage.Directory $INSTDIR # Refresh Textbox
365     Abort # Abort leaving the page
366   ${EndIf}
367
368   Call CheckIfRunning
369 FunctionEnd
370
371 Function RetrieveSMState # Custom function, called after the Startmenu page has been created
372   ${IfNot} ${FileExists} "$INSTDIR\Uninstall-LyX.exe"
373     Return # Not over-installing
374   ${EndIf}
375
376   Call PrepareShellCTX # Helper function from above
377   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "SMDir${VERSION_MAJOR}${VERSION_MINOR}"
378   ${If} $0 == ""
379     ${If} ${Silent}
380       StrCpy $StartMenuFolder ">$StartMenuFolder"
381     ${Else}
382       GetDlgItem $0 $mui.StartMenuPage 1005 # Get the HWND of the checkbox, MUI2 does not provide variable for that
383       SendMessage $0 ${BM_CLICK} 0 0 # NSIS delivers no other possibility here other than to send a click event to the checkbox
384     ${EndIf}
385   ${EndIf}
386 FunctionEnd
387
388 Function LatexFolder_Show # Custom Function, called before showing the LaTeXFolder page to create it
389   Call FindLatex # detect latex  
390   Pop $0 # Result string from FindLatex
391   # $0 should contain the path to latex.exe, it is used in the fnc_LaTeXFolder_Create
392   ${StrStr} $1 $0 "\latex.exe"
393   ${If} $1 != "" # If latex was actually found
394     ${StrRep} $0 $0 $1 ""
395     # $R9 is used in $(EnterLaTeXFolder) on the page
396     ${StrStr} $1 $0 "\miktex\bin"
397     ${If} $1 != ""
398       StrCpy $R9 "MiKTeX"
399     ${Else}
400       StrCpy $R9 "TeXLive"
401     ${EndIf}
402     StrCpy $LaTeXName $R9 # FIXME remove
403     StrCpy $1 $(EnterLaTeXFolder) # $1 is used on the page
404   ${Else}
405     StrCpy $1 $(EnterLaTeXFolderNone)
406   ${EndIf}
407   ${StrRep} $1 $1 "\r\n" "$\r$\n" # FIXME remove
408
409   Call fnc_LaTeXFolder_Show # In LaTeXFolder.nsdinc, defined by NSIS Dialog Designer
410 FunctionEnd
411
412 Function FindLatex # Searches latex.exe, called from pre and leave functions of latexfolder page
413   # Find "LaTeX Path" value in registry written by a previous installment of the same LyX series, prioritized, because could be user preference
414   Call PrepareShellCTX
415   ${If} ${RunningX64}
416     SetRegView 64
417   ${EndIf}
418   StrCpy $1 0
419   StrCpy $2 0
420   ${DoUntil} $2 > 1
421     ${SearchRegistry} $0 $1 "SOFTWARE\LyX" "${VERSION_MAJOR}${VERSION_MINOR}" $1
422     ${If} $0 != ""
423       ReadRegStr $0 SHCTX "SOFTWARE\LyX\$0" "LaTeX Path"
424       StrCpy $0 "$0\latex.exe"
425       ${If} ${FileExists} $0
426         Push $0
427         Return
428       ${EndIf}
429     ${ElseIf} ${RunningX64}
430       IntOp $2 $2 + 1
431       SetRegView 32
432     ${Else}
433       ${Break}
434     ${EndIf}
435   ${Loop}
436
437   nsExec::ExecToStack "where latex.exe" # Ask CMD to find "latex.exe" for us, works if latexpath is added to PATH
438   Pop $0 # Exit code: 0 = success, 1 = fail
439   ${If} $0 = 0
440     Return
441   ${EndIf}
442   Pop $0 # If search failed, this is garbage, otherwise path to latex
443
444   # Search the registry for TexLive
445   StrCpy $1 0
446   StrCpy $2 0
447   ${DoUntil} $1 >= 4
448     ${SearchAllRegistry} $0 $1 $2 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" "TeXLive" $1 $2 # Helper Function from above
449     ReadRegStr $0 SHCTX "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$0" "UninstallString"
450     ${StrRep} $0 $0 '"' ""
451     ${StrRep} $0 $0 "tlpkg\installer\uninst.bat" "bin\win32\latex.exe" # TeXLive is not 64 bit yet
452     ${If} ${FileExists} $0
453       Push $0
454       Return
455     ${EndIf}
456   ${Loop}
457
458   # Search the registry for MiKTex  
459   StrCpy $1 0
460   StrCpy $2 0
461   ${DoUntil} $1 >= 4
462     ${SearchAllRegistry} $0 $1 $2 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" "MiKTeX" $1 $2 # Helper Function from above
463     ReadRegStr $0 SHCTX "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$0" "InstallLocation"
464     StrCpy $0 "$0\miktex\bin\latex.exe"
465     ${If} ${FileExists} $0
466       Push $0
467       Return
468     ${EndIf}
469     ${If} ${RunningX64} # MiKTeX can also be x64
470       ${StrRep} $0 $0 "latex.exe" "x64\latex.exe"
471       ${If} ${FileExists} $0
472         Push $0
473         Return
474       ${EndIf}
475     ${EndIf}
476   ${Loop}
477
478   # No latex was found
479   Push "C:\"
480
481 FunctionEnd
482
483 Function LatexFolder_Leave # Custom function, called when trying to leave LatexFolder page to verify that "latex.exe" really exists in the given path
484   ${NSD_GetText} $hCtl_LaTeXFolder_DirRequest1_Txt $LatexPath
485   ${NSD_GetState} $hCtl_LaTeXFolder_CheckBox1 $0
486
487   ${If} $0 = ${BST_CHECKED}
488     StrCpy $LatexPath ""
489   ${ElseIfNot} ${FileExists} "$LatexPath\latex.exe"
490     MessageBox MB_OK $(InvalidLaTeXFolder)
491     Call FindLatex # Search for latex again
492     Pop $0 # Result string from FindLatex
493     ${StrStrAdv} $0 $0 "\latex.exe" ">" "<" "0" "0" "0" # Get everything before "\latex.exe"
494     ${NSD_SetText} $hCtl_LaTeXFolder_DirRequest1_Txt $0 # Refresh textbox
495     Abort # Abort leaving the page
496   ${EndIf}
497 FunctionEnd
498
499 # ========= Installer Sections =========
500
501 Section -CheckSilent # This section checks if it's a silent install and calls needed callback- and custom-pagefunctions, which are not called in this case
502   ${IfNot} ${Silent}
503     Return
504   ${EndIf}
505
506   # .onInit is called
507
508   Call CheckIfRunning
509
510   Call RetrieveSMState
511
512   Call FindLatex # Search for latex
513   Pop $0 # Result string from FindLatex
514   ${StrStrAdv} $0 $0 "\latex.exe" ">" "<" "0" "0" "0" # Get everything before "\latex.exe"
515   ${If} ${FileExists} "$0\latex.exe"
516     StrCpy $LatexPath $0
517   ${Else}
518     MessageBox MB_OK $(InvalidLaTeXFolder)
519     Quit
520   ${EndIf}
521
522   Call DefaultDesktopFileAssoc
523 SectionEnd
524
525 Section -OverInstallReg # If over-installing, we need to delete the registry keys written by the previous installment
526   Call PrepareShellCTX # Helper function from above
527   StrCpy $1 0
528   ${Do}
529     EnumRegKey $2 SHCTX "SOFTWARE\LyX" $1
530     ${StrStr} $3 $2 "${VERSION_MAJOR}${VERSION_MINOR}"
531     ReadRegStr $0 SHCTX "SOFTWARE\LyX\$3" ""
532     ${If} $0 == $INSTDIR
533       DeleteRegKey SHCTX "SOFTWARE\LyX\$3"
534       DeleteRegKey SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX$3"
535     ${EndIf}
536     IntOp $1 $1 + 1
537   ${LoopUntil} $2 == ""
538 SectionEnd
539
540 Section -ProgramFiles
541
542   # Install the core LyX files
543   
544   # Initializes the plug-ins dir ($PLUGINSDIR) if not already initialized.
545   # $PLUGINSDIR is automatically deleted when the installer exits.
546   InitPluginsDir
547   
548   # Binaries
549   SetOutPath "$INSTDIR\bin"
550   File "${FILES_LYX}\bin\LyX.exe"
551   File "${FILES_LYX}\bin\tex2lyx.exe"
552   # Visual C++ redistributable (if native build)
553   File "${FILES_DEPS}\bin\concrt140.dll"
554   File "${FILES_DEPS}\bin\msvcp140.dll"
555   File "${FILES_DEPS}\bin\vcamp140.dll"
556   File "${FILES_DEPS}\bin\vccorlib140.dll"
557   File "${FILES_DEPS}\bin\vcomp140.dll"
558   File "${FILES_DEPS}\bin\vcruntime140.dll"
559   !if ${APP_ARCHITECTURE} = 64
560     File "${FILES_DEPS}\bin\vcruntime140_1.dll"
561   !endif
562   # MinGW redistributable (if cross platform with MinGW)
563   #File "${FILES_DEPS}\bin\iconv.dll"
564   #File "${FILES_DEPS}\bin\libbz2-1.dll"
565   #File "${FILES_DEPS}\bin\libfreetype-6.dll"
566   #File "${FILES_DEPS}\bin\libgcc_s_sjlj-1.dll"
567   #File "${FILES_DEPS}\bin\libglib-2.0-0.dll"
568   #File "${FILES_DEPS}\bin\libharfbuzz-0.dll"
569   #File "${FILES_DEPS}\bin\libintl-8.dll"
570   #File "${FILES_DEPS}\bin\libjpeg-62.dll"
571   #File "${FILES_DEPS}\bin\libpcre-1.dll"
572   #File "${FILES_DEPS}\bin\libpcre2-16-0.dll"
573   #File "${FILES_DEPS}\bin\libpng16-16.dll"
574   #File "${FILES_DEPS}\bin\libstdc++-6.dll"
575   #File "${FILES_DEPS}\bin\libtiff-5.dll"
576   #File "${FILES_DEPS}\bin\libwinpthread-1.dll"
577   #File "${FILES_DEPS}\bin\libwebp-7.dll"
578   #File "${FILES_DEPS}\bin\libwebpdemux-2.dll"
579   #File "${FILES_DEPS}\bin\zlib1.dll"
580   # Netpbm
581   File "${FILES_DEPS}\bin\libnetpbm10.dll"
582   File "${FILES_DEPS}\bin\pnmcrop.exe"
583   # Rsvg
584   File "${FILES_DEPS}\bin\rsvg-convert.exe"
585   # Pdfview
586   File "${FILES_DEPS}\bin\pdfview.exe"
587   # Qt libraries
588   File "${FILES_QT}\bin\Qt5Concurrent.dll"
589   File "${FILES_QT}\bin\Qt5Core.dll"
590   File "${FILES_QT}\bin\Qt5Gui.dll"
591   File "${FILES_QT}\bin\Qt5Network.dll"
592   File "${FILES_QT}\bin\Qt5OpenGL.dll"
593   File "${FILES_QT}\bin\Qt5PrintSupport.dll"
594   File "${FILES_QT}\bin\Qt5Svg.dll"
595   File "${FILES_QT}\bin\Qt5Widgets.dll"
596   File "${FILES_QT}\bin\Qt5WinExtras.dll"
597
598   # Qt plugin DLLs
599   SetOutPath "$INSTDIR\bin\imageformats"
600   File "${FILES_QT}\bin\imageformats\qgif.dll"
601   File "${FILES_QT}\bin\imageformats\qico.dll"
602   File "${FILES_QT}\bin\imageformats\qjpeg.dll"
603   File "${FILES_QT}\bin\imageformats\qsvg.dll"
604   File "${FILES_QT}\bin\imageformats\qicns.dll"
605   File "${FILES_QT}\bin\imageformats\qtiff.dll"
606   File "${FILES_QT}\bin\imageformats\qwbmp.dll"
607   File "${FILES_QT}\bin\imageformats\qwebp.dll"
608
609   SetOutPath "$INSTDIR\bin\iconengines"
610   File "${FILES_QT}\bin\iconengines\qsvgicon.dll"
611
612   SetOutPath "$INSTDIR\bin\platforms"
613   File "${FILES_QT}\bin\platforms\qminimal.dll"
614   File "${FILES_QT}\bin\platforms\qwindows.dll"
615
616   SetOutPath "$INSTDIR\bin\styles"
617   File "${FILES_QT}\bin\styles\qwindowsvistastyle.dll"
618
619   # Resources
620   SetOutPath "$INSTDIR\Resources"
621   # recursively copy all files under Resources
622   File /r "${FILES_LYX}\Resources\"
623   File /r "${FILES_DEPS}\Resources\" # Already present files will be skiped
624
625   # Python
626   SetOutPath "$INSTDIR\Python"
627   # recursively copy all files under Python
628   File /r "${FILES_DEPS}\Python\"
629
630   # Components of ImageMagick
631   SetOutPath "$INSTDIR\imagemagick"
632   File /r "${FILES_DEPS}\imagemagick\"
633
634   # Components of Ghostscript
635   SetOutPath "$INSTDIR\ghostscript"
636   File /r "${FILES_DEPS}\ghostscript\"
637
638   # Install unoconv
639   SetOutPath "$INSTDIR\Python\Lib"
640   File "${FILES_DEPS}\bin\unoconv.py"
641   
642   WriteUninstaller "$INSTDIR\Uninstall-LyX.exe"
643 SectionEnd
644
645 Section -TexRessources # Section installs cls files from Ressources\tex and updates latex filebase
646   # First make sure that latex was found in LatexFolder_Leave function
647   ${If} $LatexPath == ""
648     Return
649   ${EndIf}
650
651   # Miktex or Texlive?
652   ${StrStr} $0 $LatexPath "\miktex\bin"
653   ${If} $0 == "" # TexLive
654     ${StrRep} $0 $LatexPath "\bin\win32" "\texmf-dist\tex\latex\lyx"
655     ${If} ${FileExists} "$0\broadway.cls"
656       Return
657     ${EndIf}
658     CopyFiles /SILENT "$INSTDIR\Resources\tex\*.*" $0
659     ExecWait '$LatexPath\texhash' # Update package file list
660
661   ${Else} # Miktex
662     ${StrRep} $0 $LatexPath $0 "\tex\latex\lyx"
663     ${If} ${FileExists} "$0\broadway.cls"
664       Return
665     ${EndIf}
666     CopyFiles /SILENT "$INSTDIR\Resources\tex\*.*" $0 # First try to put into Miktex install dir
667
668     ${IfNot} ${FileExists} "$0\broadway.cls" # Check if copying succeeded
669       # Didn't suceed to put into install dir, put into users dir
670       nsExec::ExecToStack "$LatexPath\mpm.exe --version" # get the Miktex version
671       Pop $0 # Exit code
672       Pop $0 # Result
673       ${StrStrAdv} $0 $0 "MiKTeX Package Manager " ">" ">" "0" "0" "0" # $0 contains "2.9.7420 ......" now (everything after "MiKTeX Package Manager ")
674       ${StrStrAdv} $0 $0 "." ">" "<" "0" "1" "0" # $0 contains "2.9" now (everything before the second dot)
675       SetShellVarContext current
676       ${If} ${FileExists} "$APPDATA\MiKTeX\$0\tex\latex\lyx\broadway.cls" # UserInstall-dir can not be changed in Miktex currently, so this should work
677         Return
678       ${EndIf}
679       CopyFiles /SILENT "$INSTDIR\Resources\tex\*.*" "$APPDATA\MiKTeX\$0\tex\latex\lyx" # Put into user install dir
680     ${EndIf}
681
682     # Update package file list
683     nsExec::ExecToLog "$LatexPath\initexmf.exe --admin --update-fndb" # Try as admin
684     Pop $0 # Exit code
685     ${If} $0 <> 0 # Not successful
686       nsExec::ExecToLog "$LatexPath\initexmf.exe --update-fndb" # Do as user
687       Pop $0
688     ${EndIf}
689   ${EndIf}
690 SectionEnd
691
692 Section -CompilePython
693   # Compile all Python files to byte-code
694   # The user using the scripts may not have write access
695   FileOpen $R0 "$INSTDIR\compilepy.py" w
696   FileWrite $R0 "import compileall$\r$\n"
697   FileWrite $R0 "compileall.compile_dir('$INSTDIR\python\Lib')$\r$\n"
698   FileWrite $R0 "compileall.compile_dir('$INSTDIR\Resources')$\r$\n"
699   FileClose $R0
700   DetailPrint $(TEXT_CONFIGURE_PYTHON)
701   nsExec::ExecToLog '"$INSTDIR\python\python.exe" "$INSTDIR\compilepy.py"'
702   Pop $0 # Exit code
703   Delete "$INSTDIR\compilepy.py"
704 SectionEnd
705
706 Section -LyxrcDist
707   StrCpy $pathPrefix "$LatexPath;$$LyXDir\bin;$$LyXDir\Python;$$LyXDir\Python\Lib;$$LyXDir\imagemagick;$$LyXDir\ghostscript\bin"
708
709   # Find additional software and add their install locations to the path_prefix
710   # This list contains all software, which write the string value "InstallLocation" in their corresponding keys in "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
711   # To add additional software matching this requirement, just append it to the string after "${List.Append} editors "
712   ${List.Create} editors
713   ${List.Append} editors "jEdit,PSPad,WinShell,TeXnicCenter,WinEdt"
714   ${List.Count} $R1 editors
715   IntOp $R1 $R1 - 1
716   ${For} $R2 0 $R1
717     ${List.Get} $0 editors $R2
718     ${SearchAllRegistry} $0 $1 $1 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" $0 0 0 # Helper Function from above
719     ReadRegStr $0 SHCTX "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$0" "InstallLocation"
720     ${If} $0 != ""
721       StrCpy $1 $0 "" -1 # Get the last char
722       ${If} $1 == "\" # If ends with "\"
723         StrCpy $0 $0 -1 # Remove "\"
724       ${EndIf}
725       StrCpy $pathPrefix "$pathPrefix;$0"
726     ${EndIf}
727   ${Next}
728   ${List.Destroy} editors
729
730   # Following Software does not create "InstallLocation"
731   # Vim  
732   ${SearchAllRegistry} $0 $1 $1 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" "Vim" 0 0 # Helper Function from above
733   ReadRegStr $0 SHCTX "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$0" "DisplayIcon"
734   ${StrStrAdv} $0 $0 "\gvim.exe" ">" "<" "0" "0" "0" # get everything before "\gvim.exe"
735   ${If} $0 != ""
736     StrCpy $pathPrefix "$pathPrefix;$0"
737   ${EndIf}
738
739   # JabRef, Hard to locate, JabRef uses MSI installer, lets try to find it through the .bib extension
740   SetShellVarContext all
741   ${If} ${RunningX64}
742     SetRegView 64
743   ${EndIf}
744   ${For} $R1 0 1 # Try as admin and then as user
745     ReadRegStr $0 SHCTX "SOFTWARE\Classes\.bib" ""
746     ReadRegStr $0 SHCTX "Software\Classes\$0\shell\open\command" ""
747     ${StrRep} $0 $0 '"' "" # Remove quotes
748     ${StrStrAdv} $0 $0 "\JabRef.exe" ">" "<" "0" "0" "0" # Get everything before "\JabRef.exe"
749     ${If} ${FileExists} "$0\JabRef.exe"
750       StrCpy $pathPrefix "$pathPrefix;$0"
751       ${Break}
752     ${EndIf}
753     SetShellVarContext current # Retry as user
754   ${Next}
755
756   # Gnumeric
757   SetShellVarContext all
758   ${If} ${RunningX64}
759     SetRegView 64
760   ${EndIf}
761   ${For} $R1 0 1 # Try as admin and then as user
762     ReadRegStr $0 SHCTX "Software\Classes\Applications\gnumeric.exe\shell\Open\command" ""
763     ${StrRep} $0 $0 '"' "" # Remove quotes
764     ${StrStrAdv} $0 $0 "\gnumeric.exe" ">" "<" "0" "0" "0" # Get everything before "\gnumeric.exe"
765     ${If} $0 != ""
766       StrCpy $pathPrefix "$pathPrefix;$0"
767       ${Break}
768     ${EndIf}
769     SetShellVarContext current # Retry as user
770   ${Next}
771
772   # Pandoc, same as JabRef, can only attempt to ask cmd if added to PATH, the user can't disable that while installing pandoc though
773   nsExec::ExecToStack "where pandoc.exe"
774   Pop $1 # Exit code
775   Pop $0 # Return sring
776   ${If} $1 = 0
777     ${StrStrAdv} $0 $0 "\pandoc.exe" ">" "<" "0" "0" "0" # Get everything before "\pandoc.exe"
778     ${If} $0 != ""
779       StrCpy $pathPrefix "$pathPrefix;$0"
780     ${EndIf}
781   ${EndIf}
782
783   ${Map.Create} softwarePathsMap # Create a map (NSISList plugin) to temp store paths of 3rd party software
784
785   # LilyPond
786   ${SearchAllRegistry} $0 $1 $1 "SOFTWARE" "LilyPond" 0 0 # Helper Function from above
787   ReadRegStr $0 SHCTX "SOFTWARE\$0" "Install_Dir"
788   ${If} $0 != ""
789     StrCpy $pathPrefix "$pathPrefix;$0\usr\bin"
790     ${Map.Set} softwarePathsMap "LilyPond" "$0\usr\bin"
791   ${EndIf}
792
793   # Gimp
794   ${SearchAllRegistry} $2 $1 $1 "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" "GIMP" 0 0 # Helper Function from above
795   ReadRegStr $0 SHCTX "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$2" "InstallLocation"
796   ${If} $0 != ""
797     StrCpy $pathPrefix "$pathPrefix;$0bin" # Install Location ends with '\' for Gimp in Registry
798     ReadRegStr $0 SHCTX "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$2" "DisplayIcon"
799     ${StrStrAdv} $0 $0 ".exe" ">" "<" "0" "0" "0" # Get everything before the ".exe"
800     ${StrStrAdv} $0 $0 "\" "<" ">" "0" "0" "0" # Get everything after the last "\", $0 contains "gimp-10" now
801     ${Map.Set} softwarePathsMap "ImageEditor" $0
802   ${EndIf}
803
804   # Krita
805   ${SearchAllRegistry} $0 $1 $1 "SOFTWARE" "Krita" 0 0 # Helper Function from above
806   ReadRegStr $0 SHCTX "SOFTWARE\$0" "InstallLocation"
807   ${If} $0 != ""
808     StrCpy $pathPrefix "$pathPrefix;$0\bin"
809     ${Map.Set} softwarePathsMap "ImageEditor" "krita"
810   ${EndIf}
811
812   # Photoshop
813   SetShellVarContext all
814   ${If} ${RunningX64}
815     SetRegView 64
816   ${EndIf}
817   ${For} $R1 0 1 # Try as admin and then as user
818     ReadRegStr $0 SHCTX "Software\Classes\Applications\Photoshop.exe\shell\Open\command" ""
819     ${StrRep} $0 $0 '"' "" # Remove quotes
820     ${StrStrAdv} $0 $0 "\photoshop.exe" ">" "<" "0" "0" "0" # Get everything before "\photoshop.exe"
821     ${If} $0 != ""
822       StrCpy $pathPrefix "$pathPrefix;$0"
823       ${Break}
824     ${EndIf}
825     SetShellVarContext current # Retry as user
826   ${Next}
827
828   ClearErrors
829
830   FileOpen $R0 "$INSTDIR\Resources\lyxrc.dist" a
831   FileSeek $R0 0 END
832   # set some general things
833   FileWrite $R0 '\screen_zoom 120$\r$\n'
834
835   FileWrite $R0 '\path_prefix "$pathPrefix"$\r$\n'
836   
837   # use pdfview for all types of PDF files
838   FileWrite $R0 '\format "pdf5" "pdf" "PDF (LuaTeX)" "u" "pdfview" "" "document,vector,menu=export" "application/pdf"$\r$\n\
839                 \format "pdf4" "pdf" "PDF (XeTeX)" "X" "pdfview" "" "document,vector,menu=export" "application/pdf"$\r$\n\
840                 \format "pdf3" "pdf" "PDF (dvipdfm)" "m" "pdfview" "" "document,vector,menu=export" "application/pdf"$\r$\n\
841                 \format "pdf2" "pdf" "PDF (pdflatex)" "F" "pdfview" "" "document,vector,menu=export" "application/pdf"$\r$\n\
842                 \format "pdf" "pdf" "PDF (ps2pdf)" "P" "pdfview" "" "document,vector,menu=export" "application/pdf"$\r$\n'
843
844   # If krita or gimp was found previously
845   ${Map.Get} $0 softwarePathsMap "ImageEditor"
846   ${If} $0 != "__NULL" # If "ImageEditor" is set in map    
847     FileWrite $R0 '\format "gif" "gif" "GIF" "" "auto" "$0" "" "image/gif"$\r$\n\
848                   \format "jpg" "jpg, jpeg" "JPEG" "" "auto" "$0" "" "image/jpeg"$\r$\n\
849                   \format "png" "png" "PNG" "" "auto" "$0" "" "image/x-png"$\r$\n'
850   ${EndIf}
851   
852   FileWrite $R0 '\converter "wmf" "eps" "magick -density 300 $$$$i $$$$o" ""$\r$\n\
853     \converter "emf" "eps" "magick -density 300 $$$$i $$$$o" ""$\r$\n'
854
855   # If LilyPond was found previously
856   ${Map.Get} $0 softwarePathsMap "LilyPond"
857   ${If} $0 != "__NULL" # If "LilyPond" is set in map
858     FileWrite $R0 '\format "lilypond-book" "lytex" "LilyPond book (LaTeX)" "" "" "auto" "document,menu=export" ""$\r$\n\
859                   \converter "lilypond-book" "pdflatex" "python \"$0\\lilypond-book.py\" --safe --pdf --latex-program=pdflatex --lily-output-dir=ly-pdf $$$$i" ""$\r$\n\
860                   \converter "lilypond-book" "xetex" "python \"$0\\lilypond-book.py\" --safe --pdf --latex-program=xelatex --lily-output-dir=ly-pdf $$$$i" ""$\r$\n\
861                   \converter "lilypond-book" "luatex" "python \"$0\\lilypond-book.py\" --safe --pdf --latex-program=lualatex --lily-output-dir=ly-pdf $$$$i" ""$\r$\n\
862                   \converter "lilypond-book" "latex" "python \"$0\\lilypond-book.py\" --safe --lily-output-dir=ly-eps $$$$i" ""$\r$\n'
863   ${EndIf}
864
865   ${Map.Destroy} softwarePathsMap
866
867   FileClose $R0
868   
869   ${If} ${Errors}
870     MessageBox MB_OK|MB_ICONEXCLAMATION "$(ModifyingConfigureFailed)" /SD IDOK
871     ClearErrors
872   ${EndIf}
873 SectionEnd
874
875 Section "$(SecFileAssocTitle)" SecFileAssoc # Registry information to assiciate ".lyx" extension with this LyX installation
876   Call PrepareShellCTX # Helper function from above
877
878   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\App Paths\LyX.exe" "" "$INSTDIR\bin\LyX.exe" # Writes in both 64 and 32 bit registry
879   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\App Paths\LyX.exe" "Version" ${APP_VERSION} # Writes in both 64 and 32 bit registry
880   
881   WriteRegStr SHCTX "Software\Classes\.lyx" "" "LyX.Document"
882   WriteRegStr SHCTX "Software\Classes\.lyx" "Content Type" "application/lyx"
883
884   WriteRegStr SHCTX "Software\Classes\LyX.Document" "" "LyX Document"
885   WriteRegStr SHCTX "Software\Classes\LyX.Document\DefaultIcon" "" "$INSTDIR\bin\LyX.exe,1"
886   WriteRegStr SHCTX "Software\Classes\LyX.Document\Shell\open\command" "" '"$INSTDIR\bin\LyX.exe" "%1"'
887
888   # we need to update also the automatically created entry about the lyx.exe
889   # otherwise .lyx-files will could be opened with an older LyX version
890   ReadRegStr $0 SHCTX "Software\Classes\Applications\LyX.exe\shell\open\command" ""
891   ${if} $0 != "" # if something was found
892     WriteRegStr SHCTX "Software\Classes\Applications\LyX.exe\shell\open\command" "" '"$INSTDIR\bin\LyX.exe" "%1"'
893   ${endif}
894
895   WriteRegStr SHCTX "SOFTWARE\LyX" "FileAssoc${VERSION_MAJOR}${VERSION_MINOR}" $INSTDIR # Save user preference for over-installing
896 SectionEnd
897
898 Section "$(SecDesktopTitle)" SecDesktop
899   SetOutPath "$INSTDIR\bin" # Need to set this so that the shortcut to lyx.exe is executed in the bin folder
900   CreateShortCut "$DESKTOP\LyX ${VERSION_MAJOR}.${VERSION_MINOR}.lnk" "$INSTDIR\bin\LyX.exe" "" "" "" "" "" "LyX - The Document Processor" # Last parameter is the comment of the shortcut
901   Call PrepareShellCTX
902   WriteRegStr SHCTX "SOFTWARE\LyX" "Desktop${VERSION_MAJOR}${VERSION_MINOR}" $INSTDIR # Save user preference for over-installing
903 SectionEnd
904
905 Section -StartMenuFolder # Creates starm menu shortcut and website links to lyx website and lyx wiki
906   Call PrepareShellCTX # Helper function from above, need to call be before the following macro, because the macro writes registry string (storing the default startmenu folder)
907   !insertmacro MUI_STARTMENU_WRITE_BEGIN startmenuPage # This macro also assures that the folder is not empty for us
908     SetOutPath "$INSTDIR\bin" # Need to set this so that the shortcut to lyx.exe is executed in the bin folder
909     CreateDirectory "$SMPROGRAMS\$StartMenuFolder"
910     CreateShortCut "$SMPROGRAMS\$StartMenuFolder\LyX.lnk" "$INSTDIR\bin\LyX.exe" "" "" "" "" "" "LyX - The Document Processor" # Last parameter is the comment of the shortcut
911     SetOutPath $INSTDIR
912     CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Uninstall LyX.lnk" "$INSTDIR\Uninstall-LyX.exe"
913     WriteINIStr "$SMPROGRAMS\$StartMenuFolder\LyX Website.url" "InternetShortcut" "URL" "https://www.lyx.org/"
914     WriteINIStr "$SMPROGRAMS\$StartMenuFolder\LyX Wiki.url" "InternetShortcut" "URL" "https://wiki.lyx.org"
915   !insertmacro MUI_STARTMENU_WRITE_END
916 SectionEnd
917
918 !include "src\dictionaries.nsh"
919
920 Section -SoftwareRegistry # Registry information in "SOFTWARE" subkey
921   Call PrepareShellCTX # Helper function from above
922
923   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "latestVersion"
924   ${If} $0 S< ${APP_VERSION_DOTLESS}
925     WriteRegStr SHCTX "SOFTWARE\LyX" "latestVersion" ${APP_VERSION_DOTLESS}
926   ${EndIf}
927   WriteRegStr SHCTX "SOFTWARE\LyX\${APP_VERSION_DOTLESS}" "" $INSTDIR
928   WriteRegStr SHCTX "SOFTWARE\LyX\${APP_VERSION_DOTLESS}" "LaTeX Path" $LatexPath
929   WriteRegStr SHCTX "SOFTWARE\LyX" "InstDir${VERSION_MAJOR}${VERSION_MINOR}" $INSTDIR # Remember install dir for the next over-installment of the same series
930 SectionEnd
931
932 Section -UninstallInfoRegistry # Registry information in "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" subkey
933   Call PrepareShellCTX # Helper function from above
934
935   ${If} $MultiUser.InstallMode == "AllUsers"
936     WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "DisplayName" "LyX ${APP_VERSION}"
937   ${Else}
938     WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "DisplayName" "LyX ${APP_VERSION} $(TEXT_INSTALL_CURRENTUSER)"
939   ${EndIf}
940
941   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "UninstallString" '"$INSTDIR\Uninstall-LyX.exe"'
942   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "DisplayVersion" "${APP_VERSION}"
943   WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "VersionMajor" "${VERSION_MAJOR}"
944   WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "VersionMinor" "${VERSION_MINOR}"
945   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "DisplayIcon" "$INSTDIR\bin\LyX.exe"
946   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "URLUpdateInfo" "https://www.lyx.org/"
947   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "URLInfoAbout" "https://www.lyx.org/AboutLyX"
948   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "Publisher" "LyX Team"
949   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "HelpLink" "https://www.lyx.org/MailingLists"
950   WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "NoModify" 0x00000001
951   WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "NoRepair" 0x00000001
952   WriteRegStr SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "InstallLocation" "$INSTDIR"
953
954   ${GetSize} "$INSTDIR" "/S=0K" $0 $1 $2
955   IntFmt $0 "0x%08X" $0
956   WriteRegDWORD SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}" "EstimatedSize" $0
957 SectionEnd
958
959 !if ${VERSION_MINOR} > 0 # Only valid for minor releases of the same major release (e.g. not valid for 3.0)
960   Section -CopyOldPreferences # Searches user's preferences (userdir) from previous minor release and copies it if necessary
961     Call PrepareShellCTX
962     SetShellVarContext current # Otherwise $APPDATA would return C:\ProgrammData instead of C:\Users\username\AppData\Roaming
963     
964     IntOp $0 ${VERSION_MINOR} - 1 # only check the direct previous minor release
965     ${If} ${FileExists} "$APPDATA\LyX${VERSION_MAJOR}.$0\lyxrc.defaults"
966     ${AndIfNot} ${FileExists} "$APPDATA\LyX${VERSION_MAJOR}.${VERSION_MINOR}\lyxrc.defaults"
967       CopyFiles /SILENT "$APPDATA\LyX${VERSION_MAJOR}.$0\*.*" "$APPDATA\LyX${VERSION_MAJOR}.${VERSION_MINOR}"
968     ${EndIf}
969   SectionEnd
970 !endif
971
972 Section -ConfigureScript # Runs the configure.py script
973
974   # Manipulate PATH environment of the running installer process, so that configure.py can find all the stuff needed
975   ReadEnvStr $0 "PATH"
976   ${StrRep} $pathPrefix $pathPrefix "$$LyXDir" "$INSTDIR"
977   System::Call 'Kernel32::SetEnvironmentVariable(t "PATH", t "$pathPrefix;$0")'
978   
979   Call PrepareShellCTX
980   SetShellVarContext current # Otherwise $APPDATA would return C:\ProgrammData instead of C:\Users\username\AppData\Roaming
981   SetOutPath "$APPDATA\LyX${VERSION_MAJOR}.${VERSION_MINOR}" # Need to run configure from the user dir, because it creates .lst files and some folders.
982
983   ${StrStr} $0 $LatexPath "\miktex\bin"
984   # R9 is used in $(TEXT_CONFIGURE_LYX)
985   ${If} $0 == "" # TexLive
986     StrCpy $R9 "TeXLive"
987   ${Else}
988     StrCpy $R9 "MiKTeX"
989   ${EndIf}
990   StrCpy $LaTeXInstalled $R9 # FIXME remove
991   DetailPrint $(TEXT_CONFIGURE_LYX) # Uses R9 to display the name of the installed latex distribution
992   
993   nsExec::ExecToLog '"$INSTDIR\Python\python.exe" "$INSTDIR\Resources\configure.py"'
994   Pop $0 # Return value
995 SectionEnd
996
997 Function DefaultDesktopFileAssoc # Custom function, called before the components page, reads the states of SecDesktop and SecFileAssoc from registry (if overinstalling) and sets them
998
999   Call CheckDictionaries # Search for already installed dictionaries, in dictionaries.nsh
1000
1001   ${IfNot} ${FileExists} "$INSTDIR\Uninstall-LyX.exe"
1002     Return # Not over-installing
1003   ${EndIf}
1004
1005   Call PrepareShellCTX
1006   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "FileAssoc${VERSION_MAJOR}${VERSION_MINOR}"
1007   ${If} $0 == ""
1008     SectionSetFlags ${SecFileAssoc} 0 # 0 means unselected
1009   ${EndIf}
1010   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "Desktop${VERSION_MAJOR}${VERSION_MINOR}"
1011   ${If} $0 == ""
1012     SectionSetFlags ${SecDesktop} 0 # 0 means unselected
1013   ${EndIf}
1014 FunctionEnd
1015
1016 Function StartLyX
1017   ShellExecAsUser::ShellExecAsUser "" "$INSTDIR\bin\LyX.exe"
1018 FunctionEnd
1019
1020 # Installer Section Descriptions
1021
1022 !insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
1023   !insertmacro MUI_DESCRIPTION_TEXT ${SecFileAssoc} "$(SecFileAssocDescription)"
1024   !insertmacro MUI_DESCRIPTION_TEXT ${SecDesktop} "$(SecDesktopDescription)"
1025   !insertmacro MUI_DESCRIPTION_TEXT ${SecDictionaries} "$(SecDictionariesDescription)" # In dictionaries.nsh
1026   !insertmacro MUI_DESCRIPTION_TEXT ${SecThesaurus} "$(SecThesaurusDescription)" # In dictionaries.nsh
1027 !insertmacro MUI_FUNCTION_DESCRIPTION_END
1028
1029 # ===== Uninstaller ======
1030
1031 Function un.onInit # Callback function, called when the uninstaller initializes
1032   # Check that LyX is not currently running
1033   ${If} ${RunningX64}
1034     ${DisableX64FSRedirection} # We need the following process to be 64 bit on 64 bit system
1035   ${EndIf}
1036   nsExec::ExecToStack "powershell (Get-Process LyX).Path"
1037   Pop $0 # Exit code
1038   Pop $0 # Result string
1039   ${If} ${RunningX64}
1040     ${EnableX64FSRedirection} # Need to be enabled asap or installer might crash
1041   ${EndIf}
1042   ${UnStrStr} $0 $0 "$INSTDIR\bin\LyX.exe"
1043   ${If} $0 != ""
1044     MessageBox MB_OK|MB_ICONSTOP "$(UnInstallRunning)" /SD IDOK
1045     Quit # Quit uninstaller
1046   ${EndIf}
1047
1048   Call un.PrepareShellCTX
1049   !insertmacro MULTIUSER_UNINIT
1050
1051   # Ascertain whether the user has sufficient privileges to uninstall.
1052   # abort when LyX was installed with admin permissions but the user doesn't have administrator privileges
1053   Call un.PrepareShellCTX # Helperfunction from above
1054   ReadRegStr $0 HKLM "SOFTWARE\LyX\${APP_VERSION_DOTLESS}" ""
1055   ${If} $0 == $INSTDIR
1056     ${If} $MultiUser.Privileges != "Admin"
1057     ${AndIf} $MultiUser.Privileges != "Power"
1058       MessageBox MB_OK|MB_ICONSTOP "$(UnNotAdminLabel)" /SD IDOK
1059       Abort
1060     ${Else}
1061       StrCpy $MultiUser.InstallMode "AllUsers"
1062     ${EndIf}
1063   ${Else}
1064     ReadRegStr $0 HKCU "SOFTWARE\LyX\${APP_VERSION_DOTLESS}" ""
1065     ${If} $0 == ""
1066       MessageBox MB_OK|MB_ICONEXCLAMATION "$(UnNotInRegistryLabel)" /SD IDOK
1067     ${Else}
1068       StrCpy $MultiUser.InstallMode "CurrentUser"
1069     ${EndIf}
1070   ${EndIf}
1071
1072   # question message if the user really wants to uninstall LyX
1073   ${IfCmd} MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "$(UnReallyRemoveLabel)" /SD IDYES IDNO ${||} Quit ${|}
1074 FunctionEnd
1075
1076 Function un.ComponentsPre # Callback Function, called before the components page is loaded
1077   # R9 and R8 are used in $(SecUnPreferencesDescription)
1078   SetShellVarContext current
1079   ${UnStrStrAdv} $R9 $PROFILE "\" "<" "<" "0" "0" "0" # Get everything before the last "\" of $PROFILE ("C:\Users\username") -> $R9: "C:\Users"
1080   StrCpy $AppPre $R9 # FIXME remove
1081   ${UnStrRep} $R8 $APPDATA "$PROFILE\" "" # "C:\Users\username\" gets replaced with "" in "C:\Users\username\AppData\Roaming" -> $R8: "Appdata\Roaming"
1082   StrCpy $AppSuff $R8 # FIXME remove
1083 FunctionEnd
1084
1085 Section "un.LyX" un.SecUnProgramFiles # Deletes files in $INSTDIR and registry entries
1086   SectionIn RO
1087   # LaTeX class files that were installed together with LyX
1088   # will not be uninstalled because other LyX versions will
1089   # need them and these few files don't harm to stay in LaTeX 
1090
1091   Call un.PrepareShellCTX # Helperfunction from above
1092
1093   # Subfolders and unistaller
1094   RMDir /r "$INSTDIR\bin"
1095   RMDir /r "$INSTDIR\Resources"
1096   RMDir /r "$INSTDIR\Python"
1097   RMDir /r "$INSTDIR\imagemagick"
1098   RMDir /r "$INSTDIR\ghostscript"
1099   Delete "$INSTDIR\Uninstall-LyX.exe"
1100   # Install directory
1101   SetOutPath $PLUGINSDIR
1102   RMDir $INSTDIR
1103
1104   # Startmenu
1105   !insertmacro MUI_STARTMENU_GETFOLDER startmenuPage $StartMenuFolder # Reads registry value written with macro MUI_STARTMENU_WRITE_BEGIN
1106   ShellLink::GetShortCutTarget "$SMPROGRAMS\$StartMenuFolder\LyX.lnk" # Check if the shortcut really is pointing to the current installation
1107   Pop $0
1108   ${IfNot} ${FileExists} $0 # If still exists, then belongs to another lyx installment, so don't delete
1109     RMDir /r "$SMPROGRAMS\$StartMenuFolder"
1110     DeleteRegValue SHCTX "SOFTWARE\LyX" "SMDir${VERSION_MAJOR}${VERSION_MINOR}"
1111   ${EndIf}
1112
1113   # Desktop icon
1114   ShellLink::GetShortCutTarget "$DESKTOP\LyX ${VERSION_MAJOR}.${VERSION_MINOR}.lnk"
1115   Pop $0
1116   ${UnStrStr} $0 $0 $INSTDIR
1117   ${If} $0 != ""
1118     Delete "$DESKTOP\LyX ${VERSION_MAJOR}.${VERSION_MINOR}.lnk"
1119   ${EndIf}
1120   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "Desktop${VERSION_MAJOR}${VERSION_MINOR}"
1121   ${If} $0 == $INSTDIR
1122     DeleteRegValue SHCTX "SOFTWARE\LyX" "Desktop${VERSION_MAJOR}${VERSION_MINOR}"
1123   ${EndIf}
1124
1125   # File association
1126   ReadRegStr $0 SHCTX "Software\Classes\LyX.Document\DefaultIcon" ""
1127   ${UnStrStr} $0 $0 $INSTDIR
1128   ${If} $0 != ""
1129     DeleteRegKey SHCTX "Software\Classes\LyX.Document"
1130     DeleteRegKey SHCTX "Software\Classes\.lyx"
1131   ${EndIf}
1132   ReadRegStr $0 SHCTX "Software\Microsoft\Windows\CurrentVersion\App Paths\LyX.exe" ""
1133   ${UnStrStr} $0 $0 $INSTDIR
1134   ${If} $0 != ""
1135     DeleteRegKey SHCTX "Software\Microsoft\Windows\CurrentVersion\App Paths\LyX.exe" # Deletes key in both 64 and 32 bit registry
1136   ${EndIf}
1137   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "FileAssoc${VERSION_MAJOR}${VERSION_MINOR}"
1138   ${If} $0 == $INSTDIR
1139     DeleteRegValue SHCTX "SOFTWARE\LyX" "FileAssoc${VERSION_MAJOR}${VERSION_MINOR}"
1140   ${EndIf}
1141   DeleteRegKey SHCTX "Software\Classes\Applications\LyX.exe"
1142
1143   # Uninstaller info registry
1144   DeleteRegKey SHCTX "Software\Microsoft\Windows\CurrentVersion\Uninstall\LyX${APP_VERSION_DOTLESS}"
1145
1146   # Software registry
1147   DeleteRegKey SHCTX "SOFTWARE\LyX\${APP_VERSION_DOTLESS}"
1148   DeleteRegValue SHCTX "SOFTWARE\LyX" "latestVersion"
1149   ReadRegStr $0 SHCTX "SOFTWARE\LyX" "InstDir${VERSION_MAJOR}${VERSION_MINOR}"
1150   ${If} $0 == $INSTDIR
1151     DeleteRegValue SHCTX "SOFTWARE\LyX" "InstDir${VERSION_MAJOR}${VERSION_MINOR}"
1152   ${EndIf}
1153   DeleteRegValue SHCTX "SOFTWARE\LyX" "latestVersion"
1154   # Now that "latestVersion" is gone we need to find if there is another (newest) LyX version installed and write it's verion to "latestVersion"
1155   # The keys in the registry are sorted alphabetically, so we dont need to compare, the latest found will be the newest version
1156   StrCpy $0 0
1157   StrCpy $1 ""
1158   ${Do}
1159     StrCpy $2 $1
1160     EnumRegKey $2 SHCTX "SOFTWARE\LyX" $0
1161     IntOp $0 $0 + 1
1162   ${LoopUntil} $1 == ""
1163   ${If} $2 != ""
1164     WriteRegStr SHCTX "SOFTWARE\LyX" "latestVersion" $2
1165   ${EndIf}
1166   DeleteRegKey /ifempty SHCTX "SOFTWARE\LyX"
1167 SectionEnd
1168
1169 Section /o "un.$(UnLyXPreferencesTitle)" un.SecUnPreferences # Deletes user preferences folders for all users (if admin) and registry entry made by LyX to HKCU
1170   Call un.PrepareShellCTX
1171   SetShellVarContext current
1172   # If installed as user, only delete folder and registry key of current user
1173   ${If} $MultiUser.InstallMode == "CurrentUser"
1174     RMDir /r "$APPDATA\LyX${VERSION_MAJOR}.${VERSION_MINOR}"
1175
1176   ${Else} # If installed as admin, things are a little more complicated
1177     ${UnStrStrAdv} $2 $PROFILE "\" "<" ">" "0" "0" "0" # Get everything after the last "\" of $PROFILE ("C:\Users\username") -> $2: "username"
1178     ${UnStrStrAdv} $3 $PROFILE "\" "<" "<" "0" "0" "0" # Get everything before the last "\" of $PROFILE ("C:\Users\username") -> $3: "C:\Users"
1179     ClearErrors
1180     FindFirst $1 $0 "$3\*" # Find any folder/file in "C:\Users"
1181     ${DoUntil} ${Errors} # Errors are set while FindFirst/FindNext if the search is completed (there are no more files)
1182       ${UnStrRep} $0 $APPDATA $2 $0 # Replace "username" in $APPDATA ("C:\Users\username\AppData\Roaming") with what was found
1183       ${If} ${FileExists} "$0\LyX${VERSION_MAJOR}.${VERSION_MINOR}\*" # Check if anything exists in the LyX userdir
1184         RMDir /r "$0\LyX${VERSION_MAJOR}.${VERSION_MINOR}"
1185       ${EndIf}
1186       FindNext $1 $0
1187     ${Loop}
1188     FindClose $1
1189   ${EndIf}
1190
1191   # Can only delete for current user, because Admin can't access registry of other users
1192   DeleteRegKey HKCU "Software\LyX\LyX${VERSION_MAJOR}.${VERSION_MINOR}"
1193   DeleteRegKey /ifempty HKCU "Software\LyX"
1194 SectionEnd
1195
1196 # Uninstaller Section Descriptions
1197
1198 !insertmacro MUI_UNFUNCTION_DESCRIPTION_BEGIN
1199   !insertmacro MUI_DESCRIPTION_TEXT ${un.SecUnProgramFiles} "$(SecUnProgramFilesDescription)"
1200   !insertmacro MUI_DESCRIPTION_TEXT ${un.SecUnPreferences} "$(SecUnPreferencesDescription)"
1201 !insertmacro MUI_UNFUNCTION_DESCRIPTION_END