Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 605 lines (521 sloc) 20.186 kb
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
1 #!/usr/bin/php
2 <?php
3
4 ////////////////////////////////////////////////////////////////////////////////
5 // __ _ _ //
6 // / _| | | | //
7 // | |_| |__ ___ _ __ ___ __| | //
8 // | _| '_ \ / __| '_ ` _ \ / _` | //
9 // | | | |_) | (__| | | | | | (_| | //
10 // |_| |_.__/ \___|_| |_| |_|\__,_| //
11 // //
12 // Facebook Command Line Interface Utility //
13 // http://facebook.com/fbcmd //
14 // http://fbcmd.dtompkins.com //
15 // Copyright (c) 2007,2009 Dave Tompkins //
16 // //
17 ////////////////////////////////////////////////////////////////////////////////
18 // //
19 // see fbcmd.php for copyright information //
20 // //
21 ////////////////////////////////////////////////////////////////////////////////
22
23 // This is a Utility to update your version of fbcmd
687ae64 Dave Tompkins Adding .msi packaging tool
dtompkins authored
24
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
25 ////////////////////////////////////////////////////////////////////////////////
26
27 $isTrace = 0;
28 if (isset($argv[3])) {
29 if ($argv[3]) {
30 $isTrace = 1;
31 }
32 }
98d746d Dave Tompkins Fixing bugs... testing in linux 2
dtompkins authored
33 $isTrace = 1;
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
34
35 ////////////////////////////////////////////////////////////////////////////////
36
37 $isContinueOnError = 0;
38 if (isset($argv[4])) {
39 if ($argv[4]) {
40 $isContinueOnError = 1;
41 }
42 }
43 TraceVar('isContinueOnError');
44
45 ////////////////////////////////////////////////////////////////////////////////
46
47 // Note: The Installer version is independent of the fbcmd version
48
49 $fbcmdUpdateVersion = '2.6';
50 TraceVar('fbcmdUpdateVersion');
687ae64 Dave Tompkins Adding .msi packaging tool
dtompkins authored
51
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
52 ////////////////////////////////////////////////////////////////////////////////
53
54 print "\n";
55 print "fbcmd update utility [version {$fbcmdUpdateVersion}]\n";
56 print "http://fbcmd.dtompkins.com/update\n";
57 print "for basic syntax: php fbcmd_update.php help\n\n";
58
59 ////////////////////////////////////////////////////////////////////////////////
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
60
840bc6e Dave Tompkins cleaning up of fbcmd_updater.php
dtompkins authored
61 if (isset($argv[0])) {
62 $thisProgram = $argv[0];
63 } else {
64 $thisProgram = "./fbcmd_update.php";
65 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
66 $thisProgramFolder = realpath(dirname($thisProgram));
67 TraceVar('thisProgram');
68 TraceVar('thisProgramFolder');
69
70 ////////////////////////////////////////////////////////////////////////////////
71
72 if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
73 $isWindows = true;
74 } else {
75 $isWindows = false;
76 }
77 TraceVar('isWindows');
78
79 ////////////////////////////////////////////////////////////////////////////////
80
81 // Set the defaults for fbcmd... these can be overridden in prefs.php
82 $fbcmdPrefs['update_branch'] = 'master';
83 if ($isWindows) {
84 $fbcmdPrefs['install_dir'] = CleanPath($thisProgramFolder);
85 $fbcmdPrefs['install_copy_to_path'] = '0';
86 $fbcmdPrefs['install_path_dir'] = '';
87 } else {
88 $fbcmdPrefs['install_dir'] = '/usr/local/lib/fbcmd/';
89 $fbcmdPrefs['install_copy_to_path'] = '1';
90 $fbcmdPrefs['install_path_dir'] = '/usr/local/bin/';
91 }
92 $fbcmdPrefs['mkdir_mode'] = 0777;
93 $fbcmdPrefs['install_auto_restart'] = '1';
94 $defaultInstallDir = $fbcmdPrefs['install_dir'];
95 TraceVar('defaultInstallDir');
840bc6e Dave Tompkins cleaning up of fbcmd_updater.php
dtompkins authored
96
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
97 ////////////////////////////////////////////////////////////////////////////////
98
99 $envFbcmd = getenv('FBCMD');
100 TraceVar('envFbcmd');
101 if ($envFbcmd) {
102 $fbcmdBaseDir = CleanPath($envFbcmd);
103 } else {
104 if ($isWindows) {
105 if (getenv('USERPROFILE')) {
106 $fbcmdBaseDir = CleanPath(getenv('USERPROFILE')) . 'fbcmd/';
107 } else {
108 $fbcmdBaseDir = 'c:/fbcmd/';
109 }
110 } else {
111 $fbcmdBaseDir = CleanPath(getenv('HOME')) . '.fbcmd/';
112 }
113 }
114 TraceVar('fbcmdBaseDir');
115
116 ////////////////////////////////////////////////////////////////////////////////
117
118 $isSavePrefs = false;
119 $prefsFile = "{$fbcmdBaseDir}prefs.php";
120 if (file_exists($prefsFile)) {
121 include($prefsFile);
122 $isIncludeFile = true;
123 } else {
124 $isIncludeFile = false;
125 }
126 TraceVar('isIncludeFile');
127
128 ////////////////////////////////////////////////////////////////////////////////
129
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
130 $specifiedBranch = '';
c692a1c Dave Tompkins more updates to fbcmd_updater
dtompkins authored
131 if (isset($argv[1])) {
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
132 $specifiedBranch = strtolower($argv[1]);
133 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
134 TraceVar('specifiedBranch');
135
136 ////////////////////////////////////////////////////////////////////////////////
840bc6e Dave Tompkins cleaning up of fbcmd_updater.php
dtompkins authored
137
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
138 if (isset($argv[2])) {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
139 $fbcmdPrefs['install_dir'] = $argv[2];
140 CheckPath($fbcmdPrefs['install_dir']);
141 $fbcmdPrefs['install_dir'] = CleanPath(realpath($fbcmdPrefs['install_dir']));
142 $isSavePrefs = true;
143 }
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
144
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
145 ////////////////////////////////////////////////////////////////////////////////
146
147 $keywords = array('-h','help','--help','clear','install','remove','script');
148
149 $isHelp = false;
150 $isKeyword = false;
151 if (in_array($specifiedBranch,$keywords)) {
152 if (($specifiedBranch=='-h')||($specifiedBranch=='help')||($specifiedBranch=='--help')) {
153 $isHelp = true;
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
154 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
155 $isKeyword = true;
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
156 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
157 TraceVar('isKeyword');
158 TraceVar('isHelp');
840bc6e Dave Tompkins cleaning up of fbcmd_updater.php
dtompkins authored
159
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
160 ////////////////////////////////////////////////////////////////////////////////
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
161
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
162 if ((($specifiedBranch == '')||($isHelp))&&($isIncludeFile == false)) {
163 $isFirstInstall = true;
164 } else {
165 $isFirstInstall = false;
166 }
167 TraceVar('isFirstInstall');
8c1ef42 Dave Tompkins fix new path problems
dtompkins authored
168
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
169 ////////////////////////////////////////////////////////////////////////////////
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
170
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
171 if ($isHelp) {
172 print "\nphp fbcmd_update.php [branch|keyword] [folder] [trace] [ignore_err]\n\n";
173 print "branch: Software development branch\n";
174 print " master stable, not all features available\n";
175 print " beta reaonably stable, subject to minor changes\n";
176 print " dev latest features, expect changes\n\n";
177 print "keyword: Instead of a branch, you can specify one of:\n";
178 print " help display this message\n";
179 print " install performa a full install\n";
180 print " script regenerate the fbcmd script\n";
181 print " clear clear your personal user settings\n";
182 print " remove removes fbcmd from your system\n\n";
183 print "folder: Specify a destination installtion directory\n\n";
184 print "trace: Defaults to 0. Set to 1 for verbose output\n\n";
185 print "ignore_err: Defaults to 0. Set to 1 to ignore fatal errors\n\n\n";
186 }
187
188 ////////////////////////////////////////////////////////////////////////////////
189
190 if (($isIncludeFile == false)&&($specifiedBranch != 'remove')&&($specifiedBranch != 'clear')) {
191 if (!file_exists($fbcmdBaseDir)) {
192 if (mkdir($fbcmdBaseDir,0700,true)) {
193 Trace("creating directory [{$fbcmdBaseDir}]");
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
194 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
195 print "Error: cound not create directory: [{$fbcmdBaseDir}]\n";
196 FatalError();
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
197 }
198 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
199 $isSavePrefs = true;
200 }
201
202 ////////////////////////////////////////////////////////////////////////////////
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
203
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
204 if ($isSavePrefs) {
205 $fileContents = SavePrefsContents();
206 if (file_put_contents($prefsFile,$fileContents)) {
e2e5376 Dave Tompkins Fixing bugs... testing in linux
dtompkins authored
207 Trace("creating file [{$prefsFile}]");
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
208 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
209 print "Error: cound not create file: [{$prefsFile}]\n";
210 FatalError();
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
211 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
212 }
213
214 ////////////////////////////////////////////////////////////////////////////////
215
216 if (($isHelp)||($isFirstInstall)) {
217 print "Preference file: [{$prefsFile}]\n\n";
218 print "Software development branch: [{$fbcmdPrefs['update_branch']}]\n";
219 print "Software library destination: [{$fbcmdPrefs['install_dir']}]\n";
220 print "Copy script to path?: ";
221 if ($fbcmdPrefs['install_copy_to_path']) {
222 print "[Yes]\n";
b5fa4a1 Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
223 print "Path location: [{$fbcmdPrefs['install_path_dir']}]\n";
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
224 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
225 print "[No]\n";
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
226 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
227 print "Auto-restart when necessary: ";
228 if ($fbcmdPrefs['install_auto_restart']) {
229 print "[Yes]\n\n";
230 } else {
231 print "[No]\n\n";
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
232 }
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
233
234
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
235 if ($isFirstInstall) {
236 print "\n\n";
237 print "Welcome! This appears to be the first time running fbcmd_update.\n";
238 print "\n";
239 print "To change any of the above settings, modify your preferences file\n";
240 print "To change your preferences file location, set an FBCMD environment var.\n\n";
241 print "Otherwise, The above default settings are fine for most users\n\n";
242 print "To finish the installation, re-execute this command\n";
243 if ($isWindows) {
244 print "\n php fbcmd_update.php\n\n";
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
245 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
246 print "\n $ sudo php fbcmd_update.php\n\n";
840bc6e Dave Tompkins cleaning up of fbcmd_updater.php
dtompkins authored
247 }
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
248 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
249 exit;
250 }
251
252 ////////////////////////////////////////////////////////////////////////////////
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
253
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
254 if (($specifiedBranch == 'clear')||($specifiedBranch == 'remove')) {
255 DeleteFileOrDirectory($fbcmdBaseDir);
256 if ($specifiedBranch == 'clear') {
257 exit;
9f9d124 Dave Tompkins Update Utility -- Version 1
dtompkins authored
258 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
259 }
260
261 if ($specifiedBranch == 'remove') {
262 if ($fbcmdPrefs['install_copy_to_path']) {
263 if ($isWindows) {
264 $pathShell = CleanPath($fbcmdPrefs['install_path_dir']) . "fbcmd.bat";
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
265 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
266 $pathShell = CleanPath($fbcmdPrefs['install_path_dir']) . "fbcmd";
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
267 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
268 DeleteFileOrDirectory($pathShell);
9f9d124 Dave Tompkins Update Utility -- Version 1
dtompkins authored
269 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
270 DeleteFileOrDirectory($fbcmdPrefs['install_dir']);
271 exit;
9f9d124 Dave Tompkins Update Utility -- Version 1
dtompkins authored
272 }
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
273
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
274 ////////////////////////////////////////////////////////////////////////////////
231896c Dave Tompkins added messages for adding fbcmd to your PATH
dtompkins authored
275
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
276 $installFolder = $fbcmdPrefs['install_dir'];
277 CheckPath($installFolder);
278 $installFolder = CleanPath(realpath($installFolder));
279 $installFolderOS = $installFolder;
280 if ($isWindows) {
281 $installFolderOS = str_replace('/', '\\', $installFolderOS);
282 }
283 TraceVar('installFolder');
284 TraceVar('installFolderOS');
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
285
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
286 $mainFile = "{$installFolder}fbcmd.php";
287 $updateFile = "{$installFolder}fbcmd_update.php";
288 if ($isWindows) {
289 $scriptName = "fbcmd.bat";
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
290 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
291 $scriptName = "fbcmd";
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
292 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
293 $fullScriptName = "{$installFolder}$scriptName";
b5fa4a1 Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
294 $fullPathScript = CleanPath($fbcmdPrefs['install_path_dir']) . $scriptName;
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
295 TraceVar('fullScriptName');
b5fa4a1 Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
296 TraceVar('fullPathScript');
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
297
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
298 ////////////////////////////////////////////////////////////////////////////////
299
300 $comment = "This script file was auto-generated by [{$updateFile}]";
301
302 if ($isWindows) {
f74be97 Dave Tompkins added generation of fbcmd.bat
dtompkins authored
303 $contentsBatch = "@echo off\n";
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
304 $contentsBatch .= "REM *** {$comment}\n";
305 $contentsBatch .= "php \"$mainFile\" %*\n";
f74be97 Dave Tompkins added generation of fbcmd.bat
dtompkins authored
306 } else {
307 $contentsBatch = "#! /bin/bash\n";
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
308 $contentsBatch .= "# *** {$comment}\n";
309 $contentsBatch .= "php \"$mainFile\" $* -col=$(tput cols)\n";
310 }
b5fa4a1 Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
311
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
312 if (file_put_contents($fullScriptName,$contentsBatch)) {
313 Trace ("created script: [{$fullScriptName}]");
314 if (!$isWindows) {
e2e5376 Dave Tompkins Fixing bugs... testing in linux
dtompkins authored
315 if (chmod($fullScriptName,0777)) {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
316 Trace ("chmod script: [{$fullScriptName}]");
be6edd9 Dave Tompkins adding +x to bash script
dtompkins authored
317 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
318 print "error chmod: [{$fullScriptName}] (non-fatal)\n";
be6edd9 Dave Tompkins adding +x to bash script
dtompkins authored
319 }
bcfaa49 Dave Tompkins fix typo
dtompkins authored
320 }
f74be97 Dave Tompkins added generation of fbcmd.bat
dtompkins authored
321 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
322 print "Error: cound not create file: [{$fullScriptName}]\n";
323 FatalError();
324 }
325
b5fa4a1 Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
326 ////////////////////////////////////////////////////////////////////////////////
327
72d476b Dave Tompkins Fixing bugs... testing in linux 5
dtompkins authored
328 $isCopyToPath = false;
b5fa4a1 Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
329
330 if (($specifiedBranch == 'script')||($specifiedBranch == 'install')) {
331 $isCopyToPath = true;
332 }
333 if ($fbcmdPrefs['install_copy_to_path']) {
334 if (!file_exists($fullPathScript)) {
335 $isCopyToPath = true;
336 }
337 }
338 TraceVar('isCopyToPath');
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
339
b5fa4a1 Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
340 if ($isCopyToPath) {
59ed9e2 Dave Tompkins Fixing bugs... testing in linux 6
dtompkins authored
341 CheckPath($fbcmdPrefs['install_path_dir']);
b5fa4a1 Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
342 if (file_put_contents($fullPathScript,$contentsBatch)) {
343 Trace ("created script: [{$fullPathScript}]");
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
344 if (!isWindows) {
345 if (chmod($scriptName,0777)) {
b5fa4a1 Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
346 Trace ("chmod script: [{$fullPathScript}]");
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
347 } else {
b5fa4a1 Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
348 print "error chmod: [{$fullPathScript}] (non-fatal)\n";
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
349 }
350 }
351 } else {
b5fa4a1 Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
352 print "Error: cound not create file: [{$fullPathScript}]\n";
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
353 FatalError();
354 }
f74be97 Dave Tompkins added generation of fbcmd.bat
dtompkins authored
355 }
356
b222486 Dave Tompkins added 'script' command to fbcmd_update.php
dtompkins authored
357 if ($specifiedBranch == 'script') {
358 exit;
359 }
360
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
361 ////////////////////////////////////////////////////////////////////////////////
362
363 $defaultBranch = strtolower($fbcmdPrefs['update_branch']);
364 $branch = $defaultBranch;
bb1b09f Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
365 if (($specifiedBranch)&&($specifiedBranch != 'install')) {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
366 $branch = $specifiedBranch;
367 Trace("overriding default branch: [{$defaultBranch}]");
231896c Dave Tompkins added messages for adding fbcmd to your PATH
dtompkins authored
368 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
369 TraceVar('defaultBranch');
370 TraceVar('branch');
231896c Dave Tompkins added messages for adding fbcmd to your PATH
dtompkins authored
371
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
372 ////////////////////////////////////////////////////////////////////////////////
373
374 print "...";
375 $contentsRemoteUpdater = GetGithub("fbcmd_update.php",false);
376 preg_match ("/fbcmdUpdateVersion\s=\s'([^']+)'/",$contentsRemoteUpdater,$matches);
377 $newUpdateVersion = 0;
378 if (isset($matches[1])) {
379 $newUpdateVersion = $matches[1];
380 }
381 TraceVar('newUpdateVersion');
382
383 ////////////////////////////////////////////////////////////////////////////////
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
384
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
385 if (($newUpdateVersion > $fbcmdUpdateVersion)||(!file_exists($updateFile))) {
386 if (file_put_contents($updateFile,$contentsRemoteUpdater)) {
e2e5376 Dave Tompkins Fixing bugs... testing in linux
dtompkins authored
387 Trace("creating [{$updateFile}]");
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
388 if ($newUpdateVersion > $fbcmdUpdateVersion) {
389 if ($fbcmdPrefs['install_auto_restart']) {
390 print "\nNewer update software downloaded [{$fbcmdUpdateVersion}] -> [{$newUpdateVersion}]\n";
391 print "\nattempting to restart...\n";
392 $execString = "php {$updateFile} \"{$specifiedBranch}\" \"{$installFolder}\" $isTrace $isContinueOnError";
393 exec ($execString);
394 exit;
395 } else {
396 if (realpath($thisProgram) == realpath($updateFile)) {
397 print "\nUpdate INCOMPLETE: Restart this program (it has updated itself)\n\n";
398 } else {
399 print "\nUpdate INCOMPLETE: run the NEW updater: [$updateFile]\n";
400 print "(you might want to remove this old one to avoid confusion)\n\n";
401 }
402 FatalError();
403 }
404 }
231896c Dave Tompkins added messages for adding fbcmd to your PATH
dtompkins authored
405 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
406 print "Fatal error: could not save [$updateFile]\n\n";
407 FatalError();
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
408 }
409 }
410
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
411 ////////////////////////////////////////////////////////////////////////////////
412
413 $oldMainVersion = 'none';
414 $newMainVersion = '';
415 if (file_exists($mainFile)) {
416 $oldFileContents = @file_get_contents($mainFile);
417 preg_match ("/fbcmdVersion\s=\s'([^']+)'/",$oldFileContents,$matches);
418 if (isset($matches[1])) {
419 $oldMainVersion = $matches[1];
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
420 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
421 $oldMainVersion = '???';
231896c Dave Tompkins added messages for adding fbcmd to your PATH
dtompkins authored
422 }
423 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
424 TraceVar('oldMainVersion');
425
426 ////////////////////////////////////////////////////////////////////////////////
427
428 print ".";
429 $fileList = GetGithub("filelist.txt");
430 $files = explode("\n",$fileList);
431 foreach ($files as $f) {
432 $g = preg_replace('/\s*\#.*$/','',$f);
433 if ($g) {
434 print ".";
435 $contents = GetGithub($g);
436 if ($g == 'fbcmd.php') {
437 preg_match("/fbcmdVersion\s=\s'([^']+)'/",$contents,$matches);
438 if (isset($matches[1])) {
439 $newMainVersion = $matches[1];
440 } else {
441 print "Non-fatal error: could not determine new version\n";
442 $newMainVersion = '???';
443 }
444 }
445 }
446 }
447 print "\n";
448
449 ////////////////////////////////////////////////////////////////////////////////
450
451 print "\nUpdate: COMPLETE!\n\n";
452 print "fbcmd version: [{$oldMainVersion}] --> [{$newMainVersion}]\n";
453
454 if (!$fbcmdPrefs['install_copy_to_path']) {
455 if (stripos(getenv('PATH'),substr($installFolderOS,0,strlen($installFolderOS)-1)) === false) {
456 print "\nNote: Your PATH does not appear to include {$installFolderOS}\n";
457 if ($isWindows) {
458 print "(right click) My Computer -> Properties -> Advanced -> Environment Variables\n";
459 print "Edit the PATH entry and add: ;{$installFolderOS}\n";
460 } else {
461 print "Add the following line to your ~/.bash_profile file:\n";
462 print " PATH=\$PATH:{$installFolderOS}; export PATH\n";
463 }
464 }
465 }
466
467 if (realpath($thisProgram) != realpath($updateFile)) {
468 print "\nNote: fbcmd_update.php is now at [{$updateFile}]\n";
469 print "so you can remove the old one at [{$thisProgram}]\n\n";
470 }
231896c Dave Tompkins added messages for adding fbcmd to your PATH
dtompkins authored
471
74b9046 Dave Tompkins clean up path message
dtompkins authored
472 print "\n";
473
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
474 exit;
475
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
476 function DeleteFileOrDirectory($dir) { # snagged from http://ca3.php.net/rmdir
b5fa4a1 Dave Tompkins Fixing bugs... testing in linux 3
dtompkins authored
477 Trace("deleting [{$dir}]");
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
478 if (!file_exists($dir)) return true;
479 if (!is_dir($dir)) {
480 if (unlink($dir)) {
481 return true;
482 } else {
483 print "Could Not Delete File: [{$dir}]\n";
484 FatalError();
485 }
486 }
487 foreach (scandir($dir) as $item) {
488 if ($item == '.' || $item == '..') continue;
489 if (!DeleteFileOrDirectory($dir.DIRECTORY_SEPARATOR.$item)) return false;
490 }
491 if (rmdir($dir)) {
492 return true;
493 } else {
494 print "Could Not Delete Directory: [{$dir}]\n";
495 FatalError();
496 }
497 }
498
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
499 function GetGithub($filename, $save = true) {
500 global $branch;
840bc6e Dave Tompkins cleaning up of fbcmd_updater.php
dtompkins authored
501 global $installFolder;
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
502 $fileSrc = "http://github.com/dtompkins/fbcmd/raw/{$branch}/{$filename}";
840bc6e Dave Tompkins cleaning up of fbcmd_updater.php
dtompkins authored
503 $fileDest = "{$installFolder}{$filename}";
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
504 $fileContents = @file_get_contents($fileSrc);
505 if ($fileContents) {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
506 Trace("downloading: [$fileSrc}]");
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
507 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
508 print "Could not download: [{$fileSrc}]\n";
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
509 FatalError();
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
510 }
511 if ($save) {
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
512 CheckPath(dirname($fileDest));
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
513 if (@file_put_contents($fileDest,$fileContents)) {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
514 Trace("saving: [{$fileDest}]");
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
515 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
516 print "Could not save: [{$fileDest}]\n";
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
517 FatalError();
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
518 }
519 }
520 return $fileContents;
521 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
522
07b4db0 Dave Tompkins fbcmd_update.pl Version 2
dtompkins authored
523 function CleanPath($curPath) {
9f9d124 Dave Tompkins Update Utility -- Version 1
dtompkins authored
524 $path = $curPath;
525 if ($path == '') {
526 $path = './';
527 }
528 $path = str_replace('\\', '/', $path);
529 if ($path[strlen($path)-1] != '/') {
530 $path .= '/';
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
531 }
9f9d124 Dave Tompkins Update Utility -- Version 1
dtompkins authored
532 return $path;
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
533 }
534
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
535 function CheckPath($filePath) {
9f9d124 Dave Tompkins Update Utility -- Version 1
dtompkins authored
536 global $fbcmdPrefs;
537 if (!file_exists($filePath)) {
07b4db0 Dave Tompkins fbcmd_update.pl Version 2
dtompkins authored
538 if (mkdir($filePath,$fbcmdPrefs['mkdir_mode'],true)) {
98d746d Dave Tompkins Fixing bugs... testing in linux 2
dtompkins authored
539 Trace("creating [{$filePath}]");
9f9d124 Dave Tompkins Update Utility -- Version 1
dtompkins authored
540 } else {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
541 print "Error creating [{$filePath}]\n";
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
542 FatalError();
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
543 }
544 }
545 }
546
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
547 function FatalError() {
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
548 global $isContinueOnError;
549 if ($isContinueOnError) {
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
550 print "Ignoring Error...\n";
551 return;
552 }
553 exit;
554 }
df61119 Dave Tompkins MAJOR overhaul of fbcmd_update
dtompkins authored
555
556 function PrintPref($key) {
557 global $fbcmdPrefs;
558 if ($key == 'mkdir_mode') {
559 print str_pad($key,25,' ') . '[' . decoct($fbcmdPrefs[$key]) . "]\n";
560 } else {
561 print str_pad($key,25,' ') . '[' . $fbcmdPrefs[$key] . "]\n";
562 }
563 }
564
565 function Trace($line) {
566 global $isTrace;
567 if ($isTrace) {
568 print "$line\n";
569 }
570 }
571
572 function TraceVar($varName) {
573 Trace("$varName = [" . $GLOBALS[$varName] . "]");
574 }
575
576 ////////////////////////////////////////////////////////////////////////////////
577 ////////////////////////////////////////////////////////////////////////////////
578
579 // Copied Directly from fbcmd.php
580
581 function SavePrefsContents() {
582 global $fbcmdPrefs;
583 if (isset($fbcmdPrefs['savepref_include_files'])) {
584 $includeFiles = $fbcmdPrefs['savepref_include_files'];
585 } else {
586 $includeFiles = false;
587 }
588 $fileContents = "<?php\n";
589 foreach ($fbcmdPrefs as $switchKey => $switchValue) {
590 if ($switchKey != 'prefs') {
591 if (($includeFiles)||(($switchKey != 'keyfile')&&($switchKey != 'postfile')&&($switchKey != 'mailfile'))) {
592 if ($switchKey == 'mkdir_mode') {
593 $fileContents .= " \$fbcmdPrefs['{$switchKey}'] = 0" . decoct($switchValue) . ";\n";
594 } else {
595 $fileContents .= " \$fbcmdPrefs['{$switchKey}'] = " . var_export($switchValue,true) . ";\n";
596 }
597 }
598 }
599 }
600 $fileContents .= "?>\n";
601 return $fileContents;
602 }
6eb5b8b Dave Tompkins Misc. cleanup of updater... including arguments
dtompkins authored
603
e5d0ad5 Dave Tompkins Initial UPDATE code
dtompkins authored
604 ?>
Something went wrong with that request. Please try again.