Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 309 lines (278 sloc) 10.273 kB
e5d0ad5 @dtompkins 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
24
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
25 print "\nFBCMD Update Utility -- version 2.2\n\n";
c692a1c @dtompkins more updates to fbcmd_updater
dtompkins authored
26 print "http://fbcmd.dtompkins.com/update for help\n\n";
27
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
28 print "syntax: php fbcmd_update.php [BRANCH] [DIRECTORY] [CONTINUE_ON_ERR]\n";
29 print "default: php fbcmd_update.php master . 0\n\n";
30
31 $specifiedBranch = '';
c692a1c @dtompkins more updates to fbcmd_updater
dtompkins authored
32 if (isset($argv[1])) {
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
33 $specifiedBranch = strtolower($argv[1]);
34 }
35
36 if (isset($argv[2])) {
37 $installDir = CleanPath($argv[2]);
c692a1c @dtompkins more updates to fbcmd_updater
dtompkins authored
38 } else {
39 $installDir = CleanPath('.');
40 }
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
41
42 $continueOnError = false;
43 if (isset($argv[3])) {
44 if ($argv[3]) {
45 $continueOnError = true;
46 }
47 }
48
49 CheckPath($installDir);
50 $fullPath = realpath($installDir);
231896c @dtompkins added messages for adding fbcmd to your PATH
dtompkins authored
51 if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
52 $fullPath = str_replace('/', '\\', $fullPath);
231896c @dtompkins added messages for adding fbcmd to your PATH
dtompkins authored
53 }
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
54 print "Installation folder: {$fullPath}\n";
55
56 $fbcmdBaseDir = getenv('FBCMD');
57 if ($fbcmdBaseDir) {
58 $isFbcmdSet = true;
59 print "Found: environment variable FBCMD={$fbcmdBaseDir}\n";
60 $fbcmdBaseDir = CleanPath($fbcmdBaseDir);
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
61 } else {
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
62 $isFbcmdSet = false;
63 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
64 if (getenv('USERPROFILE')) {
65 $fbcmdBaseDir = CleanPath(getenv('USERPROFILE')) . 'fbcmd/';
66 } else {
67 $fbcmdBaseDir = 'c:/fbcmd/';
68 }
69 } else {
70 $fbcmdBaseDir = CleanPath(getenv('HOME')) . '.fbcmd/';
71 }
72 print "Not found: environment variable FBCMD (default is {$fbcmdBaseDir})\n";
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
73 }
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
74
07b4db0 @dtompkins fbcmd_update.pl Version 2
dtompkins authored
75 // set some simple defaults:
76 $fbcmdPrefs['mkdir_mode'] = 0777;
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
77 $fbcmdPrefs['update_branch'] = 'master';
07b4db0 @dtompkins fbcmd_update.pl Version 2
dtompkins authored
78
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
79 if (file_exists("{$fbcmdBaseDir}prefs.php")) {
80 print "Found: preference file: {$fbcmdBaseDir}prefs.php\n";
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
81 print "Loading: preferences...\n";
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
82 include("{$fbcmdBaseDir}prefs.php");
c692a1c @dtompkins more updates to fbcmd_updater
dtompkins authored
83 } else {
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
84 print "Not found: preference file: [{$fbcmdBaseDir}prefs.php] (using defaults)\n";
c692a1c @dtompkins more updates to fbcmd_updater
dtompkins authored
85 }
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
86
c692a1c @dtompkins more updates to fbcmd_updater
dtompkins authored
87 $defaultBranch = strtolower($fbcmdPrefs['update_branch']);
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
88 if ($specifiedBranch) {
89 $branch = $specifiedBranch;
90 if ($defaultBranch != $branch) {
91 print "Overriding default branch: [$defaultBranch] with [$branch]\n";
92 } else {
93 print "Specified and default branch are both: [$branch]\n";
94 }
c692a1c @dtompkins more updates to fbcmd_updater
dtompkins authored
95 } else {
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
96 $branch = $defaultBranch;
c692a1c @dtompkins more updates to fbcmd_updater
dtompkins authored
97 print "Using default branch: [$branch]\n";
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
98 }
99b59b7 @dtompkins more updates to fbcmd_updater
dtompkins authored
99
f85c264 @dtompkins minor... added 'none' as oldVersion
dtompkins authored
100 $oldVersion = 'none';
99b59b7 @dtompkins more updates to fbcmd_updater
dtompkins authored
101 $newVersion = '';
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
102
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
103 if (file_exists("{$installDir}fbcmd.php")) {
104 print "Found: existing fbcmd: {$installDir}fbcmd.php\n";
105 $oldFbcmdFile = @file_get_contents("{$installDir}fbcmd.php");
106 preg_match ("/fbcmdVersion\s=\s'([^']+)'/",$oldFbcmdFile,$matches);
107 if (isset($matches[1])) {
99b59b7 @dtompkins more updates to fbcmd_updater
dtompkins authored
108 $oldVersion = $matches[1];
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
109 } else {
99b59b7 @dtompkins more updates to fbcmd_updater
dtompkins authored
110 print "Non-fatal error: could not determine old version\n";
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
111 }
112 } else {
113 print "Not found: existing fbcmd.php file (OK if new installation)\n";
114 }
115
116 $currentUpdater = $argv[0];
9f9d124 @dtompkins Update Utility -- Version 1
dtompkins authored
117 $contentsCurrentUpdater = '';
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
118 if (file_exists("{$currentUpdater}")) {
9f9d124 @dtompkins Update Utility -- Version 1
dtompkins authored
119 print "Loading current updater: [{$currentUpdater}]... ";
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
120 $contentsCurrentUpdater = @file_get_contents($currentUpdater);
121 if ($contentsCurrentUpdater) {
122 print "ok\n";
123 } else {
124 print "fail! (non-fatal)\n";
125 }
126 } else {
9f9d124 @dtompkins Update Utility -- Version 1
dtompkins authored
127 print "Non-fatal error: can't locate current updater: [{$currentUpdater}]\n";
128 $localUpdater = "{$installDir}fbcmd_update.php";
129 if (file_exists("{$localUpdater}")) {
130 print "Loading local updater: [{$localUpdater}]... ";
131 $contentsCurrentUpdater = @file_get_contents($localUpdater);
132 if ($contentsCurrentUpdater) {
133 print "ok\n";
134 } else {
135 print "fail! (non-fatal)\n";
136 }
137 } else {
138 print "Non-fatal error: can't locate local updater: [{$currentUpdater}]\n";
139 }
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
140 }
141
142 $contentsRemoteUpdater = GetGithub("fbcmd_update.php",false);
143
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
144 if ($contentsCurrentUpdater == $contentsRemoteUpdater) {
145 print "Current or local updater is identical\n";
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
146 } else {
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
147 print "Current or local updater does not match\n";
9f9d124 @dtompkins Update Utility -- Version 1
dtompkins authored
148 print "Saving new updater: [{$installDir}fbcmd_update.php]...";
149 if (@file_put_contents("{$installDir}fbcmd_update.php",$contentsRemoteUpdater)) {
150 print "ok\n";
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
151 print "\nUpdate INCOMPLETE: restart with new updater: {$installDir}fbcmd_update.php\n\n";
152 FatalError();
9f9d124 @dtompkins Update Utility -- Version 1
dtompkins authored
153 } else {
154 print "fail!\n";
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
155 print "Fatal error: could not save [{$installDir}fbcmd_update.php]\n\n";
156 FatalError();
9f9d124 @dtompkins Update Utility -- Version 1
dtompkins authored
157 }
158 }
99b59b7 @dtompkins more updates to fbcmd_updater
dtompkins authored
159
9f9d124 @dtompkins Update Utility -- Version 1
dtompkins authored
160 $fileList = GetGithub("filelist.txt");
161 $files = explode("\n",$fileList);
162 foreach ($files as $f) {
163 $g = preg_replace('/\s*\#.*$/','',$f);
164 if ($g) {
99b59b7 @dtompkins more updates to fbcmd_updater
dtompkins authored
165 $contents = GetGithub($g);
166 if ($g == 'fbcmd.php') {
167 preg_match("/fbcmdVersion\s=\s'([^']+)'/",$contents,$matches);
168 if (isset($matches[1])) {
169 $newVersion = $matches[1];
170 } else {
171 print "Non-fatal error: could not determine new version\n";
f85c264 @dtompkins minor... added 'none' as oldVersion
dtompkins authored
172 $newVersion = '???';
99b59b7 @dtompkins more updates to fbcmd_updater
dtompkins authored
173 }
174 }
9f9d124 @dtompkins Update Utility -- Version 1
dtompkins authored
175 }
176 }
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
177
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
178 $comment1 = "This script file was auto-generated by fbcmd_update.php\n";
179 $comment2 = "You should add the folder [{$fullPath}] to your PATH";
231896c @dtompkins added messages for adding fbcmd to your PATH
dtompkins authored
180 $comment3 = "or copy this file to a pathed folder";
181
f74be97 @dtompkins added generation of fbcmd.bat
dtompkins authored
182 $execPath = realpath("{$installDir}fbcmd.php");
be6edd9 @dtompkins adding +x to bash script
dtompkins authored
183 if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
f74be97 @dtompkins added generation of fbcmd.bat
dtompkins authored
184 $contentsBatch = "@echo off\n";
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
185 $contentsBatch .= "REM *** {$comment1}\n";
f74be97 @dtompkins added generation of fbcmd.bat
dtompkins authored
186 $contentsBatch .= "REM *** {$comment2}\n";
187 $contentsBatch .= "REM *** {$comment3}\n";
188 $contentsBatch .= "php $execPath %*\n";
189 $batchName = "{$installDir}fbcmd.bat";
190 } else {
191 $contentsBatch = "#! /bin/bash\n";
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
192 $contentsBatch .= "# *** {$comment1}\n";
f74be97 @dtompkins added generation of fbcmd.bat
dtompkins authored
193 $contentsBatch .= "# *** {$comment2}\n";
194 $contentsBatch .= "# *** {$comment3}\n";
b825122 @dtompkins Fixed bash script, added column width
dtompkins authored
195 $contentsBatch .= "php $execPath $* -print_wrap_width=$(tput cols)\n";
f74be97 @dtompkins added generation of fbcmd.bat
dtompkins authored
196 $batchName = "{$installDir}fbcmd";
197 }
198 print "Generating script file: [{$batchName}]...";
199 if (@file_put_contents("{$batchName}",$contentsBatch)) {
200 print "ok\n";
be6edd9 @dtompkins adding +x to bash script
dtompkins authored
201 if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') {
202 print "Making script file executable: [{$batchName}]...";
203 if (chmod($batchName,0755)) {
204 print "ok\n";
205 } else {
231896c @dtompkins added messages for adding fbcmd to your PATH
dtompkins authored
206 print "fail (do it maually: chmod +x fbcmd)\n";
be6edd9 @dtompkins adding +x to bash script
dtompkins authored
207 }
bcfaa49 @dtompkins fix typo
dtompkins authored
208 }
f74be97 @dtompkins added generation of fbcmd.bat
dtompkins authored
209 } else {
210 print "fail!\n";
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
211 FatalError();
f74be97 @dtompkins added generation of fbcmd.bat
dtompkins authored
212 }
213
231896c @dtompkins added messages for adding fbcmd to your PATH
dtompkins authored
214 $currentPath = getenv('PATH');
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
215 if (stripos($currentPath,$fullPath)) {
216 print "Found: current path appears to include {$fullPath}\n";
231896c @dtompkins added messages for adding fbcmd to your PATH
dtompkins authored
217 $showPath = false;
218 } else {
219 $showPath = true;
220 }
221
222 print "\nUpdate: COMPLETE!\n\n";
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
223 print "FBCMD Version: [{$oldVersion}] --> [{$newVersion}]\n";
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
224
231896c @dtompkins added messages for adding fbcmd to your PATH
dtompkins authored
225 if ($showPath) {
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
226 print "\nNote: Your PATH does not appear to include {$fullPath}\n";
227 print "To add {$fullPath} to your path:\n";
231896c @dtompkins added messages for adding fbcmd to your PATH
dtompkins authored
228 if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {
229 print "(right click) My Computer -> Properties -> Advanced -> Environment Variables\n";
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
230 print "Edit the PATH entry and add: ;{$fullPath}\n";
231896c @dtompkins added messages for adding fbcmd to your PATH
dtompkins authored
231 } else {
232 print "Add the following line to your ~/.bash_profile file:\n";
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
233 print " PATH=$PATH:{$fullPath}; export PATH\n";
234 }
235 }
236
237 if (!$isFbcmdSet) {
238 print "\nNote: The environment variable FBCMD is not set\n";
239 if (file_exists("{$fbcmdBaseDir}sessionkeys.txt")) {
240 print "fbcmd is storing your user files in: {$fbcmdBaseDir}\n";
241 } else {
242 print "By default, fbcmd will store your user files in: {$fbcmdBaseDir}\n";
231896c @dtompkins added messages for adding fbcmd to your PATH
dtompkins authored
243 }
244 }
245
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
246 exit;
247
248 function GetGithub($filename, $save = true) {
249 global $branch;
250 global $installDir;
251 $fileSrc = "http://github.com/dtompkins/fbcmd/raw/{$branch}/{$filename}";
9b8b804 @dtompkins ongoing updater changes... part 1
dtompkins authored
252 $fileDest = "{$installDir}{$filename}";
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
253 print "Downloading: [$fileSrc]... ";
254 $fileContents = @file_get_contents($fileSrc);
255 if ($fileContents) {
256 print "ok\n";
257 } else {
258 print "fail!\n";
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
259 FatalError();
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
260 }
261 if ($save) {
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
262 CheckPath(dirname($fileDest));
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
263 print "Saving: [{$fileDest}]... ";
264 if (@file_put_contents($fileDest,$fileContents)) {
265 print "ok\n";
266 } else {
267 print "fail!\n";
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
268 FatalError();
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
269 }
270 }
271 return $fileContents;
272 }
273
07b4db0 @dtompkins fbcmd_update.pl Version 2
dtompkins authored
274 function CleanPath($curPath) {
9f9d124 @dtompkins Update Utility -- Version 1
dtompkins authored
275 $path = $curPath;
276 if ($path == '') {
277 $path = './';
278 }
279 $path = str_replace('\\', '/', $path);
280 if ($path[strlen($path)-1] != '/') {
281 $path .= '/';
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
282 }
9f9d124 @dtompkins Update Utility -- Version 1
dtompkins authored
283 return $path;
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
284 }
285
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
286 function CheckPath($filePath) {
9f9d124 @dtompkins Update Utility -- Version 1
dtompkins authored
287 global $fbcmdPrefs;
288 if (!file_exists($filePath)) {
289 print "Creating Directory: [{$filePath}]... ";
07b4db0 @dtompkins fbcmd_update.pl Version 2
dtompkins authored
290 if (mkdir($filePath,$fbcmdPrefs['mkdir_mode'],true)) {
9f9d124 @dtompkins Update Utility -- Version 1
dtompkins authored
291 print "ok\n";
292 } else {
293 print "fail!\n";
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
294 FatalError();
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
295 }
296 }
297 }
298
6eb5b8b @dtompkins Misc. cleanup of updater... including arguments
dtompkins authored
299 function FatalError() {
300 global $continueOnError;
301 if ($continueOnError) {
302 print "Ignoring Error...\n";
303 return;
304 }
305 exit;
306 }
307
e5d0ad5 @dtompkins Initial UPDATE code
dtompkins authored
308 ?>
Something went wrong with that request. Please try again.