#!/usr/bin/perl

$IMGDIR="images/web";

&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

  print "<SCRIPT LANGUAGE=\"JavaScript\">\n";
  print "<!-- // Begin javascript hiding\n";
  print "firstTree=new Tree({id:\"first\", items:\"[ \\\n";

  $indentsize=0;  #autodetect
  $odepth=0;
  while(<>) {
    next if (/^\s*#/);   #skip comment lines
    if (/^INDENT=/) {
      chop;
      s/INDENT=//;
      $indentsize=$_;
    } else {
      chop;
      ++$key;
      ($url,$text)=split(' ',$_,2);
      $text =~ s/'/&#39;/g;
      $text =~ s/"/\\"/g;
      ($text,$othertext)=split(/\|/,$text,2);

      $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) {
	print "\\\n";
	for ($i=$odepth; $i>$depth; --$i) {
	  print " " x (($i-1)*2);
	  print "], \\\n";
	}
      } elsif ($depth > $odepth) {
	if (($depth-$odepth)>1) {
	  print "error in input file -- too much indenting -- no parent\n";
	  exit(0);
	}
	print "[ \\\n";
      } else {
	print "\\\n";
      }
      print $spaces;
      if ($url eq "*") {
	print "'$text $othertext', ";
      } else {
	print "'<A HREF=\\\"$url\\\">$text</A> $othertext', ";
      }

      $odepth=$depth;
    }
  }
  print "\\\n";
  for ($i=$odepth; $i>0; --$i) {
    print " " x (($i-1)*2);
    print "], \\\n";
  }
  print "] \"});\n";
  print "// end javascript hiding -->\n";
  print "</SCRIPT>\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);
}
