<?php 

// $Date: 2004/01/25 07:11:12 $
// $Revision: 1.32 $
// $Author: jcrocholl $

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

require(
'txt2web.php');

function 
load_subdirs($path, $dir, $collect) {
    global 
$root, $icons, $links;
    
$subdirsfile = "$dir/Subdirs";
    if (!
file_exists($subdirsfile)) return false;
    
$input = file($subdirsfile);
    foreach (
$input as $line) {
        if (
preg_match('/^\s*(\S+)\s*(\S*)\s*(\S*.*\S*)\s*$/', $line, $m)) {
            if (
$m[2]) $icons[$m[1]] = $m[2];
            if (!
$m[3]) $m[3] = "$m[1]/";
            
$result[$m[1]] = $m[3];
            
// if ($collect) 
            
$links[$m[3]] = "$root$path/$m[1]/";
            
// print "$m[1] $m[3]<br/>\n";
        
}
    }
    return 
$result;
}

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")) {
        
load_links($mylinks, "$fileroot$path/$file/");
        
$abstracts[$file] = format_file("$fileroot$path/$file/", 
                                        
'Abstract', $mylinks, false);
    }
    
// print count($abstracts);
}

function 
cmp_file($a, $b) {
    global 
$files;
    
$fa = $files[$a];
    
$fb = $files[$b];
    
// print "$a $fa $b $fb<br/>\n";
    
if ($fa < $fb) return -1;
    if (
$fa > $fb) return 1;
    if (
$a < $b) return -1;
    if (
$a > $b) return 1;
    return 
0;
}

function 
maxlencut(&$text, $max) {
    if (
strlen($text) > $max) 
        
$text = substr($text, 0, $max);
}

function 
maxlen_(&$text, $max) {
    
$parts = explode('_', $text);
    while (
strlen($text) > $max) {
        
$max_index = -1;
        
$max_length = 0;
        foreach (
$parts as $index => $part) {
            
$length = strlen($part);
            if (
$length > $max_length) {
                
$max_index = $index;
                
$max_length = $length;
            }
        }
        
$part = $parts[$max_index];
        
maxlencut($part, $max_length - 1);
        
$parts[$max_index] = $part;
        
$text = implode('_', $parts);
    }
}

$navmaxlen = 14;
function 
maxlen(&$text, $max = 14) {
    
$parts = explode('-', $text);
    while (
strlen($text) > $max) {
        
$max_index = -1;
        
$max_length = 0;
        foreach (
$parts as $index => $part) {
            
$length = strlen($part);
            if (
$length > $max_length) {
                
$max_index = $index;
                
$max_length = $length;
            }
        }
        
$part = $parts[$max_index];
        
maxlen_($part, $max_length - 1);
        
$parts[$max_index] = $part;
        
$text = implode('-', $parts);
    }
}

function 
dir_frame($path, $item, $here, $collect) {
    global 
$root, $fileroot, $links, $abstracts, $icons, $files;
    
$files = array();
    
// print $path;
    
if (preg_match('#^release/[^/]+$#', $path)) return;
    
$frame_head = frame_head('black', icon('open'), 
        
"<a class=\"white\" href=\"$root$path\">$item</a>");
    
$subdirs = load_subdirs($path, "$fileroot$path", $collect);
    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 (
$subdirs) {
            foreach (
$subdirs as $subdir => $text) {
                
$aclass = 'nav'; $icon = 'folder';
                if (
$here == $subdir) {
                    
$aclass = 'here';
                    
$icon = 'open';
                }
                if (
$icons[$subdir]) $icon = $icons[$subdir];
                
frame_row($rows, icon($icon), 
                    
"<a class=\"$aclass\" href=\"$root$path/$subdir/\">$text</a>");
                
$files[$subdir] = '0dir';
                
register_abstract($path, $subdir, $collect);
            }
        } elseif (
$files) {
            
uksort($files, "cmp_file");
            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';
                }
                
maxlen($text);
                
frame_row($rows, icon($icon), 
                    
"<a class=\"$aclass\" href=\"$link\">$text</a>");
            }
        }
    }
    if (
$collect) {
        global 
$absort, $abtitles;
        if (
$subdirs) {
            
$absort = array_keys($subdirs);
            
$abtitles = $subdirs;
        } elseif (
$abstracts) {
            
$absort = array_keys($abstracts);
            
sort($absort);
        }
    }
    if (
$rows) print $frame_head . frame_tail($rows);
}

function 
package_frame($path, $item, $here) {
    global 
$root, $fileroot, $icons;
    global 
$navmaxlen;
    
$text = $item;
    
// print $path;
    
maxlen($text);
    print 
frame_head('black', icon('package'), $text);
    
maxlen($text, $navmaxlen - 4);
    if (
$here == "$item.ads") $aclass = 'here'; else $aclass = 'nav'; 
    
frame_row($rows, icon('ads'), "<a class=\"$aclass\" " . 
              
"href=\"$root$path.ads.html\">$text.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\">$text.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 == 'showcase') $aclass = 'here'; else $aclass = 'nav'; 
    
frame_row($rows, icon('showcase'), "<a class=\"$aclass\" href=\"$root"."showcase/\">" . 
        
"Showcase</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('');

?>