#!/usr/bin/php
<?php
/**
 * PHP Fortune 
 *
 * A command-line script for pulling a random fortune from a fortune file.
 * Defaults to /usr/share/fortune/fortunes as the fortune file, and, if no path
 * information is given in the fortune file, /usr/share/fortune is assumed as
 * the base directory.
 *
 * Usage:
 * <code>
 * # phpFortune fortunes
 * # phpFortune fortunes starwars hitchhiker
 * # phpFortune
 * </code>
 *
 * @author    Matthew Weier O'Phinney <mweierophinney@gmail.com>
 * @copyright 2005, Matthew Weier O'Phinney
 * @version   @package_version@
 */
require_once 'File/Fortune.php';

$files = null;
if (isset($_SERVER['argv'])) {
    $files = $_SERVER['argv'];
    array_shift($files);
}
if (empty($files)) {
    $files = array('fortunes');
}

foreach ($files as $key => $file) {
    if (!strstr($file, '/')) {
        $file = '/usr/share/fortune/' . $file;
        $files[$key] = $file;
    }
    if (!file_exists($file)) {
        unset($files[$key]);
    }
}

if (empty($files)) {
    echo "No files specified, or no files found\n";
    exit(1);
}

try {
    $fortune = File_Fortune::getRandomFromSet($files);
    echo $fortune;
} catch (File_Fortune_Exception $e) {
    echo "Unable to retrieve fortune\n";
}
?>
