#!/usr/local/bin/php -Cq
<?php

/**
* Console script to generate PHPDoc API Docucumentation from the command line
*
* @author Tomas V.V.Cox <cox@idecnet.com>
* @author Derick Rethans <d.rethans@jdimedia.nl>
* @version $Id: phpdoc,v 1.7 2002/07/26 14:49:39 derick Exp $
*/

/*** CONFIGURATION ***/
// Directory with PHPDoc include files
$phpdoc_include_dir = '/usr/local/share/pear/PHPDoc';
// directory where your source files reside
$source_files = '/usr/local/share/pear';
// save the generated docs here:
$dest_apidoc_dir = '/usr/local/share/doc/pear';
// default template dir:
$template_dir = '/usr/local/share/pear/data';
$template     = 'default';
// force the deletion of the target apidocs directory
$dest_apidoc_delete = false;

/*** END CONF ***/

require_once 'PEAR.php';
require_once 'Console/Getopt.php';
require_once 'System.php';

error_reporting(E_ALL ^ E_NOTICE);

$argv = Console_Getopt::readPHPArgv();
$options = Console_Getopt::getopt($argv, "h?fv:t:e:s:d:");
if (PEAR::isError($options)) {
    usage($options);
}

if (sizeof($options[0]) < 1) {
    usage();
}
foreach ($options[0] as $opt) {
    $param = $opt[1];
    switch($opt[0]) {
        case 's':
            // directory where your source files reside
            $source_files = $param;
            break;
        case 'd':
            // save the generated docs here:
            $dest_apidoc_dir = $param;
            break;
        case 't':
            // template directory:
            $template_dir = $param;
            break;
        case 'e':
            // template:
            $template = $param;
            break;
        case 'f':
            // force the deletion of the target apidocs directory
            $dest_apidoc_delete = true;
            break;
        case 'h':
        case '?':
        default:
            usage();
            break;
    }
}

$start = time();

// WARNING: long runtimes! Make modifications
// to the php[3].ini if neccessary. A P3-500
// needs slightly more than 30 seconds to
// document phpdoc itself.
ini_set('max_execution_time', 0);

// Directory with PHPDoc include files
$phpdoc_include_dir = realpath($phpdoc_include_dir);
define('PHPDOC_INCLUDE_DIR', $phpdoc_include_dir . DIRECTORY_SEPARATOR);

// Directory with PHPDoc template files
$template_dir = realpath($template_dir);
define('PHPDOC_TEMPLATE_DIR', $template_dir . DIRECTORY_SEPARATOR);

// Important: set this to the Linebreak sign of your system!
$lnbreak = (OS_WINDOWS) ? "\r\n" : "\n"; // Mac not supported yet
define('PHPDOC_LINEBREAK', $lnbreak);

// Html tags allowed to be used in docs
define('PHPDOC_ALLOWEDHTMLTAGS', '<a>,<i>,<b>,<pre>,<ul>,<li>,<br>,<code>');

// PHPDoc tries to handle args by it self
unset($argc);
// Clean-up previous docs
if ($dest_apidoc_delete && @is_dir($dest_apidoc_dir)) {
    System::rm("-r $dest_apidoc_dir");
}

// main PHPDoc Include File
include PHPDOC_INCLUDE_DIR . 'prepend.php';

$doc = new Phpdoc;

// Sets the name of your application.
// The name of the application gets used in many default templates.
$doc->setApplication("PEAR Repository");

// directory where your source files reside:
$doc->setSourceDirectory($source_files);

// save the generated docs here:
$doc->setTarget($dest_apidoc_dir);

// use these templates:
$doc->setTemplateDirectory($template_dir.'/renderer/html/'. $template);

// source files have one of these suffixes:
$doc->setSourceFileSuffix(array ('php', 'inc'));

// parse and generate the xml files
$doc->parse();

// turn xml in to html using templates
$doc->render();

// Copy the standard files
$files = array('empty.html', 'index.html', 'phpdoc.css', 'phpdoc.dtd');
$DS = DIRECTORY_SEPARATOR;
$files_prefix = implode($DS, array('renderer', 'html', 'default', 'static'));
foreach($files as $file) {
    copy($template_dir . $DS . $files_prefix . $DS . $file,
         "$dest_apidoc_dir/$file");
}

print "API Docs for $source_files done in $dest_apidoc_dir\n\n";
printf("%d seconds needed\n\n", time() - $start);

function usage($obj = null)
{
    $stderr = fopen('php://stderr', 'w');
    if ($obj !== null) {
        fputs($stderr, $obj->getMessage());
    }
    fputs($stderr,
          "\nUsage: phpdoc [-h] [-f] [-i <dir>] [-s <dir>] [-d <dir>]\n".
          "Options:\n".
          "     -s <dir>  directory where your source files reside\n".
          "     -d <dir>  destination dir (save the generated docs here)\n".
          "     -t <dir>  template dir (path to the templates)\n".
          "     -e <name> template name (default='default')\n".
          "     -f        force the deletion of the destination directory\n".
          "     -h, -?    display help/usage (this message)\n".
          "You could also set defaults values editing the header of this script\n".
          "\n");
    fclose($stderr);
    exit;
}

?>
