#!/usr/bin/php $fbcmdUpdateVersion)||(!file_exists($updateFile))) { if (file_put_contents($updateFile,$contentsRemoteUpdater)) { Trace("creating [{$updateFile}]"); if ($newUpdateVersion > $fbcmdUpdateVersion) { if ($fbcmdPrefs['install_auto_restart']) { print "\nNewer update software downloaded [{$fbcmdUpdateVersion}] -> [{$newUpdateVersion}]\n"; print "\nattempting to restart...\n"; $execString = "php \"{$updateFile}\" \"{$specifiedBranch}\" \"{$installLibDir}\" $isTrace $isContinueOnError"; passthru($execString); exit; } else { if (realpath($thisProgram) == realpath($updateFile)) { print "\nUpdate INCOMPLETE: Restart this program (it has updated itself)\n\n"; } else { print "\nUpdate INCOMPLETE: run the NEW updater: [$updateFile]\n"; print "(you might want to remove this old one to avoid confusion)\n\n"; } FatalError(); } } } else { print "Fatal error: could not save [$updateFile]\n\n"; FatalError(); } } //////////////////////////////////////////////////////////////////////////////// $oldMainVersion = 'none'; $newMainVersion = ''; if (file_exists($mainFile)) { $oldFileContents = @file_get_contents($mainFile); preg_match ("/fbcmdVersion\s=\s'([^']+)'/",$oldFileContents,$matches); if (isset($matches[1])) { $oldMainVersion = $matches[1]; } else { $oldMainVersion = '???'; } } TraceVar('oldMainVersion'); //////////////////////////////////////////////////////////////////////////////// print "."; $fileList = GetGithub("filelist.txt"); $files = explode("\n",$fileList); foreach ($files as $f) { $g = preg_replace('/\s*\#.*$/','',$f); if ($g) { print "."; $contents = GetGithub($g); if ($g == 'fbcmd.php') { preg_match("/fbcmdVersion\s=\s'([^']+)'/",$contents,$matches); if (isset($matches[1])) { $newMainVersion = $matches[1]; } else { print "Non-fatal error: could not determine new version\n"; $newMainVersion = '???'; } } } } print "\n"; //////////////////////////////////////////////////////////////////////////////// print "\nUpdate: COMPLETE!\n\n"; print "fbcmd version: [{$oldMainVersion}] --> [{$newMainVersion}]\n"; if ($specifiedBranch != 'install') { if (!$fbcmdPrefs['install_copy_to_bin']) { if (stripos(getenv('PATH'),substr($installLibDirOS,0,strlen($installLibDirOS)-1)) === false) { print "\nNote: Your PATH does not appear to include {$installLibDirOS}\n"; if ($isWindows) { print "(right click) My Computer -> Properties -> Advanced -> Environment Variables\n"; print "Edit the PATH entry and add: ;{$installLibDirOS}\n"; } else { print "Add the following line to your ~/.bash_profile file:\n"; print " PATH=\$PATH:{$installLibDirOS}; export PATH\n"; } } } } if (realpath($thisProgram) != realpath($updateFile)) { print "\nNote: fbcmd_update.php is now at [{$updateFile}]\n"; print "so you can remove the old one at [" . realpath($thisProgram) . "]\n\n"; } if ($oldMainVersion == 'none') { print "\ntype " . $fbcmdPrefs['install_script_name'] . " to begin\n\n"; } print "\n"; exit; function DeleteFileOrDirectory($dir) { # snagged from http://ca3.php.net/rmdir Trace("deleting [{$dir}]"); if (!file_exists($dir)) return true; if (!is_dir($dir)) { if (unlink($dir)) { return true; } else { print "Could Not Delete File: [{$dir}]\n"; FatalError(); } } foreach (scandir($dir) as $item) { if ($item == '.' || $item == '..') continue; if (!DeleteFileOrDirectory($dir.DIRECTORY_SEPARATOR.$item)) return false; } if (rmdir($dir)) { return true; } else { print "Could Not Delete Directory: [{$dir}]\n"; FatalError(); } } function GetGithub($filename, $save = true) { global $branch; global $installLibDir; $fileSrc = "https://raw.github.com/dtompkins/fbcmd/{$branch}/{$filename}"; $fileDest = "{$installLibDir}{$filename}"; $fileContents = @file_get_contents($fileSrc); if ($fileContents) { Trace("downloading: [$fileSrc}]"); } else { print "Could not download: [{$fileSrc}]\n"; FatalError(); } if ($save) { CheckPath(dirname($fileDest)); if (@file_put_contents($fileDest,$fileContents)) { Trace("saving: [{$fileDest}]"); } else { print "Could not save: [{$fileDest}]\n"; FatalError(); } } return $fileContents; } function CleanPath($curPath) { $path = $curPath; if ($path == '') { $path = './'; } $path = str_replace('\\', '/', $path); if ($path[strlen($path)-1] != '/') { $path .= '/'; } return $path; } function CheckPath($filePath) { global $fbcmdPrefs; if (!file_exists($filePath)) { if (mkdir($filePath,octdec($fbcmdPrefs['mkdir_mode']),true)) { Trace("creating [{$filePath}]"); } else { print "Error creating [{$filePath}]\n"; FatalError(); } } } function MakeLibDir() { global $fbcmdPrefs; global $isWindows; global $isSudo; global $sudoUser; $dir = $fbcmdPrefs['install_lib_dir']; if (!file_exists($dir)) { if (mkdir($dir,octdec($fbcmdPrefs['install_lib_mkdir_mode']),true)) { Trace("creating directory [{$dir}]"); } else { print "Error: cound not create directory: [{$dir}]\n"; FatalError(); } } if (!$isWindows) { if ($isSudo) { if (isset($sudoUser)) { if (chown($dir,$sudoUser)) { Trace ("chown [{$dir}] [{$sudoUser}]"); } else { print "error chown: [{$dir}] [{$sudoUser}] (non-fatal)\n"; } } if (chmod($dir,octdec($fbcmdPrefs['install_lib_mkdir_mode']))) { Trace ("chmod [{$dir}]"); } else { print "error chmod: [{$dir}] (non-fatal)\n"; } } } } function FatalError() { global $isContinueOnError; if ($isContinueOnError) { print "Ignoring Error...\n"; return; } exit; } function PrintPref($key) { global $fbcmdPrefs; print str_pad($key,25,' ') . '[' . $fbcmdPrefs[$key] . "]\n"; } function Trace($line) { global $isTrace; if ($isTrace) { print "$line\n"; } } function TraceVar($varName) { Trace("$varName = [" . $GLOBALS[$varName] . "]"); } //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// // Copied Directly from fbcmd.php function SavePrefsContents() { global $fbcmdPrefs; $fileContents = " $switchValue) { if ($switchKey != 'prefs') { $fileContents .= " \$fbcmdPrefs['{$switchKey}'] = " . var_export($switchValue,true) . ";\n"; } } $fileContents .= "?>\n"; return $fileContents; } ?>