#!/usr/bin/perl

use vars qw/$home_dir/;

BEGIN {
  $home_dir = "/home/grapher2";
  open(FL,"/etc/hgs2.conf");
  ($t1,$t2,undef)=split(/:/,<FL>);
  close(FL);
  if ( $t1 ne ""){ $home_dir=$t1;}
};




print "Content-type: text/html\n\n";




$dir="$home_dir/data";
$grep="grep";


 ($dmn,undef)=@ARGV;
  if ( $dmn eq "" ){ $dmn="ALL";}
  if ( $dmn ne "ALL" ){
    $search="DOMAIN=$dmn";
  }else{
    $search="DOMAIN";
  }
 (@temp)=`$grep $search $dir/*/info2`;
 if ( $#temp > 0 ) {
   foreach $t (@temp){
     (undef,$t)=split(/\/data\//,$t);chomp($t);
     ($t,undef)=split(/\//,$t);chomp($t);
     push @machines,$t;
   }
 }elsif ( ( $#temp == 0 ) and ( $temp[0]=~ /\/data\// )){
   (undef,$t)=split(/\/data\//,$temp[0]);chomp($t);
   ($t,undef)=split(/\//,$t);chomp($t);
   push @machines,$t;
 }elsif ( $#temp == 0 ) {
  # this is done this way because other systems may not support some grep flags  
    opendir(DIR, $dir);
    @dots = grep { ! /^\./  } readdir(DIR);
    closedir DIR;
    foreach $d (@dots){
      (@temp2)=`$grep $search $dir/$d/info2`;
      if ( $#temp2 > -1 ){ push @machines,$d;}
    }
 }

 (@temp)=`$grep DOMAIN $dir/*/info2`;
 foreach $t (@temp){
   (undef,$t)=split(/=/,$t);chomp($t);
   $domains{$t}=1;
 }
   $domains{"ALL"}=1;
  
print '
<body bgcolor=#FFE6C5>
<a href="cover?',$dmn,'" target="right"><b>Host Grapher</b></a><p>
<table border=0>
<tr><td>
<form>
<select id=selectDOMAIN name=selectDOMAIN size=1 style="WIDTH:120px; font-family: vt100, Courier New; 
font-size=xx-small;" onchange="reload_menu();" >';
foreach $domain (sort keys %domains) {
  if ( $domain eq $dmn ){
    print "<option value=\"$domain\" selected>\u$domain</option>\n";
  }else{
    print "<option value=\"$domain\">\u$domain</option>\n";
  }
}

print '</select>
<select id=selectHOST name=selectHOST size=10 style="WIDTH:120px; font-family: vt100, Courier New; 
font-size=xx-small;" onchange="draw_graph();" >';

foreach $machine (sort @machines) {print "<option value=\"$machine\">\u$machine</option>\n";}

print '</select>
</td></tr>
<tr>
<td colspan=2><font color=gray face="Trebuchet MS, Times New Roman, Times, serif" size="3">
Preferences</font>
</td>
</tr>
<tr><td>
<font face="Trebuchet MS, Times New Roman, Times, serif" size="1">
<input type=radio name=radioTIME value=h onclick="draw_graph();">Hourly<br>
<input type=radio name=radioTIME value=d onclick="draw_graph();"checked>Daily<br>
<input type=radio name=radioTIME value=w onclick="draw_graph();">Weekly<br>
<input type=radio name=radioTIME value=m onclick="draw_graph();">Monthly<br>
<input type=radio name=radioTIME value=y onclick="draw_graph();">Yearly<br>
</font>
</form>
</td></tr>
</table>
<script LANGUAGE=javascript>

	function draw_graph() {
		if (document.forms[0].selectHOST.selectedIndex > -1 ) 
                {
			host = document.forms[0].selectHOST.options[document.forms[0].selectHOST.selectedIndex].text;
			for(i=0;i<document.forms[0].radioTIME.length;i++) 
                          if(document.forms[0].radioTIME[i].checked) 
                             time=document.forms[0].radioTIME[i].value;
			url = "grapher" + "?" + host + "." + time + ".";
			parent.frames[1].location.href=url;
		}
	}

	function reload_menu() {
		if (document.forms[0].selectDOMAIN.selectedIndex > -1 ) 
                {
			host = document.forms[0].selectDOMAIN.options[document.forms[0].selectDOMAIN.selectedIndex].text;
			url = "menu" + "?" + host ;
			parent.frames[0].location.href=url;
		}
	}

</script>
';

