<?php

// $Date: 2004/01/15 02:21:05 $
// $Revision: 1.25 $
// $Author: jcrocholl $

function icon($name) {
    global
$root, $fileroot;
    return
"<img class=\"nav\" " .
        
"width=\"16\" height=\"16\" ".
        
"alt=\"\" src=\"$root" .
        
"png/nav/$name.png\" />";
}

function
h($level, &$text) {
    global
$previous_par;
    
$previous_par = false;
    if (
$text) {
        
$result .= "<h$level>";
        
$result .= $text;
        
$result .= "</h$level>\n";
    }
    
$text = '';
    return
$result;
}

function
wrap($text) {
    global
$previous_par;
    if (
$text) {
        
$aclass = 'justify';
        if (
$previous_par) $aclass .= ' margin-top';
        
$text = "<p class=\"$aclass\">$text</p>\n";
        
$previous_par = true;
    }
    return
wordwrap($text);
}

function
par(&$text) {
    
$result = wrap($text);
    
$text = '';
    return
$result;
}

function
format_file($filename, $pp = true) {
    global
$previous_par;
    
$previous_par = $pp;
    
$input = file($filename);
    foreach (
$input as $line) {
        
$line = trim($line);
        if (!
$line) {
            
$result .= par($text);
        } elseif (
preg_match('/^=+$/', $line)) {
            
$result .= h(1, $text);
        } elseif (
preg_match('/^-+$/', $line)) {
            
$result .= h(2, $text);
        } elseif (
preg_match('/^-\s+(.+)$/', $line, $m)) {
            if (
$text) $text .= "<br/>\n";
            
$text .= "&bull;&nbsp;$m[1]";
        } else {
            if (
$text) $text .= ' ';
            
$text .= $line;
        }
    }
    
$result .= par($text);
    return
$result;
}

function
load_subdirs($dir) {
    
$linksfile = "$dir/Links";
    if (!
file_exists($linksfile)) return false;
    
$links = file($linksfile);
    
$subdirs = array_shift($links);
    if (
substr($subdirs, 0, 8) == 'subdirs:') {
        
$subdirs = trim(substr($subdirs, 8));
        
// print $subdirs;
        
return explode(' ', $subdirs);
    }
    return
false;
}

function
register_abstract($path, $file, $collect) {
    global
$root, $fileroot;
    global
$links, $abstracts;
    if (
$collect or !$links["$file/"])
        
$links["$file/"] = "$root$path/$file/";
    if (
$collect and file_exists("$fileroot$path/$file/Abstract"))
        
$abstracts[$file] = format_file("$fileroot$path/$file/Abstract", false);
    
// print count($abstracts);
}

function
dir_frame($path, $item, $here, $collect) {
    global
$root, $fileroot, $links, $abstracts;
    
// print $path;
    
if (preg_match('#^release/[^/]+$#', $path)) return;
    print
frame_head('black', icon('open'), "<a class=\"white\" href=\"$root$path\">$item</a>");
    
$subdirs = load_subdirs("$fileroot$path");
    if (
$dh = opendir("$fileroot$path")) {
        while ((
$file = readdir($dh)) !== false) {
            if (
$file[0] != '.' and $file != 'CVS'
                
and substr($file, 0, 7) != 'roemer-') // exclude release files
            
{
                
$type = filetype("$fileroot$path/$file");
                if (
$type == 'dir' and !$subdirs) {
                    
$files[$file] = '0dir';
                    
register_abstract($path, $file, $collect);
                } elseif (
$type == 'file') {
                    if (
preg_match('/(.+)\.ads$/', $file, $matches)) {
                        
$files[$matches[1]] = '1ads';
                        if (
$collect or !$links["$file/"])
                            
$links[$matches[1]] = "$root$path/$matches[1].html";
                    }  else if (
preg_match('/(.+)\.adb$/', $file, $matches)) {
                        if (!
$files[$matches[1]]) $files[$matches[1]] = '2adb';
                    }  else if (
preg_match('/(.+)\.php$/', $file, $matches)) {
                        if (!
$files[$matches[1]]) $files[$matches[1]] = '3php';
                        if (
$collect or !$links["$file/"])
                            
$links["$matches[1].php"] = "$root$path/$matches[1].php.html";
                    } elseif (
preg_match('/(.+)\.ado$/', $file, $matches)) {
                    } elseif (
preg_match('/(.+)\.ali$/', $file, $matches)) {
                    } elseif (
preg_match('/(.+)\.o$/', $file, $matches)) {
                    } elseif (
preg_match('/([^\.]+)/', $file, $matches)) {
                    } else {
                        
$files[$file] = '4other';
                    }
                }
            }
        }
        
closedir($dh);
        if (
$files or $subdirs) {
            if (
$files) ksort($files);
            if (
$subdirs) {
                foreach (
$subdirs as $subdir) {
                    
$files[$subdir] = '0dir';
                    
register_abstract($path, $subdir, $collect);
                }
            }
            
asort($files);
            foreach (
$files as $file => $type) {
                
$aclass = 'nav';
                if (
$type == '0dir') {
                    
$link = "$root$path/$file/";
                    
$text = "$file/";
                    
$icon = 'folder';
                    if (
$here == $file) {
                        
$aclass = 'here';
                        
$icon = 'open';
                    }
                } elseif (
$type == '1ads') {
                    
$link = "$root$path/$file.html";
                    
$text = $file;
                    
$icon = 'package';
                    if (
$here == $text) $aclass = 'here';
                    if (
$here == "$file.ads") $aclass = 'here';
                    if (
$here == "$file.adb") $aclass = 'here';
                } elseif (
$type == '2adb') {
                    
$link = "$root$path/$file.adb.html";
                    
$text = "$file.adb";
                    
$icon = 'adb';
                    if (
$here == $text) $aclass = 'here';
                } elseif (
$type == '3php') {
                    
$link = "$root$path/$file.php.html";
                    
$text = "$file.php";
                    
$icon = 'php';
                    if (
$here == $text) $aclass = 'here';
                } else {
// 4other
                    
$link = "$root$path/$file.html";
                    
$text = $file;
                    if (
$here == $text) $aclass = 'here';
                }
                
frame_row($rows, icon($icon), "<a class=\"$aclass\" href=\"$link\">$text</a>");
            }
        }
    }
    if (
$collect) {
        global
$absort;
        if (
$subdirs) {
            
$absort = $subdirs;
        } elseif (
$abstracts) {
            
$absort = array_keys($abstracts);
            
sort($absort);
        }
    }
    print
frame_tail($rows);
}

