commit 1b1c276ce1f4e67c6084e58bff7a691656269bf0
Author: Hamish Coleman <hamish@zot.org>
Date:   Sun Dec 16 11:33:06 2007 +1100

    Yank new code above old page logic, sort by sequence, simplify shown tree, remove some debug

diff --git a/mrtg.cgi b/mrtg.cgi
index 99a649c..b1e3bf2 100755
--- a/mrtg.cgi
+++ b/mrtg.cgi
@@ -191,14 +191,14 @@ sub maketree_grouphosttest($) {
 
 	if (defined $hosttype) {
 		$db->{tree}->{group}->{$group}->{host}->{$hosttype}->{$host}->{$test}->{$name} = $sequence;
-		$db->{tree}->{group}->{$group}->{host}->{$hosttype}->{test}->{$test}->{$host}->{$name} = $sequence;
-		$db->{tree}->{host}->{$hosttype}->{$host}->{$test}->{$name} = $sequence;
-		$db->{tree}->{test}->{$test}->{$hosttype}->{$host}->{$name} = $sequence;
+		$db->{tree}->{group}->{$group}->{host}->{$hosttype}->{test}->{$test}->{$name} = $sequence;
+		$db->{tree}->{group}->{ALL}->{host}->{$hosttype}->{$host}->{$test}->{$name} = $sequence;
+		$db->{tree}->{group}->{ALL}->{test}->{$test}->{$hosttype}->{$host}->{$name} = $sequence;
 		$db->{tree}->{group}->{$group}->{test}->{$test}->{$hosttype}->{$host}->{$name} = $sequence;
 	} else {
 		$db->{tree}->{group}->{$group}->{host}->{$host}->{$test}->{$name} = $sequence;
-		$db->{tree}->{host}->{$host}->{$test}->{$name} = $sequence;
-		$db->{tree}->{test}->{$test}->{$host}->{$name} = $sequence;
+		$db->{tree}->{group}->{ALL}->{host}->{$host}->{$test}->{$name} = $sequence;
+		$db->{tree}->{group}->{ALL}->{test}->{$test}->{$host}->{$name} = $sequence;
 		$db->{tree}->{group}->{$group}->{test}->{$test}->{$host}->{$name} = $sequence;
 	}
 
@@ -230,12 +230,12 @@ sub emit_tree_one($$$) {
 	for my $i (sort keys %{$dir}) {
 		my $val = $dir->{$i};
 		if (!ref($val)) {
-			#print "insDoc($parentnode, gLnk(\"S\", \"$i\", \"?page=x&path=$path:$i\"))\n";
+			#print "insDoc($parentnode, gLnk(\"S\", \"$i\", \"?path=$path:$i\"))\n";
 			next;
 		}
 
 		my $node = 'n'.$node_next++;
-		print "$node = insFld($parentnode, gFld(\"$i\", \"?page=x&path=$path:$i\"))\n";
+		print "$node = insFld($parentnode, gFld(\"$i\", \"?path=$path:$i\"))\n";
 		emit_tree_one("$node","$path:$i",$val);
 	}
 }
@@ -304,7 +304,7 @@ sub emit_path_contents($$) {
 		for my $i (keys %{$node[0]}) {
 			my $val = $node[0]->{$i};
 			if (!ref($val)) {
-				$targets{$i}++;
+				$targets{$i}=$val;	# set sequence number
 			} else {
 				push @node,$val;
 			}
@@ -313,7 +313,7 @@ sub emit_path_contents($$) {
 	}
 
 	# TODO - sort by sequence numbers
-	for my $i (keys %targets) {
+	for my $i (sort {$targets{$a} <=> $targets{$b}} keys %targets) {
 		my $target = $db->{target}->{$i};
 
 		# FIXME - what if mtime is undef?
@@ -337,10 +337,6 @@ sub emit_path_contents($$) {
 		);
 		print br,br,"\n";
 	}
-
-	print "<pre>\n";
-	print Dumper(\%targets);
-	print "</pre>\n";
 }
 
 sub init_tests(@) {
@@ -681,91 +677,8 @@ sub print_menutypelist() {
 	print a({-href=>"?page=treeview",-target=>"_top"}, "Tree (Frames)");
 }
 
-my $page = param('page') || 'treenf';
 my $this_host = hostname;
-my @tests;
-my $lists;
-
-if($page eq 'treeview') {
-	print "Content-type: text/html\n\n";
-	print "<html><head><title>$this_host MRTG Index (V$VERSION)</title></head>\n";
-	print "<script>\n";
-	print "function op() { }\n";
-	print "</script>\n";
-	print '<frameset cols="200,*">'; # FIXME onresize if navigator.family==nn4
-	print '<frame src="?page=treemenu" name=treeframe>';
-	print '<frame src="javascript:parent.op()" name=basefrm>';
-	print "</frameset>\n";
-	print "</html>\n";
-
-} elsif($page eq 'list') {
-	print header,
-		start_html(-TITLE=>"$this_host MRTG Index (V$VERSION)", -BGCOLOR=>'#e6e6e6'),
-		"\n";
-
-	@tests = init_tests(@config_files);
-	$lists = init_lists(@tests);
-
-	# Time the next update to occur a little while after the next interval completes
-	my $interval = 300; # 5 min update interval
-	my $guardband = 15; # updates occur this many seconds after predicted gif completion
-	my $refresh = $interval + $guardband + $gifdone - time; # predict how long until next update
-	$refresh = $interval if $refresh <= $guardband;
-	my $expires = gmtime (time + $interval * 2 + $guardband);
-
-	my $list = param('list');
-	my $graph = param('graph') || 'day';
-
-	print
-		"<meta http-equiv=\"expires\" content=\"$expires GMT\">\n",
-		"<meta http-equiv=\"refresh\" content=$refresh>\n";
-	if ($list) {
-		print_graph_chooser($graph,$list,$page,'none');
-		print "<br/>";
-		print_list($lists,$graph,$list,'basefrm');
-	}
-	print end_html;
-} elsif($page eq 'treemenu') {
-	print header,
-		start_html(-TITLE=>"$this_host MRTG Index (V$VERSION)", -BGCOLOR=>'#e6e6e6'),
-		"\n";
-
-	@tests = init_tests(@config_files);
-	$lists = init_lists(@tests);
-
-	print <<EOF;
-  <STYLE>
-    BODY {
-      background-color: white;}
-    TD {
-      font-size: 10pt; 
-      font-family: verdana,helvetica; 
-      text-decoration: none;
-      white-space:nowrap;}
-    A {
-      text-decoration: none;
-      color: black;}
-    .specialClass {
-      font-family:garamond; 
-      font-size:12pt;
-      color:green;
-      font-weight:bold;
-      text-decoration:underline}
-  </STYLE>
-EOF
-	print '<script src="ua.js"></script>';
-	print '<script src="ftiens4.js"></script>';
-	print '<script>';
-	print_treemenu('frame',$lists);
-	print '</script>';
-	print '<DIV style="position:absolute; top:0; left:0;"><TABLE border=0><TR><TD><FONT size=-2><A style="font-size:7pt;text-decoration:none;color:silver" href="http://www.treemenu.net/" target=_blank>Javascript Tree Menu</A></FONT></TD></TR></TABLE></DIV>';
-	print '<script>initializeDocument()</script>';
-	print '<NOSCRIPT>A tree for site navigation will open here if you enable JavaScript in your browser.</NOSCRIPT>';
-	print hr;
-	print_menutypelist();
-
-	print end_html;
-} elsif($page eq 'treenf') {
+if(defined param('path')) {
 	print header,
 		start_html(-TITLE=>"$this_host MRTG Index (V$VERSION)",
 			-bgcolor=>"white",
@@ -774,8 +687,9 @@ EOF
 			-onResize=>"if (navigator.family == 'nn4') window.location.reload()" ),
 		"\n";
 
-	@tests = init_tests(@config_files);
-	$lists = init_lists(@tests);
+	config_load_file(@config_files);
+	config_read_filesystem();
+	maketree();
 
 	print <<EOF;
   <STYLE>
@@ -803,7 +717,7 @@ EOF
 	print '<script src="ua.js"></script>';
 	print '<script src="ftiens4.js"></script>';
 	print '<script>';
-	print_treemenu('treenf',$lists);
+	emit_tree('treenf');
 	print '</script>';
 	print <<EOF;
  <TABLE cellpadding="0" cellspacing="0" border="0" width="772">
@@ -826,11 +740,6 @@ EOF
    A tree for site navigation will open here if you enable JavaScript in your browser.
   </NOSCRIPT>
  </SPAN>
-EOF
-
-	print hr;
-	print_menutypelist();
-	print <<EOF;
           </TD>
          </TR>
         </TABLE>
@@ -854,16 +763,16 @@ EOF
 	$refresh = $interval if $refresh <= $guardband;
 	my $expires = gmtime (time + $interval * 2 + $guardband);
 
-	my $list = param('list');
+	my $path = param('path');
 	my $graph = param('graph') || 'day';
 
 	print
 		"<meta http-equiv=\"expires\" content=\"$expires GMT\">\n",
 		"<meta http-equiv=\"refresh\" content=$refresh>\n";
-	if ($list) {
-		print_graph_chooser($graph,$list,$page,'none');
+	if ($path) {
+		print_graph_chooser($graph,'FIXME','FIXME','none');
 		print "<br/>";
-		print_list($lists,$graph,$list,'_self');
+		emit_path_contents($path,$graph);
 	}
 
 	print <<EOF;
@@ -877,7 +786,92 @@ EOF
 EOF
 
 	print end_html;
-} elsif($page eq 'x') {
+} else {
+
+my $page = param('page') || 'treenf';
+my @tests;
+my $lists;
+
+if($page eq 'treeview') {
+	print "Content-type: text/html\n\n";
+	print "<html><head><title>$this_host MRTG Index (V$VERSION)</title></head>\n";
+	print "<script>\n";
+	print "function op() { }\n";
+	print "</script>\n";
+	print '<frameset cols="200,*">'; # FIXME onresize if navigator.family==nn4
+	print '<frame src="?page=treemenu" name=treeframe>';
+	print '<frame src="javascript:parent.op()" name=basefrm>';
+	print "</frameset>\n";
+	print "</html>\n";
+
+} elsif($page eq 'list') {
+	print header,
+		start_html(-TITLE=>"$this_host MRTG Index (V$VERSION)", -BGCOLOR=>'#e6e6e6'),
+		"\n";
+
+	@tests = init_tests(@config_files);
+	$lists = init_lists(@tests);
+
+	# Time the next update to occur a little while after the next interval completes
+	my $interval = 300; # 5 min update interval
+	my $guardband = 15; # updates occur this many seconds after predicted gif completion
+	my $refresh = $interval + $guardband + $gifdone - time; # predict how long until next update
+	$refresh = $interval if $refresh <= $guardband;
+	my $expires = gmtime (time + $interval * 2 + $guardband);
+
+	my $list = param('list');
+	my $graph = param('graph') || 'day';
+
+	print
+		"<meta http-equiv=\"expires\" content=\"$expires GMT\">\n",
+		"<meta http-equiv=\"refresh\" content=$refresh>\n";
+	if ($list) {
+		print_graph_chooser($graph,$list,$page,'none');
+		print "<br/>";
+		print_list($lists,$graph,$list,'basefrm');
+	}
+	print end_html;
+} elsif($page eq 'treemenu') {
+	print header,
+		start_html(-TITLE=>"$this_host MRTG Index (V$VERSION)", -BGCOLOR=>'#e6e6e6'),
+		"\n";
+
+	@tests = init_tests(@config_files);
+	$lists = init_lists(@tests);
+
+	print <<EOF;
+  <STYLE>
+    BODY {
+      background-color: white;}
+    TD {
+      font-size: 10pt; 
+      font-family: verdana,helvetica; 
+      text-decoration: none;
+      white-space:nowrap;}
+    A {
+      text-decoration: none;
+      color: black;}
+    .specialClass {
+      font-family:garamond; 
+      font-size:12pt;
+      color:green;
+      font-weight:bold;
+      text-decoration:underline}
+  </STYLE>
+EOF
+	print '<script src="ua.js"></script>';
+	print '<script src="ftiens4.js"></script>';
+	print '<script>';
+	print_treemenu('frame',$lists);
+	print '</script>';
+	print '<DIV style="position:absolute; top:0; left:0;"><TABLE border=0><TR><TD><FONT size=-2><A style="font-size:7pt;text-decoration:none;color:silver" href="http://www.treemenu.net/" target=_blank>Javascript Tree Menu</A></FONT></TD></TR></TABLE></DIV>';
+	print '<script>initializeDocument()</script>';
+	print '<NOSCRIPT>A tree for site navigation will open here if you enable JavaScript in your browser.</NOSCRIPT>';
+	print hr;
+	print_menutypelist();
+
+	print end_html;
+} elsif($page eq 'treenf') {
 	print header,
 		start_html(-TITLE=>"$this_host MRTG Index (V$VERSION)",
 			-bgcolor=>"white",
@@ -886,9 +880,8 @@ EOF
 			-onResize=>"if (navigator.family == 'nn4') window.location.reload()" ),
 		"\n";
 
-	config_load_file(@config_files);
-	config_read_filesystem();
-	maketree();
+	@tests = init_tests(@config_files);
+	$lists = init_lists(@tests);
 
 	print <<EOF;
   <STYLE>
@@ -916,7 +909,7 @@ EOF
 	print '<script src="ua.js"></script>';
 	print '<script src="ftiens4.js"></script>';
 	print '<script>';
-	emit_tree('treenf');
+	print_treemenu('treenf',$lists);
 	print '</script>';
 	print <<EOF;
  <TABLE cellpadding="0" cellspacing="0" border="0" width="772">
@@ -939,6 +932,11 @@ EOF
    A tree for site navigation will open here if you enable JavaScript in your browser.
   </NOSCRIPT>
  </SPAN>
+EOF
+
+	print hr;
+	print_menutypelist();
+	print <<EOF;
           </TD>
          </TR>
         </TABLE>
@@ -962,16 +960,16 @@ EOF
 	$refresh = $interval if $refresh <= $guardband;
 	my $expires = gmtime (time + $interval * 2 + $guardband);
 
-	my $path = param('path');
+	my $list = param('list');
 	my $graph = param('graph') || 'day';
 
 	print
 		"<meta http-equiv=\"expires\" content=\"$expires GMT\">\n",
 		"<meta http-equiv=\"refresh\" content=$refresh>\n";
-	if ($path) {
-		print_graph_chooser($graph,'FIXME',$page,'none');
+	if ($list) {
+		print_graph_chooser($graph,$list,$page,'none');
 		print "<br/>";
-		emit_path_contents($path,$graph);
+		print_list($lists,$graph,$list,'_self');
 	}
 
 	print <<EOF;
@@ -1042,12 +1040,13 @@ EOF
 
 }
 
+}
+
 if ($warnings && param('debug')) {
 	print "<pre>\n", "$warnings", "</pre>\n";
 }
 
-print "<pre>\n";
-print Dumper(\$db),"\n";
-#emit_tree('treenf');
-print "</pre>";
+#print "<pre>\n";
+#print Dumper(\$db),"\n";
+#print "</pre>";
 
