commit 7d6cf686f4dad31938fdd32f95f24390e5c23357
Author: Hamish Coleman <hamish@zot.org>
Date:   Fri Feb 20 11:00:07 2009 +1100

    Structure of indexmrtg is now mostly right

diff --git a/indexmrtg.cgi b/indexmrtg.cgi
index cfcc2ab..3bfd384 100755
--- a/indexmrtg.cgi
+++ b/indexmrtg.cgi
@@ -29,12 +29,12 @@ my @config_files = (
 );
 
 # FIXME - globals
-my $gifdone = 0; # Scan for newest graph and save info for later
 my $warnings;
 my $db = {};	# storage for all tests found
 
-$db->{mrtg}{default}{post}{xsize} = 500;
-$db->{mrtg}{default}{post}{ysize} = 135;
+$db->{mrtg}{gifdone} = 0; # Scan for newest graph and save info for later
+$db->{mrtg}{global}{postfix}{xsize} = 500;
+$db->{mrtg}{global}{postfix}{ysize} = 135;
 
 sub config_save_val($$$$) {
 	my ($filename,$name,$var,$val) = @_;
@@ -52,11 +52,11 @@ sub config_save_val($$$$) {
 
 	# Save any default initializers
 	if ($name eq '_') {
-		$db->{mrtg}{default}{post}{$var} = $val;
+		$db->{mrtg}{global}{postfix}{$var} = $val;
 		return;
 	}
 	if ($name eq '^') {
-		$db->{mrtg}{default}{pre}{$var} = $val;
+		$db->{mrtg}{global}{prefix}{$var} = $val;
 		return;
 	}
 
@@ -64,7 +64,7 @@ sub config_save_val($$$$) {
 	my $unique_name = $name;
 	my $unique_id = 1;
 	while (defined $db->{graph}->{$unique_name}
-		 && $db->{graph}->{$unique_name}->{srcfile} ne $filename) {
+		 && $db->{graph}->{$unique_name}->{definedby} ne $filename) {
 		$unique_name = $name . '_' . $unique_id;
 		$unique_id++;
 	}
@@ -77,14 +77,14 @@ sub config_save_val($$$$) {
 		push @{$db->{sequence}}, $unique_name;
 
 		$db->{graph}->{$unique_name}->{name} = $name;
-		$db->{graph}->{$unique_name}->{srcfile} = $filename;
+		$db->{graph}->{$unique_name}->{definedby} = $filename;
 		$db->{graph}->{$unique_name}->{sequence} = @{$db->{sequence}};
 
-		$db->{graph}{$unique_name}{img}{xsize} = $db->{mrtg}{default}{post}{xsize};
-		$db->{graph}{$unique_name}{img}{ysize} = $db->{mrtg}{default}{post}{ysize};
+		$db->{graph}{$unique_name}{img}{xsize} = $db->{mrtg}{global}{postfix}{xsize};
+		$db->{graph}{$unique_name}{img}{ysize} = $db->{mrtg}{global}{postfix}{ysize};
 
 	}
-	$db->{graph}{$unique_name}{mrtg}{$var} = $val;
+	$db->{mrtg}{target}{$unique_name}{$var} = $val;
 }
 
 # Load one or more files
@@ -101,6 +101,7 @@ sub config_load_file(@) {
 		}
 
 		#TODO - multi-line values
+		#TODO - reset the {mrtg}{global} vars
 
 		while(<$fh>){
 
@@ -112,10 +113,17 @@ sub config_load_file(@) {
 
 				config_save_val($filename,$name,$var,$val);
 
+			} elsif ( $_ =~ /^([^#[][^[]+):\s*(.+)$/ ) {
+				# its a global statement
+				my $var=lc $1;
+				my $val=$2;
+
+				$db->{mrtg}{global}{$var}=$val;
 			}
+
 			# TODO - Handle extra statements:
 			#	Include
-			# 	WorkDir
+			# 	WorkDir - see config_read_filesystem
 			#	LogDir
 			#	##HC Magic for baseurl
 		}
@@ -126,21 +134,28 @@ sub config_load_file(@) {
 # any other filesystem details for them.
 #
 sub config_read_filesystem() {
+
+	# TODO - also check the other directory statements
+	if ($db->{mrtg}{global}{workdir}) {
+		chdir $db->{mrtg}{global}{workdir};
+	}
+
 	# check and update details for all known tests;
 	for my $i (keys %{$db->{graph}}) {
-		my $name = $db->{graph}->{$i};
-		my $testname = $name->{name};
-
-		# TODO - check workdir and validate it here
+		my $graph = $db->{graph}->{$i};
+		my $graphname = $graph->{name};
+		my $mrtg = $db->{mrtg}{target}{$i};
 
 		# Default is GIF, unless a PNG file exists
-		$name->{mrtg}{_imagetype} = 'gif';
-		if ( -e "$testname-day.png" ) {
-			$name->{mrtg}{_imagetype} = "png";
+		$mrtg->{_imagetype} = 'gif';
+		if ( -e "$graphname-day.png" ) {
+			$mrtg->{_imagetype} = "png";
 		}
 
+		# TODO - determine correct URL prefix
 		# TODO - change the filename based on the displayed period
-		my $filename = $testname . "-day." . $name->{mrtg}{_imagetype};
+		my $filename = $graphname . "-day." . $mrtg->{_imagetype};
+		$graph->{img}{URL} = $filename;
 		
 		my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
 			$atime,$mtime,$ctime,$blksize,$blocks)
@@ -150,12 +165,15 @@ sub config_read_filesystem() {
 			next;
 		}
 
-		$name->{_mtime} = $mtime;
+		# TODO - move these to a better function
+		$graph->{URL} = $graphname . '.html';
+		$graph->{title} = $mrtg->{title};
+
+		$graph->{img}{mtime} = $mtime;
 
-		# FIXME - global
 		# find the newest file
-		if ($mtime > $gifdone) {
-			$gifdone = $mtime;
+		if ($mtime > $db->{mrtg}{gifdone}) {
+			$db->{mrtg}{gifdone} = $mtime;
 		}
 	}
 }
@@ -246,13 +264,13 @@ sub maketree_grouphosttest($$) {
 	}
 }
 
-sub maketree_srcfile($$) {
+sub maketree_definedby($$) {
 	my ($name,$sequence) = @_;
 
 	# config files
-	my $srcfile = $db->{graph}->{$name}->{srcfile};
+	my $definedby = $db->{graph}->{$name}->{definedby};
 
-	my @srcpath = split /\//,$srcfile;
+	my @srcpath = split /\//,$definedby;
 
 	shift @srcpath;	# remove initial slash
 
@@ -278,7 +296,7 @@ sub maketree() {
 
 	for my $name (keys %{$db->{graph}}) {
 		my $sequence = $db->{graph}->{$name}->{sequence};
-		maketree_srcfile($name,$sequence);
+		maketree_definedby($name,$sequence);
 		maketree_grouphosttest($name,$sequence);
 		# TODO - parse the Target setting and create trees
 		# TODO - parse magic comments and create trees here
@@ -296,11 +314,13 @@ print header,
 
 config_load_file(@config_files);
 config_read_filesystem();
+maketree();
+
 
 # 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
+my $refresh = $interval + $guardband + $db->{mrtg}{gifdone} - time; # predict how long until next update
 $refresh = $interval if $refresh <= $guardband;
 my $expires = gmtime (time + $interval * 2 + $guardband);
 
