#!/usr/bin/perl

$IMGDIR="images";

&do_tree($cvsroot);

print "<!-- ENDOFTREE -->\n";

#
# END OF MAIN
#

sub do_tree {
  local($search)=$_[0];
  local($dir,$user,$group,$wd,$d,$f,$initdepth,$depth);
  local(@curdirs);
  local(@curfils);

#
# FORMAT:
#
# INDENT=2
#
# http://foo.bar.com/blah Link Text | nonlink text
#   http://foo.bar.com/blah/subdir Link Text &#124; with a pipe
#   * Text without a link
#

  $indentsize=0;  #autodetect
  $odepth=0;
  @parlist=("root");
  $tree{"root","kidcount"}=0;
  $tree{"root","depth"}=0;
  $tree{"root","parent"}="root";
  $tree{"root","text"}="fictitious entry";
  while(<>) {
    next if (/^\s*#/);   #skip comment lines
    next if (/^$/);      #skip blank lines
    if (/^INDENT=/) {
      chop;
      s/INDENT=//;
      $indentsize=$_;
    } else {
      chop;
      ++$key;
      ($url,$text)=split(' ',$_,2);
      ($text,$othertext)=split(/\|/,$text,2);
      push(@keylist,$key);
      $spaces=$_;
      $spaces =~ s/[^ ].*$//;
      if (length($spaces)) {
        #set indent if not set (first spaces guaranteed to be single indent)
        if ($indentsize == 0) { $indentsize=length($spaces); }
        $depth=length($spaces)/$indentsize;
      } else {
        $depth=0;
      }
      if ($depth > $odepth) {
	if (($depth-$odepth)>1) {
	  print "error in input file -- too much indenting -- no parent\n";
	  print "  $url\n";
	  print "  $depth, $odepth\n";
	  exit(0);
	}
	push(@parlist,$oldkey);
      } elsif ($depth < $odepth) {
	for ($i=0; $i<($odepth-$depth); ++$i) { pop(@parlist); }
      }
      $parent=$parlist[$#parlist];
      $tree{$key,"depth"}=$depth;
      $tree{$key,"kidcount"}=0;
      $tree{$key,"parent"}=$parent;
      $tree{$key,"text"}=$text;
      $tree{$key,"url"}=$url;
      $tree{$key,"othertext"}=$othertext;
      ++$tree{$parent,"kidcount"};
      $odepth=$depth;
      $oldkey=$key;
    }
  }
  
  foreach $key (@keylist) {
    for ($i=0; $i<$tree{$key,"depth"}; ++$i) {
      $currlevel=&getparent($key,$tree{$key,"depth"}-$i);
      #print STDERR "$tree{$key,\"url\"}\n";
      #print STDERR "  for $key, currlevel is $currlevel\n";
      #print STDERR "  i=$i;  depth is $tree{$key,'depth'}\n";
      if ($i == $tree{$key,"depth"}-1) {
	if (--$tree{$currlevel,"kidcount"}) {
	  print "<IMG ALIGN=TEXTTOP SRC=\"$IMGDIR/tree-tee.xbm\">";
	} else {
	  print "<IMG ALIGN=TEXTTOP SRC=\"$IMGDIR/tree-end.xbm\">";
	}
      } else {
	if ($tree{$currlevel,"kidcount"}) {
	  print "<IMG ALIGN=TEXTTOP SRC=\"$IMGDIR/tree-pass.xbm\">";
	} else {
	  print "<IMG ALIGN=TEXTTOP SRC=\"$IMGDIR/tree-blank.xbm\">";
	}
      }
    }
    if ($tree{$key,'url'} eq "*") {
      print $tree{$key,'text'};
    } else {
      print "<A HREF=\"$tree{$key,'url'}\">$tree{$key,'text'}</A>";
    }
    print " ", $tree{$key,'othertext'};
    print "<BR>\n";
  }
}

sub getparent {
  local($start,$num)=($_[0],$_[1]);
  local($i);
  local($parent)=$start;
  for ($i=0; $i<$num; ++$i) {
    $parent=$tree{$parent,"parent"};
  }
  return($parent);
}
