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