This repository has been archived by the owner on Apr 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup
252 lines (220 loc) · 6.99 KB
/
setup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/* This file is part of the XP framework
*
* $Id$
*/
define('BASE_URL', 'https://github.com/xp-framework/xp-runners/releases/download/v6.4.0/');
// {{{ println(string* args)
function println() {
foreach (func_get_args() as $a) { echo $a; }
echo PHP_EOL;
}
// }}}
// {{{ ardl(string proxy, string url, string targetdir)
function ardl($proxy, $url, $targetdir, $permissions= -1) {
static $pw= 10;
static $headers= "User-Agent: xp-setup/6.4.0\r\nAccept: */*\r\n\r\n";
do {
if ($proxy) {
println('---> ', $url, ' via ', $proxy);
$u= parse_url($proxy);
$request= sprintf(
"GET %s%s HTTP/1.0\r\nHost: %s\r\n%s",
$url,
isset($u['query']) ? '?'.$u['query'] : '',
$u['host'],
$headers
);
} else {
println('---> ', $url);
$u= parse_url($url);
$request= sprintf(
"GET %s%s HTTP/1.0\r\nHost: %s\r\n%s",
$u['path'],
isset($u['query']) ? '?'.$u['query'] : '',
$u['host'],
$headers
);
}
if ('https' === $u['scheme']) {
$fd= fsockopen('ssl://'.$u['host'], isset($u['port']) ? $u['port'] : 443, $errno, $errstr);
} else {
$fd= fsockopen($u['host'], isset($u['port']) ? $u['port'] : 80, $errno, $errstr);
}
if (!$fd) {
println('*** Failed (#', $errno, ': ', $errstr, ' ***');
return FALSE;
}
// Send HTTP request
fputs($fd, $request);
// Read HTTP response
$status= fgets($fd, 1024);
sscanf($status, "HTTP/1.%*d %d %[^\r]", $code, $message);
if (301 === $code || 302 === $code) {
do {
if (FALSE === ($header= fgets($fd, 1024))) return FALSE;
if (0 === strncasecmp($header, 'Location: ', 10)) {
$url= rtrim(substr($header, 10));
continue 2;
}
} while ($header);
println('*** Failed (Redirect without location, HTTP ', $code, ': ', $message, ' from ', $url, ') ***');
return FALSE;
} else if (200 === $code) {
do {
if (FALSE === ($header= fgets($fd, 1024))) return FALSE;
if ('' === ($header= rtrim($header))) break;
} while ($header);
} else {
println('*** Failed (HTTP ', $code, ': ', $message, ' from ', $url, ') ***');
return FALSE;
}
} while (200 !== $code);
while ($line= fgets($fd, 0xFF)) {
if (2 != sscanf($line, '--%d:%[^:]--', $length, $filename)) continue;
printf(
'---> %s (%.2f kB) [%s]%s',
$filename,
$length / 1024,
str_repeat('.', $pw),
str_repeat("\x08", $pw+ 1)
);
$target= $targetdir.DIRECTORY_SEPARATOR.$filename;
$ft= fopen($target, 'wb');
if (!$ft) {
println('*** I/O Error w/ ', $filename, ' ***');
return FALSE;
}
$s= 0; $c= 0;
while ($s < $length) {
$s+= fwrite($ft, fread($fd, min(0x1000, $length- $s)));
// Update progress
$d= ceil(($s / $length) * $pw);
if ($d == $c) continue;
echo str_repeat('#', $d- $c); flush();
$c= $d;
}
fclose($ft);
// Change permissions if requested
if (-1 != $permissions) {
chmod($target, fileperms($target) | $permissions);
}
println();
}
fclose($fd);
return TRUE;
}
// }}}
// {{{ which(string command [, bool ext])
function which($command, $ext= FALSE) {
$search= explode(PATH_SEPARATOR, getenv('PATH'));
$extensions= array('');
$ext && $extensions+= explode(PATH_SEPARATOR, getenv('PATHEXT'));
foreach ($search as $path) {
foreach ($extensions as $ext) {
if (file_exists($q= $path.DIRECTORY_SEPARATOR.$command.$ext)) return realpath($q);
}
}
return NULL;
}
// }}}
// {{{ inpath(string dir)
function inpath($dir) {
$realpath= realpath($dir);
foreach (explode(PATH_SEPARATOR, getenv('PATH')) as $path) {
if (realpath($path) == $realpath) return TRUE;
}
return FALSE;
}
// }}}
// {{{ usage()
function usage() {
println('*** Usage: setup [-d <runner-dir>] [-p <proxy-url>] [-e <runner-env>]');
println('- <runner-dir>: Path to where XP runners should be installed (default: $CWD)');
println('- <proxy-url> : HTTP proxy to use (default: none), e.g. http://proxy.example.com:3128/');
println('- <runner-env>: Runner environment to use (windows | unix, default: auto-detect)');
return 1;
}
// }}}
// {{{ main
error_reporting(E_ALL);
$runners= $proxy= NULL;
$warnings= array();
$uname= php_uname();
$bin= getcwd();
println('===> Setup xp runners @ ', $uname);
// Parse command line arguments
for ($i= 0; $i < $argc; $i++) {
switch ($argv[$i]) {
case '-d': $bin= $argv[++$i]; break;
case '-e': $runners= $argv[++$i]; break;
case '-p': $proxy= $argv[++$i]; break;
case '-?': case '--help': usage() && exit(1);
}
}
if (!$bin) usage() && exit(1);
// Runner path: Check whether "bin" exists and is in PATH
if (!is_dir($bin)) {
$warnings[]= 'Bindir '.$bin.' does not exist, created it for you';
}
if (!inpath($bin)) {
$warnings[]= 'Bindir '.$bin.' is not in PATH, you might want to consider adding it!';
}
// Detect environment
println('===> Environment');
switch (TRUE) {
case NULL !== $runners:
break;
case TRUE == preg_match('/^([A-Z]+)BSD/i', $uname):
$runners= 'bsd';
break;
case TRUE == preg_match('/^Windows/i', $uname):
$runners= 'windows';
// Verify .NET version
$d= dir(getenv('SYSTEMROOT').'/Microsoft.NET/Framework');
$versions= array();
while ($entry= $d->read()) {
if (sscanf($entry, 'v%d.%d.%s', $major, $minor, $patch) > 1) {
$versions[]= substr($entry, 1);
}
}
$d->close();
if (!version_compare(max($versions), '2.0', 'gt')) {
println('*** .NET Framework 2.0 or greater required, found [', implode(', ', $versions), ']');
exit(1);
}
break;
default:
$runners= 'unix';
break;
}
println('---> Using ', $runners, ' runners');
// Create directories
foreach (array($bin) as $dir) {
if (is_dir($dir)) continue;
if (mkdir($dir)) continue;
println('*** Failed to create directory ', $dir, ' ***');
exit(1);
}
// Download runners
ardl($proxy, BASE_URL.$runners.'.ar', $bin, 0700) || exit(3);
// Check for xp.ini
$config= $bin.DIRECTORY_SEPARATOR.'xp.ini';
if (!file_exists($config)) {
$fp= fopen($config, 'wb');
fwrite($fp, 'use=[PLEASE EDIT "'.$config.'"]'.PHP_EOL);
fwrite($fp, PHP_EOL);
fclose($fp);
$warnings[]= 'No configuration file found - created it for you';
$warnings[]= 'Please edit '.$config;
}
// Print warnings
if (!empty($warnings)) {
println('===> Warnings:');
foreach ($warnings as $warning) {
println('!!! ', $warning);
}
}
println('===> XP runners installed to the directory <'.$bin.'>');
// }}}
?>