Fix JSON dump field names (fixes #45)
[darcs-mirror-arbtt.git] / modpath.iss
1 // ----------------------------------------------------------------------------\r
2 //\r
3 // Inno Setup Ver:  5.2.1\r
4 // Script Version:  1.3.1\r
5 // Author:          Jared Breland <jbreland@legroom.net>\r
6 // Homepage:            http://www.legroom.net/software\r
7 //\r
8 // Script Function:\r
9 //      Enable modification of system path directly from Inno Setup installers\r
10 //\r
11 // Instructions:\r
12 //      Copy modpath.iss to the same directory as your setup script\r
13 //\r
14 //      Add this statement to your [Setup] section\r
15 //              ChangesEnvironment=yes\r
16 //\r
17 //      Add this statement to your [Tasks] section\r
18 //      You can change the Description or Flags, but the Name must be modifypath\r
19 //              Name: modifypath; Description: &Add application directory to your system path; Flags: unchecked\r
20 //\r
21 //      Add the following to the end of your [Code] section\r
22 //      setArrayLength must specify the total number of dirs to be added\r
23 //      Dir[0] contains first directory, Dir[1] contains second, etc.\r
24 //              function ModPathDir(): TArrayOfString;\r
25 //              var\r
26 //                      Dir:    TArrayOfString;\r
27 //              begin\r
28 //                      setArrayLength(Dir, 1)\r
29 //                      Dir[0] := ExpandConstant('{app}');\r
30 //                      Result := Dir;\r
31 //              end;\r
32 //              #include "modpath.iss"\r
33 // ----------------------------------------------------------------------------\r
34 \r
35 procedure ModPath();\r
36 var\r
37         oldpath:        String;\r
38         newpath:        String;\r
39         pathArr:        TArrayOfString;\r
40         aExecFile:      String;\r
41         aExecArr:       TArrayOfString;\r
42         i, d:           Integer;\r
43         pathdir:        TArrayOfString;\r
44 begin\r
45 \r
46         // Get array of new directories and act on each individually\r
47         pathdir := ModPathDir();\r
48         for d := 0 to GetArrayLength(pathdir)-1 do begin\r
49 \r
50                 // Modify WinNT path\r
51                 if UsingWinNT() = true then begin\r
52 \r
53                         // Get current path, split into an array\r
54                         RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', oldpath);\r
55                         oldpath := oldpath + ';';\r
56                         i := 0;\r
57                         while (Pos(';', oldpath) > 0) do begin\r
58                                 SetArrayLength(pathArr, i+1);\r
59                                 pathArr[i] := Copy(oldpath, 0, Pos(';', oldpath)-1);\r
60                                 oldpath := Copy(oldpath, Pos(';', oldpath)+1, Length(oldpath));\r
61                                 i := i + 1;\r
62 \r
63                                 // Check if current directory matches app dir\r
64                                 if pathdir[d] = pathArr[i-1] then begin\r
65                                         // if uninstalling, remove dir from path\r
66                                         if IsUninstaller() = true then begin\r
67                                                 continue;\r
68                                         // if installing, abort because dir was already in path\r
69                                         end else begin\r
70                                                 abort;\r
71                                         end;\r
72                                 end;\r
73 \r
74                                 // Add current directory to new path\r
75                                 if i = 1 then begin\r
76                                         newpath := pathArr[i-1];\r
77                                 end else begin\r
78                                         newpath := newpath + ';' + pathArr[i-1];\r
79                                 end;\r
80                         end;\r
81 \r
82                         // Append app dir to path if not already included\r
83                         if IsUninstaller() = false then\r
84                                 newpath := newpath + ';' + pathdir[d];\r
85 \r
86                         // Write new path\r
87                         RegWriteStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', newpath);\r
88 \r
89                 // Modify Win9x path\r
90                 end else begin\r
91 \r
92                         // Convert to shortened dirname\r
93                         pathdir[d] := GetShortName(pathdir[d]);\r
94 \r
95                         // If autoexec.bat exists, check if app dir already exists in path\r
96                         aExecFile := 'C:\AUTOEXEC.BAT';\r
97                         if FileExists(aExecFile) then begin\r
98                                 LoadStringsFromFile(aExecFile, aExecArr);\r
99                                 for i := 0 to GetArrayLength(aExecArr)-1 do begin\r
100                                         if IsUninstaller() = false then begin\r
101                                                 // If app dir already exists while installing, abort add\r
102                                                 if (Pos(pathdir[d], aExecArr[i]) > 0) then\r
103                                                         abort;\r
104                                         end else begin\r
105                                                 // If app dir exists and = what we originally set, then delete at uninstall\r
106                                                 if aExecArr[i] = 'SET PATH=%PATH%;' + pathdir[d] then\r
107                                                         aExecArr[i] := '';\r
108                                         end;\r
109                                 end;\r
110                         end;\r
111 \r
112                         // If app dir not found, or autoexec.bat didn't exist, then (create and) append to current path\r
113                         if IsUninstaller() = false then begin\r
114                                 SaveStringToFile(aExecFile, #13#10 + 'SET PATH=%PATH%;' + pathdir[d], True);\r
115 \r
116                         // If uninstalling, write the full autoexec out\r
117                         end else begin\r
118                                 SaveStringsToFile(aExecFile, aExecArr, False);\r
119                         end;\r
120                 end;\r
121 \r
122                 // Write file to flag modifypath was selected\r
123                 //   Workaround since IsTaskSelected() cannot be called at uninstall and AppName and AppId cannot be "read" in Code section\r
124                 if IsUninstaller() = false then\r
125                         SaveStringToFile(ExpandConstant('{app}') + '\uninsTasks.txt', WizardSelectedTasks(False), False);\r
126         end;\r
127 end;\r
128 \r
129 procedure CurStepChanged(CurStep: TSetupStep);\r
130 begin\r
131         if CurStep = ssPostInstall then\r
132                 if IsTaskSelected('modifypath') then\r
133                         ModPath();\r
134 end;\r
135 \r
136 procedure CurUninstallStepChanged(CurUninstallStep: TUninstallStep);\r
137 var\r
138         appdir:                 String;\r
139         selectedTasks:  AnsiString;\r
140 begin\r
141         appdir := ExpandConstant('{app}')\r
142         if CurUninstallStep = usUninstall then begin\r
143                 if LoadStringFromFile(appdir + '\uninsTasks.txt', selectedTasks) then\r
144                         if Pos('modifypath', selectedTasks) > 0 then\r
145                                 ModPath();\r
146                 DeleteFile(appdir + '\uninsTasks.txt')\r
147         end;\r
148 end;\r
149 \r
150 function NeedRestart(): Boolean;\r
151 begin\r
152         if IsTaskSelected('modifypath') and not UsingWinNT() then begin\r
153                 Result := True;\r
154         end else begin\r
155                 Result := False;\r
156         end;\r
157 end;\r