function
package_frame($path, $item, $here) {
    global
$root, $fileroot, $icons;
    
// print $path;
    
print frame_head('black', icon('package'), $item);
    if (
$here == "$item.ads") $aclass = 'here'; else $aclass = 'nav';
    
frame_row($rows, icon('ads'), "<a class=\"$aclass\" " .
              
"href=\"$root$path.ads.html\">$item.ads</a>");
    if (
is_file("$fileroot$path.adb")) {
        if (
$here == "$item.adb") $aclass = 'here'; else $aclass = 'nav';
        
frame_row($rows, icon('adb'), "<a class=\"$aclass\" " .
                  
"href=\"$root$path.adb.html\">$item.adb</a>");
    }
    print
frame_tail($rows);
}

function
nav($array, $path) {
    global
$root, $fileroot;
    
$item = array_shift($array);
    
$here = $array[0];
    if (!
$item) return;
    
$path .= $item;
    if (
$array) nav($array, "$path/", $array[1]);
    if (
is_dir("$fileroot$path")) {
        
dir_frame($path, $item, $here, count($array) == 1);
    } else {
        if (
preg_match('/^(.+?)(\.ad[sb])*$/', $path, $m)) {
            
// print $matches[1];
            
if (is_file("$fileroot$m[1].ads")) {
                
preg_match('/^.+\/(.+)$/', $m[1], $m2);
                
package_frame($m2[0], $m2[1], "$m2[1]$m[2]");
            }
        }
    }
}

function
main_frame($here) {
    global
$root, $fileroot, $icons;
    
// if (!$here) $aclass = 'here'; else
    
$aclass = 'white';
    print
frame_head('black', icon('home'),
                     
"<a class=\"$aclass\" href=\"$root\">Home</a>");
    if (
$here == 'release') $aclass = 'here'; else $aclass = 'nav';
    
frame_row($rows, icon('snapshot'), "<a class=\"$aclass\" href=\"$root"."release/\">" .
        
"Snapshots</a>");
    if (
$here == 'ada') $aclass = 'here'; else $aclass = 'nav';
    
frame_row($rows, icon('ada'), "<a class=\"$aclass\" href=\"$root"."ada/\">" .
        
"Ada Source</a>");
    if (
$here == 'php') $aclass = 'here'; else $aclass = 'nav';
    
frame_row($rows, icon('php'), "<a class=\"$aclass\" href=\"$root"."php/\">" .
        
"PHP Source</a>");
    if (
$here == 'fonts') $aclass = 'here'; else $aclass = 'nav';
    
frame_row($rows, icon('fonts'), "<a class=\"$aclass\" href=\"$root"."fonts/\">" .
        
"Fonts</a>");
    print
frame_tail($rows);
}

function
sf_frame($here) {
    global
$root, $fileroot, $icons, $path;
    
$aclass = 'white';
    print
frame_head('black', icon('sf'), "<a class=\"$aclass\" href=\"" .
                     
"http://sourceforge.net/\">Sourceforge</a>");
    
$aclass = 'nav';
    
frame_row($rows, icon('summary'), "<a class=\"$aclass\" href=\"" .
        
"http://sourceforge.net/projects/roemer/\">" .
        
"Summary</a>");
    
frame_row($rows, icon('download'), "<a class=\"$aclass\" href=\"" .
        
"http://sourceforge.net/project/showfiles.php?group_id=96129\">" .
        
"Downloads</a>");
    
$link = "http://cvs.sourceforge.net/viewcvs.py/roemer/roemer/";
    
$filename = implode('/', $path);
    if (
file_exists("$fileroot$filename")) $link .= $filename;
    elseif (
file_exists("$fileroot$filename.ads")) $link .= "$filename.ads";
    
frame_row($rows, icon('cvs'), "<a class=\"$aclass\" href=\"" .
        
"$link\">" .
        
"Browse&nbsp;CVS</a>");
    
frame_row($rows, icon('donate'), "<a class=\"$aclass\" href=\"" .
        
"http://sourceforge.net/donate/index.php?group_id=96129\">" .
        
"Donate</a>");
    
frame_row($rows, icon('activity'), "<a class=\"$aclass\" href=\"" .
        
"http://sourceforge.net/project/stats/?group_id=96129\">" .
        
"Activity</a>");
    print
frame_tail($rows);
    print
"<p class=\"sourceforge\">Hosted by<br/>\n";
    print
'<a href="http://sourceforge.net">';
    print
'<img src="http://sourceforge.net/sflogo.php?group_id=96129&amp;type=2" ';
    print
'width="125" height="37" border="0" alt="SourceForge.net Logo" /></a></p>';
}

// $array = $path;        // make a local copy
// array_pop($array);    // remove file name
nav($path, '', '');    // start recursion
main_frame($path[0]);
sf_frame('');

?>