#!/usr/bin/php
<?
/*
    Raydium - CQFD Corp.
    http://raydium.org/
    Released under both BSD license and Lesser GPL library license.
    See "license.txt" file.
*/

// This script generates a Wiki(ni) style documentation from comments of
// all header files of Raydium (raydium/header/*.h)

$page="http://wiki.raydium.org/wiki/RaydiumApiReference";

$intro="
======Raydium API Reference======

=====CQFD Corp.=====

This document is the most up-to-date version. **This is a work in progress**:
there's again some errors and wrong informations. Try, wait, or contribute ;)

\"\"<a href=$page#chapters>Index of chapters</a>\"\"
\"\"<a href=$page#index>Index of all Raydium functions</a>\"\"

----
This document is autogenerated, any change will be lost,
use RaydiumApiReferenceComments for any need.
{DATE}, for Raydium **{VERSION}**
----
";


function cleanName($str)
{
$res="";
$allowed="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";

$str=explode('(',$str);
$str=$str[0];

for($i=0;$i<strlen($str);$i++)
    if(strpos($allowed,$str[$i])!==false)
        $res.=$str[$i];

return $res;
}

function getTagLine($tag,$lines,$from=0)
{
for($i=$from;$i<count($lines);$i++)
    {
    $l=$lines[$i];
    if(substr(trim($l),0,strlen($tag))==$tag)
        return $i;
    }
return -1;
}

function getVersion()
{
$file="../common.h";
$f=file($file);
$maj=getTagLine("#define RAYDIUM_MAJOR",$f);
$min=getTagLine("#define RAYDIUM_MINOR",$f);

$maj=str_replace("\t"," ",trim($f[$maj]));
$min=str_replace("\t"," ",trim($f[$min]));

$maj=explode(" ",$maj);
$min=explode(" ",$min);
$maj=$maj[count($maj)-1];
$min=$min[count($min)-1];

return sprintf("%d.%03d",$maj,$min);
}

function getMain($filename,$offset)
{
$f=file($filename);
$l=getTagLine("/*=",$f);
if($l==-1)
    return -1;
$res=trim($f[$l+$offset]);
return $res;
}

function getPriority($filename)
{
$res=trim(getMain($filename,2));
if(is_numeric($res))
    return $res;
else
    return -1;
}

function getTitle($filename)
{
return trim(getMain($filename,1));
}



function getHeaders($directory)
{
$res=array();
if (is_dir($directory))
    {
        if ($dh = opendir($directory))
        {
            while (($file = readdir($dh)) !== false)
            {
                if(substr($file,-2)==".h")
                {
                    $res[]=$file;
                }
            }
        closedir($dh);
        }
    }
    else echo "'$directory' is not a directory";
return $res;
}


$chapters=array();
function h1($str,$addchap,$file="")
{
global $chapters;
if($addchap)
    {
    echo '""'."<a name=\"$file\"></a>".'""';
    $chapters[$file]=$str;
    $i++;
    }
echo "\n=====$str:=====\n";
}

function h2($str)
{
echo "====$str:====\n";
}

function body($str)
{
echo $str."\n\n";
}

function intro($str)
{
$str=str_replace("{DATE}","Generated: ".date("Y-m-d H:i:s"),$str);
$str=str_replace("{VERSION}",getVersion(),$str);
echo $str;
}

$index=array();
function addToIndex($f)
{
global $index;

$p=strpos($f,"raydium_");
if($p!==false)
    $f=substr($f,$p);
else
    $f="unsupported - $f";

$cf=cleanName($f);
$index[$cf]=$f."|$cf";
return $cf;
}

// Main

$id="";
$files=getHeaders(".");
//var_dump($files);
if($files==-1)
    die("No header found");

unset($sorted);
for($i=0;$i<count($files);$i++)
    {
    $file=$files[$i];
    $p=getPriority($file);
    if($p==-1)
        $p=999000+$i;

    while(isset($sorted[$p]))
        $p++;
    $sorted[$p]=$file;
    }
ksort($sorted);
$sorted=array_values($sorted);
//var_dump($sorted);

// Files are sorted, now

intro($intro);

for($i=0;$i<count($sorted);$i++)
    {
    $file=$sorted[$i];
    $title=getTitle($file);

    if($title==-1)
        {
        h1(($i+1)." no documentation for $file",true,$file);
        continue;
        }

    h1(($i+1)." $title",true,$file);

    $f=file($file);
    $last=0;
    $n=0;
    while(($l=getTagLine("/**",$f,$last))!=-1)
        {
        $title=trim($f[$l-1]);
        if($title=="")
            $title="// unknown item";

        // types:
        // 1 - Comment (//)
        // 2 - Macro (#)
        // 3 - Code (...)
        $type=3;
        if($title[0]=="/")
            {
            $type=1;
            $com_type=$title[1];
            $title=trim(substr($title,2));
            if($com_type=='*')
                {
                $title=str_replace( "*/", "", $title);
                $title=trim($title);
                }
            }

        if($title[0]=="#")
            {
            $type=2;
            $pos=strpos($title,")");
            if($pos)
                {
                $title=substr($title,0,$pos+1);
                }
            $title=trim(str_replace("#define ","",$title))." (macro)";

            }

        if($type==3)
            {
            if(substr($title,0,7)=="extern ")
                $title=trim(substr($title,7));
            if(substr($title,0,9)=="__rayapi ")
                $title=trim(substr($title,9));
            if($title[strlen($title)-1]==";")
                $title=substr($title,0,-1);
            $title=trim(str_replace("**","* *",$title));
            }

        if($type==2 || $type==3)
            $id=addToIndex($title);

        h2('""'."<a name=\"$id\"></a>".'""'.($i+1).".".($n+1)." $title");

        $last=$l+1;
        $end=getTagLine("**/",$f,$last);
        if($end==-1)
            die("expected '**/' (started line $l)");

        unset($body);
/*      for($j=$l+1;$j<$end;$j++)
            {
            $lj=trim($f[$j]);
            if($lj=="")
                $lj="\n\n";
            else
                $lj.=" ";
            $body[]=$lj;
            }
        $str=implode("",$body);*/
        for($j=$l+1;$j<$end;$j++)
            {
            $lj=trim($f[$j]);
            $body[]=$lj;
            }
        $str=@implode("\n",$body);
        body($str);

        $last=$end+1;
        $n++;
        }
    }


h1('""<a name=chapters></a>""Chapters',false);
foreach($chapters as $key => $val)
    {
    echo('====""'."<a href=\"$page#$key\">$val</a>".'""====')."\n";
    }

sort($index);
h1('""<a name=index></a>""Index',false);
for($i=0;$i<count($index);$i++)
    {
    $j=explode("|",$index[$i]);
    $k=$j[0];
    $l=$j[1];
    echo '""'."<a href=\"$page#$l\"><tt>$k</tt></a>".'""'."\n";
    }