sub Generate {
    my($sip, $ip);
    my(@iplist) = @_;

# Create Fail, Warn, Ok hashes for requested ip numbers

    foreach $ip (@iplist) {
      $sip = &sip($ip);
      $Site{$mySite{$sip}{'status'}}{$sip}{'ip'}       = $mySite{$sip}{'ip'};
      $Site{$mySite{$sip}{'status'}}{$sip}{'name'}     = $mySite{$sip}{'name'};
      $Site{$mySite{$sip}{'status'}}{$sip}{'alias'}    = $mySite{$sip}{'Alias_8'};
      $Site{$mySite{$sip}{'status'}}{$sip}{'ports'}    = $mySite{$sip}{'ports'};
      $Site{$mySite{$sip}{'status'}}{$sip}{'portlist'} = $mySite{$sip}{'portlist'};
      if ($mySite{$sip}{'os'}) {
        $Site{$mySite{$sip}{'status'}}{$sip}{'os'}    = $mySite{$sip}{'os'};
        $Site{$mySite{$sip}{'status'}}{$sip}{'icon'}  = $mySite{$sip}{'icon'};
      } else {
        $in{'os'}   = &GetOS($ip);
        $in{'icon'} = &GetICON($in{'os'});
        &SaveStoredInfo($ip);
        $Site{$mySite{$sip}{'status'}}{$sip}{'os'}    = $in{'os'};
        $Site{$mySite{$sip}{'status'}}{$sip}{'icon'}  = $in{'icon'};
      }
    }

    return;
}

sub WriteHTMLfiles {

# Create the compact and basic HTML strings and write to files

    local ($o, $found, $cc, $cb, $sip, $c, $b);

    local $compact = "<table border=0 cellspacing=0 cellpadding=0 width=\"100%\">".
                     "<tr><td><span style=\"".$Fxs.
                     "\">Click on a site address for detailed information.</span></td>".
                     "<td align=\"right\"><span style=\"".$Fxs."\">Created: ".scalar(localtime()).
                     "</span></td></tr></table>\n";
    local $basic   = $compact."<table border=0 cellspacing=2 cellpadding=0>";
    $compact      .= "<table border=0 cellspacing=2 cellpadding=0>";

    foreach $o ($Stat_fail, $Stat_warn, $Stat_ok) {
      $found = $cc = $cb = 0;

      foreach $sip (sort keys %{$Site{$o}}) {
        unless ($found) {
          $compact .= "<tr><td colspan=".$Basiccols.
                      "><div style=\"font-weight:bold;color:black;background-color:white\">".
                     (($o == $Stat_fail) ? "Sites not responding" : ($o == $Stat_warn) ? "Sites responding with errors" : "Fully Operational Sites").
                      "</div></td></tr>";

          $basic   .= "<tr><td colspan=".$Basiccols.
                      "><div style=\"font-weight:bold;color:black;background-color:white\">".
                     (($o == $Stat_fail) ? "Sites not responding" : ($o == $Stat_warn) ? "Sites responding with errors" : "Fully Operational Sites").
                      "</div></td></tr>";
        }
        ($c, $b) = &Display2($o, $sip, $cc++, $cb++);

        $compact .= $c;
        $basic   .= $b;

        $cc = 0 if $cc >= $Basiccols;
        $cb = 0 if $cb >= $Basiccols;

        $found = 1;
      }
      $compact .= "</tr>\n" if $cc;
      $basic   .= "</tr>\n" if $cb;
    }
    $compact .= "</table>\n";
    $basic   .= "</table>\n";

    open(F, ">$Compactresultfile") || &Error("Unable to open result file: $Compactresultfile\n$!");
    print F &Header($Myname,"compact").$compact.&Footer();
    close(F);

    open(F, ">$Basicresultfile") || &Error("Unable to open result file: $Basicresultfile\n$!");
    print F &Header($Myname,"basic").$basic.&Footer();
    close(F);
}


sub Display2 {
    local($o, $sip, $cc, $cb) = @_;
    local($compact, $basic);

    local($color)     = ($o == $Stat_fail) ? $Color_fail : ($o == $Stat_warn) ? $Color_warn : $Color_ok;
    local($textcolor) = ($o == $Stat_fail) ? $Text_fail  : ($o == $Stat_warn) ? $Text_warn  : $Text_ok;

    local($name)      = $Site{$o}{$sip}{'name'};
    local($ip)        = $Site{$o}{$sip}{'ip'};
    local($icon)      = $Site{$o}{$sip}{'icon'};
    local($alias)     = $Site{$o}{$sip}{'alias'};
      
    $compact = "<tr valign=\"middle\" align=\"center\">\n" unless $cc;
    $basic   = "<tr valign=\"middle\" align=\"center\">\n" unless $cb;

    $compact .= "<td style=\"background-color:".$color."\"><a title=\"".$name."(".($ip eq $name?"":$ip).
                ")\" style=\"text-decoration:none;".$Fxs.";color:".
                $textcolor."\" href=\"".$nPULSEurl."?a=detail&site=".$ip."&returnurl=".
                $Compactdisplay_url."\">".
                ($UseAliases ? &Cname($ip,$name,$alias,$CustomName) : $ip).
                "</a></td>";

    $basic   .= "<td style=\"".$Fss.";border:1 solid black\"><a style=\"".$Fxs.";color:".$textcolor.
                ";text-decoration:none;\" href=\"".
                $nPULSEurl."?a=detail&site=".$ip."&returnurl=".$Basicdisplay_url."\">".
                "<img border=0 width=32 height=32 src=\"/images/".$icon.".gif\"><br>".
                "<span style=\"background-color:".$color."\">".
                ($UseAliases ? &Cname($ip,$name,$alias,$CustomName) : $ip).
                "</span></a></td>\n";

    $compact .= "</tr>\n" if $cc > $Basiccols;
    $basic   .= "</tr>\n" if $cb > $Basiccols;

	return $compact, $basic;
}

sub Cname {
    my($ip,$name,$alias,$type) = @_;
    my(@parts, $i);
    return $ip    unless $name;
    return $name  if (($type <= -1) or ($type > 9));
    return $alias if ($alias and ($type == 0));

    @parts = split(/\./, $name);

    if ($type == 9) {
      pop   @parts unless $#parts <= 0;
      shift @parts unless $#parts <= 0;
    } else {
      $name = $alias || $name;
      for ($i = 1; $i <= $type; $i++) { pop @parts unless $#parts <= 0; }
    }
    return join('.', @parts);
}

sub DisplayHTML {
    local($f) = shift || return;
    local(*F, $output);
    local($/); undef $/;
    open(F, "<$f") || return;
    $output = <F>;
    close(F);
    return $output;
}

1;
