* support/scripts/sdcc.nsi: cross compiling of WIN32 setup.exe on Linux
[fw/sdcc] / support / scripts / sdcc.nsi
1 # sdcc.nsi - NSIS installer script for SDCC
2 #
3 # Copyright (c) 2003,2004 Borut Razem
4 #
5 # This file is part of sdcc.
6 #
7 #  This software is provided 'as-is', without any express or implied
8 #  warranty.  In no event will the authors be held liable for any damages
9 #  arising from the use of this software.
10 #
11 #  Permission is granted to anyone to use this software for any purpose,
12 #  including commercial applications, and to alter it and redistribute it
13 #  freely, subject to the following restrictions:
14 #
15 #  1. The origin of this software must not be misrepresented; you must not
16 #     claim that you wrote the original software. If you use this software
17 #     in a product, an acknowledgment in the product documentation would be
18 #     appreciated but is not required.
19 #  2. Altered source versions must be plainly marked as such, and must not be
20 #     misrepresented as being the original software.
21 #  3. This notice may not be removed or altered from any source distribution.
22 #
23 #  Borut Razem
24 #  borut.razem@siol.net
25
26 # How to create WIN32 setup.exe
27 #
28 # - unpack WIN32 mingw daily snapshot sdcc-snapshot-i586-mingw32msvc-yyyymmdd.zip
29 #   to a clean directory (the option to create directories should be enambled).
30 #   A sub directory sdcc is created (referenced as PKGDIR in continuation).
31 # - copy files sdcc/support/scripts/sdcc.ico and sdcc/support/scripts/sdcc.nsi
32 #   (this file) from the sdcc CVS snapshot to the PKGDIR directory
33 # - copy file COPYING from the sdcc CVS snapshot to the PKGDIR directory,
34 #   rename it to COPYING.TXT and convert file COPYING to DOS format:
35 #   unix2dos COPYING.TXT
36 # - run NSIS installer from PKGDIR directory:
37 #   "c:\Program Files\NSIS\makensis.exe" sdcc.nsi
38 # - A setup file setup.exe is created in PKGDIR directory.
39 #   Rename it to sdcc_yyyymmdd_setup.exe and upload it
40 #   to sdcc download repository at sourceforge.net
41 #
42 #
43 # How to upload secc setup.exe tosourceforge.net
44 #
45 # Execute following commands from the cmd prompt:
46 # - sftp sdcc.sourceforge.net
47 # - cd /home/groups/s/sd/sdcc/htdocs/snapshots/i586-mingw32msvc-setup
48 # - put sdcc_yyyymmdd_setup.exe
49 # - quit
50
51
52 !include "MUI.nsh"
53
54 SetCompressor lzma
55
56 !define SDCC_ROOT "."
57
58 !define FROM_SNAPSHOT
59
60 !ifdef FROM_SNAPSHOT
61 !system "dir ${SDCC_ROOT}\device > NUL" <> 0
62 !define DEV_ROOT "${SDCC_ROOT}"
63 !else
64 !system "dir ${SDCC_ROOT}\device > NUL" = 0
65 !define DEV_ROOT "${SDCC_ROOT}\device"
66 !endif
67
68 !system "unix2dos ${SDCC_ROOT}\doc\ChangeLog_head.txt" = 0
69 !system "unix2dos ${SDCC_ROOT}\doc\README.TXT" = 0
70
71 InstType "Full (Bin, Doc, Lib, Src)"
72 InstType "Medium (Bin, Doc, Lib)"
73 InstType "Compact (Bin, Doc)"
74
75 ;--------------------------------
76 ;Variables
77
78 Var MUI_STARTMENUPAGE_VARIABLE
79
80 ;--------------------------------
81 !define MUI_ABORTWARNING
82
83 !insertmacro MUI_PAGE_WELCOME
84 !insertmacro MUI_PAGE_LICENSE "${SDCC_ROOT}\COPYING.TXT"
85 !define MUI_STARTMENUPAGE_DEFAULTFOLDER "SDCC"
86 !define MUI_STARTMENUPAGE_REGISTRY_ROOT "HKLM"
87 !define MUI_STARTMENUPAGE_REGISTRY_KEY "Software\Microsoft\Windows\CurrentVersion\Uninstall\SDCC"
88 !define MUI_STARTMENUPAGE_REGISTRY_VALUENAME "NSIS:StartMenuDir"
89 !define MUI_STARTMENUPAGE_NODISABLE
90 !insertmacro MUI_PAGE_STARTMENU Application $MUI_STARTMENUPAGE_VARIABLE
91 !insertmacro MUI_PAGE_COMPONENTS
92 !insertmacro MUI_PAGE_DIRECTORY
93 !insertmacro MUI_PAGE_INSTFILES
94 !insertmacro MUI_PAGE_FINISH
95
96 !insertmacro MUI_UNPAGE_INSTFILES
97
98 !insertmacro MUI_LANGUAGE "English"
99
100 Name "SDCC"
101 BrandingText ""
102 OutFile "setup.exe"
103 InstallDir "$PROGRAMFILES\SDCC"
104 ShowInstDetails show
105 ShowUnInstDetails show
106
107 Function .onInit
108   ;Uninstall the old version, if present
109   ReadRegStr $R0 HKLM \
110   "Software\Microsoft\Windows\CurrentVersion\Uninstall\$(^Name)" \
111   "UninstallString"
112   StrCmp $R0 "" inst
113
114   MessageBox MB_OKCANCEL|MB_ICONEXCLAMATION \
115   "$(^Name) is already installed. $\n$\nClick 'OK' to remove the \
116   previous version or 'Cancel' to cancel this upgrade." \
117   IDOK uninst
118   Abort
119
120 uninst:
121   ; Run the uninstaller
122   ClearErrors
123   ExecWait '$R0 _?=$INSTDIR' ;Do not copy the uninstaller to a temp file
124
125   ;IfErrors no_remove_uninstaller
126   ;  ;You can either use Delete /REBOOTOK in the uninstaller or add some code
127   ;  ;here to remove to remove the uninstaller. Use a registry key to check
128   ;  ;whether the user has chosen to uninstall. If you are using an uninstaller
129   ;  ;components page, make sure all sections are uninstalled.
130   ;no_remove_uninstaller:
131
132   Goto done
133 inst:
134
135   ; Install the new version
136   MessageBox MB_YESNO|MB_ICONQUESTION "This will install $(^Name). Do you wish to continue?" IDYES +2
137   Abort
138
139 done:
140 FunctionEnd
141
142 Section -Common
143   SetOutPath "$INSTDIR"
144   File ".\sdcc.ico"
145   File "${SDCC_ROOT}\COPYING.TXT"
146 SectionEnd
147
148 Section "SDCC application files"
149   SectionIn 1 2 3 RO
150   SetOutPath "$INSTDIR\bin"
151   File "${SDCC_ROOT}\bin\*.exe"
152 SectionEnd
153
154 Section "SDCC documentation"
155   SectionIn 1 2 3
156   SetOutPath "$INSTDIR\doc"
157   File "${SDCC_ROOT}\doc\ChangeLog_head.txt"
158   File "${SDCC_ROOT}\doc\README.TXT"
159 SectionEnd
160
161 Section "SDCC include files"
162   SectionIn 1 2
163   SetOutPath "$INSTDIR\include\asm\default"
164   File "${DEV_ROOT}\include\asm\default\features.h"
165   SetOutPath "$INSTDIR\include\asm\gbz80"
166   File "${DEV_ROOT}\include\asm\gbz80\features.h"
167   SetOutPath "$INSTDIR\include\asm\z80"
168   File "${DEV_ROOT}\include\asm\z80\features.h"
169   SetOutPath "$INSTDIR\include\pic16"
170   File "${DEV_ROOT}\include\pic16\*.h"
171   SetOutPath "$INSTDIR\include"
172   File "${DEV_ROOT}\include\*.h"
173 SectionEnd
174
175 Section "SDCC DS390 library"
176   SectionIn 1 2
177   SetOutPath "$INSTDIR\lib\ds390"
178   File "${DEV_ROOT}\lib\ds390\*.asm"
179   File "${DEV_ROOT}\lib\ds390\*.rel"
180   File "${DEV_ROOT}\lib\ds390\*.lib"
181 SectionEnd
182
183 Section "SDCC DS400 library"
184   SectionIn 1 2
185   SetOutPath "$INSTDIR\lib\ds400"
186   File "${DEV_ROOT}\lib\ds400\*.asm"
187   File "${DEV_ROOT}\lib\ds400\*.rel"
188   File "${DEV_ROOT}\lib\ds400\*.lib"
189 SectionEnd
190
191 Section "SDCC GBZ80 library"
192   SectionIn 1 2
193   SetOutPath "$INSTDIR\lib\gbz80"
194   File "${DEV_ROOT}\lib\gbz80\*.asm"
195   File "${DEV_ROOT}\lib\gbz80\*.o"
196   File "${DEV_ROOT}\lib\gbz80\*.lib"
197 SectionEnd
198
199 Section "SDCC Z80 library"
200   SectionIn 1 2
201   SetOutPath "$INSTDIR\lib\z80"
202   File "${DEV_ROOT}\lib\z80\*.asm"
203   File "${DEV_ROOT}\lib\z80\*.o"
204   File "${DEV_ROOT}\lib\z80\*.lib"
205 SectionEnd
206
207 Section "SDCC large model library"
208   SectionIn 1 2
209   SetOutPath "$INSTDIR\lib\large"
210   File "${DEV_ROOT}\lib\large\*.asm"
211   File "${DEV_ROOT}\lib\large\*.rel"
212   File "${DEV_ROOT}\lib\large\*.lib"
213 SectionEnd
214
215 Section "SDCC small model library"
216   SectionIn 1 2
217   SetOutPath "$INSTDIR\lib\small"
218   File "${DEV_ROOT}\lib\small\*.asm"
219   File "${DEV_ROOT}\lib\small\*.rel"
220   File "${DEV_ROOT}\lib\small\*.lib"
221 SectionEnd
222
223 Section "SDCC HC08 library"
224   SectionIn 1 2
225   SetOutPath "$INSTDIR\lib\hc08"
226   File "${DEV_ROOT}\lib\hc08\*.asm"
227   File "${DEV_ROOT}\lib\hc08\*.rel"
228   File "${DEV_ROOT}\lib\hc08\*.lib"
229 SectionEnd
230
231 Section "SDCC library sources"
232   SectionIn 1
233   SetOutPath "$INSTDIR\lib\src\ds390\examples"
234   File "${DEV_ROOT}\lib\src\ds390\examples\MOVED"
235
236   SetOutPath "$INSTDIR\lib\src\ds390"
237   File "${DEV_ROOT}\lib\src\ds390\*.c"
238   File "${DEV_ROOT}\lib\src\ds390\libds390.lib"
239   File "${DEV_ROOT}\lib\src\ds390\Makefile"
240   File "${DEV_ROOT}\lib\src\ds390\Makefile.dep"
241
242   SetOutPath "$INSTDIR\lib\src\ds400"
243   File "${DEV_ROOT}\lib\src\ds400\*.c"
244   File "${DEV_ROOT}\lib\src\ds400\libds400.lib"
245   File "${DEV_ROOT}\lib\src\ds400\Makefile"
246   File "${DEV_ROOT}\lib\src\ds400\Makefile.dep"
247
248   SetOutPath "$INSTDIR\lib\src\gbz80"
249   File "${DEV_ROOT}\lib\src\gbz80\*.c"
250   File "${DEV_ROOT}\lib\src\gbz80\*.s"
251   File "${DEV_ROOT}\lib\src\gbz80\gbz80.lib"
252   File "${DEV_ROOT}\lib\src\gbz80\Makefile"
253   File "${DEV_ROOT}\lib\src\gbz80\README"
254
255   SetOutPath "$INSTDIR\lib\src\z80"
256   File "${DEV_ROOT}\lib\src\z80\*.c"
257   File "${DEV_ROOT}\lib\src\z80\*.s"
258   File "${DEV_ROOT}\lib\src\z80\Makefile"
259   File "${DEV_ROOT}\lib\src\z80\README"
260   File "${DEV_ROOT}\lib\src\z80\z80.lib"
261
262   SetOutPath "$INSTDIR\lib\src\hc08"
263   File "${DEV_ROOT}\lib\src\hc08\*.c"
264   File "${DEV_ROOT}\lib\src\hc08\Makefile"
265   File "${DEV_ROOT}\lib\src\hc08\hc08.lib"
266
267   SetOutPath "$INSTDIR\lib\src"
268   File "${DEV_ROOT}\lib\src\*.c"
269 SectionEnd
270
271 Section -Icons
272 !insertmacro MUI_STARTMENU_WRITE_BEGIN Application
273   CreateDirectory "$SMPROGRAMS\$MUI_STARTMENUPAGE_VARIABLE"
274   CreateShortCut "$SMPROGRAMS\$MUI_STARTMENUPAGE_VARIABLE\SDCC on the Web.lnk" "$INSTDIR\sdcc.url" 
275   CreateShortCut "$SMPROGRAMS\$MUI_STARTMENUPAGE_VARIABLE\Uninstall SDCC.lnk" "$INSTDIR\uninstall.exe" 
276   CreateShortCut "$SMPROGRAMS\$MUI_STARTMENUPAGE_VARIABLE\Documentation.lnk" "$INSTDIR\doc\README.TXT" "" "$INSTDIR\sdcc.ico" "" "" "" ""
277   CreateShortCut "$SMPROGRAMS\$MUI_STARTMENUPAGE_VARIABLE\Change Log.lnk" "$INSTDIR\doc\ChangeLog_head.txt" "" "$INSTDIR\sdcc.ico" "" "" "" ""
278   CreateShortCut "$SMPROGRAMS\$MUI_STARTMENUPAGE_VARIABLE\GPL 2 License.lnk" "$INSTDIR\COPYING.TXT" 
279 !insertmacro MUI_STARTMENU_WRITE_END
280 SectionEnd
281
282 Section -INI
283   WriteIniStr "$INSTDIR\sdcc.url" "InternetShortcut" "URL" "http://sdcc.sourceforge.net/"
284 SectionEnd
285
286 Section -PostInstall
287   WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SDCC" "DisplayName" "SDCC"
288   WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SDCC" "UninstallString" "$INSTDIR\uninstall.exe"
289   WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SDCC" "Publisher" "sdcc.sourceforge.net"
290   WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SDCC" "URLInfoAbout" "http://sdcc.sourceforge.net/"
291   WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SDCC" "HelpLink" "http://sdcc.sourceforge.net/"
292   WriteRegStr HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SDCC" "URLUpdateInfo" "http://sdcc.sourceforge.net/"
293   WriteUninstaller "$INSTDIR\uninstall.exe"
294 SectionEnd
295
296 Section -AddToPath
297   MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Do you want to add $INSTDIR\bin to the PATH?" IDNO +3
298     Push "$INSTDIR\bin"
299     Call AddToPath
300 SectionEnd
301
302
303 ;;;; Uninstaller code ;;;;
304
305 Function un.onInit
306   MessageBox MB_ICONQUESTION|MB_YESNO|MB_DEFBUTTON2 "Are you sure you want to completely remove $(^Name) and all of its components?" IDYES +2
307   Abort
308 FunctionEnd
309
310 Function un.onUninstSuccess
311   HideWindow
312   MessageBox MB_ICONINFORMATION|MB_OK "$(^Name) was successfully removed from your computer."
313 FunctionEnd
314
315 Section Uninstall
316   !insertmacro MUI_STARTMENU_GETFOLDER Application $MUI_STARTMENUPAGE_VARIABLE
317
318   Delete "$INSTDIR\sdcc.url"
319
320   DeleteRegValue HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SDCC" "NSIS:StartMenuDir"
321   DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\SDCC"
322
323   Delete "$INSTDIR\uninstall.exe"
324   Delete "$SMPROGRAMS\$MUI_STARTMENUPAGE_VARIABLE\GPL 2 License.lnk"
325   Delete "$SMPROGRAMS\$MUI_STARTMENUPAGE_VARIABLE\Change Log.lnk"
326   Delete "$SMPROGRAMS\$MUI_STARTMENUPAGE_VARIABLE\Documentation.lnk"
327   Delete "$SMPROGRAMS\$MUI_STARTMENUPAGE_VARIABLE\Uninstall SDCC.lnk"
328   Delete "$SMPROGRAMS\$MUI_STARTMENUPAGE_VARIABLE\SDCC on the Web.lnk"
329
330   Delete "$INSTDIR\lib\src\hc08\*.c"
331   Delete "$INSTDIR\lib\src\hc08\hc08.lib"
332   Delete "$INSTDIR\lib\src\hc08\Makefile"
333
334   Delete "$INSTDIR\lib\src\z80\*.c"
335   Delete "$INSTDIR\lib\src\z80\*.s"
336   Delete "$INSTDIR\lib\src\z80\z80.lib"
337   Delete "$INSTDIR\lib\src\z80\README"
338   Delete "$INSTDIR\lib\src\z80\Makefile"
339
340   Delete "$INSTDIR\lib\src\gbz80\*.c"
341   Delete "$INSTDIR\lib\src\gbz80\*.s"
342   Delete "$INSTDIR\lib\src\gbz80\gbz80.lib"
343   Delete "$INSTDIR\lib\src\gbz80\README"
344   Delete "$INSTDIR\lib\src\gbz80\Makefile"
345
346   Delete "$INSTDIR\lib\src\ds390\*.c"
347   Delete "$INSTDIR\lib\src\ds390\libds390.lib"
348   Delete "$INSTDIR\lib\src\ds390\Makefile.dep"
349   Delete "$INSTDIR\lib\src\ds390\Makefile"
350   Delete "$INSTDIR\lib\src\ds390\examples\MOVED"
351
352   Delete "$INSTDIR\lib\src\ds400\*.c"
353   Delete "$INSTDIR\lib\src\ds400\libds400.lib"
354   Delete "$INSTDIR\lib\src\ds400\Makefile.dep"
355   Delete "$INSTDIR\lib\src\ds400\Makefile"
356
357   Delete "$INSTDIR\lib\src\*.c"
358
359   Delete "$INSTDIR\lib\hc08\*.rel"
360   Delete "$INSTDIR\lib\hc08\*.asm"
361   Delete "$INSTDIR\lib\hc08\*.lib"
362
363   Delete "$INSTDIR\lib\z80\*.o"
364   Delete "$INSTDIR\lib\z80\*.asm"
365   Delete "$INSTDIR\lib\z80\*.lib"
366
367   Delete "$INSTDIR\lib\small\*.asm"
368   Delete "$INSTDIR\lib\small\*.rel"
369   Delete "$INSTDIR\lib\small\*.lib"
370
371   Delete "$INSTDIR\lib\large\*.asm"
372   Delete "$INSTDIR\lib\large\*.rel"
373   Delete "$INSTDIR\lib\large\*.lib"
374
375   Delete "$INSTDIR\lib\gbz80\*.asm"
376   Delete "$INSTDIR\lib\gbz80\*.o"
377   Delete "$INSTDIR\lib\gbz80\*.lib"
378
379   Delete "$INSTDIR\lib\ds390\*.asm"
380   Delete "$INSTDIR\lib\ds390\*.rel"
381   Delete "$INSTDIR\lib\ds390\*.lib"
382
383   Delete "$INSTDIR\lib\ds400\*.asm"
384   Delete "$INSTDIR\lib\ds400\*.rel"
385   Delete "$INSTDIR\lib\ds400\*.lib"
386
387   Delete "$INSTDIR\include\asm\z80\*.h"
388   Delete "$INSTDIR\include\asm\gbz80\*.h"
389   Delete "$INSTDIR\include\asm\default\*.h"
390   Delete "$INSTDIR\include\pic16\*.h"
391   Delete "$INSTDIR\include\*.h"
392
393   Delete "$INSTDIR\doc\README.TXT"
394   Delete "$INSTDIR\doc\ChangeLog_head.txt"
395
396   Delete "$INSTDIR\bin\*.exe"
397
398   Delete "$INSTDIR\COPYING.TXT"
399   Delete "$INSTDIR\sdcc.ico"
400
401   RMDir "$SMPROGRAMS\$MUI_STARTMENUPAGE_VARIABLE"
402
403   RMDir "$INSTDIR\lib\src\z80"
404   RMDir "$INSTDIR\lib\src\gbz80"
405   RMDir "$INSTDIR\lib\src\ds390\examples"
406   RMDir "$INSTDIR\lib\src\ds390"
407   RMDir "$INSTDIR\lib\src\ds400"
408   RMDir "$INSTDIR\lib\src\hc08"
409   RMDir "$INSTDIR\lib\src"
410
411   RMDir "$INSTDIR\lib\z80"
412   RMDir "$INSTDIR\lib\small"
413   RMDir "$INSTDIR\lib\large"
414   RMDir "$INSTDIR\lib\gbz80"
415   RMDir "$INSTDIR\lib\ds390"
416   RMDir "$INSTDIR\lib\ds400"
417   RMDir "$INSTDIR\lib\hc08"
418   RMDir "$INSTDIR\lib"
419
420   RMDir "$INSTDIR\include\asm\z80"
421   RMDir "$INSTDIR\include\asm\gbz80"
422   RMDir "$INSTDIR\include\asm\default"
423   RMDir "$INSTDIR\include\asm"
424   RMDir "$INSTDIR\include\pic16"
425   RMDir "$INSTDIR\include"
426
427   RMDir "$INSTDIR\doc"
428
429   RMDir "$INSTDIR\bin"
430
431   RMDir "$INSTDIR"
432
433   Push "$INSTDIR\bin"
434   Call un.RemoveFromPath
435
436   SetAutoClose true
437 SectionEnd
438
439 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
440 ; Path Manipulation functions                                                 ;
441 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
442
443 !verbose 3
444 !include "WinMessages.nsh"
445 !verbose 4
446
447 ; AddToPath - Adds the given dir to the search path.
448 ;        Input - head of the stack
449 ;        Note - Win9x systems requires reboot
450
451 Function AddToPath
452   Exch $0
453   Push $1
454   Push $2
455   Push $3
456   Push $4
457
458   ; don't add if the path doesn't exist
459   IfFileExists $0 "" AddToPath_done
460
461   Call IsNT
462   Pop $4
463   StrCmp $4 1 +3
464     ; Not on NT: read PATH from environment variable
465     ReadEnvStr $1 PATH
466     Goto +2
467     ; On NT: read PATH from registry
468     ReadRegStr $1 HKCU "Environment" "PATH"
469   Push "$1;"
470   Push "$0;"
471   Call StrStr
472   Pop $2
473   StrCmp $2 "" "" AddToPath_done
474   Push "$1;"
475   Push "$0\;"
476   Call StrStr
477   Pop $2
478   StrCmp $2 "" "" AddToPath_done
479   GetFullPathName /SHORT $3 $0
480   Push "$1;"
481   Push "$3;"
482   Call StrStr
483   Pop $2
484   StrCmp $2 "" "" AddToPath_done
485   Push "$1;"
486   Push "$3\;"
487   Call StrStr
488   Pop $2
489   StrCmp $2 "" "" AddToPath_done
490
491   StrCmp $4 1 AddToPath_NT
492     ; Not on NT
493     StrCpy $1 $WINDIR 2
494     FileOpen $1 "$1\autoexec.bat" a
495     FileSeek $1 -1 END
496     FileReadByte $1 $2
497     IntCmp $2 26 0 +2 +2 ; DOS EOF
498       FileSeek $1 -1 END ; write over EOF
499     FileWrite $1 "$\r$\nSET PATH=%PATH%;$3$\r$\n"
500     FileClose $1
501     SetRebootFlag true
502     Goto AddToPath_done
503
504   AddToPath_NT:
505     ReadRegStr $1 HKCU "Environment" "PATH"
506     StrCpy $2 $1 1 -1  ; copy last char
507     StrCmp $2 ";" 0 +2 ; if last char == ;
508       StrCpy $1 $1 -1  ; remove last char
509     StrCmp $1 "" AddToPath_NTdoIt
510       StrCpy $0 "$1;$0"
511     AddToPath_NTdoIt:
512       WriteRegExpandStr HKCU "Environment" "PATH" $0
513       SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
514
515   AddToPath_done:
516     Pop $4
517     Pop $3
518     Pop $2
519     Pop $1
520     Pop $0
521 FunctionEnd
522
523 ; RemoveFromPath - Remove a given dir from the path
524 ;     Input: head of the stack
525
526 Function un.RemoveFromPath
527   Exch $0
528   Push $1
529   Push $2
530   Push $3
531   Push $4
532   Push $5
533   Push $6
534
535   IntFmt $6 "%c" 26 ; DOS EOF
536
537   Call un.IsNT
538   Pop $1
539   StrCmp $1 1 unRemoveFromPath_NT
540     ; Not on NT
541     StrCpy $1 $WINDIR 2
542     FileOpen $1 "$1\autoexec.bat" r
543     GetTempFileName $4
544     FileOpen $2 $4 w
545     GetFullPathName /SHORT $0 $0
546     StrCpy $0 "SET PATH=%PATH%;$0"
547     Goto unRemoveFromPath_dosLoop
548
549     unRemoveFromPath_dosLoop:
550       FileRead $1 $3
551       StrCpy $5 $3 1 -1 ; read last char
552       StrCmp $5 $6 0 +2 ; if DOS EOF
553         StrCpy $3 $3 -1 ; remove DOS EOF so we can compare
554       StrCmp $3 "$0$\r$\n" unRemoveFromPath_dosLoopRemoveLine
555       StrCmp $3 "$0$\n" unRemoveFromPath_dosLoopRemoveLine
556       StrCmp $3 "$0" unRemoveFromPath_dosLoopRemoveLine
557       StrCmp $3 "" unRemoveFromPath_dosLoopEnd
558       FileWrite $2 $3
559       Goto unRemoveFromPath_dosLoop
560       unRemoveFromPath_dosLoopRemoveLine:
561         SetRebootFlag true
562         Goto unRemoveFromPath_dosLoop
563
564     unRemoveFromPath_dosLoopEnd:
565       FileClose $2
566       FileClose $1
567       StrCpy $1 $WINDIR 2
568       Delete "$1\autoexec.bat"
569       CopyFiles /SILENT $4 "$1\autoexec.bat"
570       Delete $4
571       Goto unRemoveFromPath_done
572
573   unRemoveFromPath_NT:
574     ReadRegStr $1 HKCU "Environment" "PATH"
575     StrCpy $5 $1 1 -1 ; copy last char
576     StrCmp $5 ";" +2  ; if last char != ;
577       StrCpy $1 "$1;" ; append ;
578     Push $1
579     Push "$0;"
580     Call un.StrStr    ; Find `$0;` in $1
581     Pop $2            ; pos of our dir
582     StrCmp $2 "" unRemoveFromPath_done
583       ; else, it is in path
584       ; $0 - path to add
585       ; $1 - path var
586       StrLen $3 "$0;"
587       StrLen $4 $2
588       StrCpy $5 $1 -$4   ; $5 is now the part before the path to remove
589       StrCpy $6 $2 "" $3 ; $6 is now the part after the path to remove
590       StrCpy $3 $5$6
591
592       StrCpy $5 $3 1 -1  ; copy last char
593       StrCmp $5 ";" 0 +2 ; if last char == ;
594         StrCpy $3 $3 -1  ; remove last char
595
596       StrCmp $3 "" +3
597         ; New PATH not empty: update the registry
598         WriteRegExpandStr HKCU "Environment" "PATH" $3
599         Goto +2
600         ; New PATH empty: remove from the registry
601         DeleteRegValue HKCU "Environment" "PATH"
602       SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
603
604   unRemoveFromPath_done:
605     Pop $6
606     Pop $5
607     Pop $4
608     Pop $3
609     Pop $2
610     Pop $1
611     Pop $0
612 FunctionEnd
613
614 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
615 ;            Utility Functions            ;
616 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
617
618 ; IsNT
619 ; no input
620 ; output, top of the stack = 1 if NT or 0 if not
621 ;
622 ; Usage:
623 ;   Call IsNT
624 ;   Pop $R0
625 ;  ($R0 at this point is 1 or 0)
626
627 !macro IsNT un
628 Function ${un}IsNT
629   Push $0
630   ReadRegStr $0 HKLM "SOFTWARE\Microsoft\Windows NT\CurrentVersion" CurrentVersion
631   StrCmp $0 "" 0 IsNT_yes
632   ; we are not NT.
633   Pop $0
634   Push 0
635   Return
636
637   IsNT_yes:
638     ; NT!!!
639     Pop $0
640     Push 1
641 FunctionEnd
642 !macroend
643 !insertmacro IsNT ""
644 !insertmacro IsNT "un."
645
646 ; StrStr
647 ; input, top of stack = string to search for
648 ;        top of stack-1 = string to search in
649 ; output, top of stack (replaces with the portion of the string remaining)
650 ; modifies no other variables.
651 ;
652 ; Usage:
653 ;   Push "this is a long ass string"
654 ;   Push "ass"
655 ;   Call StrStr
656 ;   Pop $R0
657 ;  ($R0 at this point is "ass string")
658
659 !macro StrStr un
660 Function ${un}StrStr
661 Exch $R1   ; st=haystack,old$R1, $R1=needle
662   Exch     ; st=old$R1,haystack
663   Exch $R2 ; st=old$R1,old$R2, $R2=haystack
664   Push $R3
665   Push $R4
666   Push $R5
667   StrLen $R3 $R1
668   StrCpy $R4 0
669   ; $R1=needle
670   ; $R2=haystack
671   ; $R3=len(needle)
672   ; $R4=cnt
673   ; $R5=tmp
674   loop:
675     StrCpy $R5 $R2 $R3 $R4
676     StrCmp $R5 $R1 done
677     StrCmp $R5 "" done
678     IntOp $R4 $R4 + 1
679     Goto loop
680 done:
681   StrCpy $R1 $R2 "" $R4
682   Pop $R5
683   Pop $R4
684   Pop $R3
685   Pop $R2
686   Exch $R1
687 FunctionEnd
688 !macroend
689 !insertmacro StrStr ""
690 !insertmacro StrStr "un."