#!/usr/bin/perl -I/usr/lib/bs/bin -I/usr/lib/bs/uxmon
#    Big Sister network monitor
#    Copyright (C) 1998  Thomas Aeby
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

#=============================================================================
#
$main::Usage	  = "[-D level] [-l logdir] [-b bigsisterdirectory]";
#
#=============================================================================

@main::options = ( "l:s", "c" );
use common;
proginit();

use strict;
use Time::Local;
require bscgi;

$main::opt_D = 0;
my $dl = $main'dl = $main::opt_D;

my $logdir = "$main::root/www/logs";
$logdir = $main::opt_l if( $main::opt_l );

$main::on_unix = Platforms::isunix();

if( $main::on_unix ) {
    $ENV{PATH} = '/usr/ucb:/bin:/usr/bin:/sbin:/usr/sbin';
}

bscgi::mainloop( \&one_pass, "white_bg,static_lamps", 300 );


sub one_pass {
    my( $skin, %cgivars ) = @_;
    my $month = 0;
    my $year = 0;
    my $day = 0;
    my $host = "";
    my $item = "";
    my $toptime;
    my $i;

    $month = $cgivars{"MONTH"};
    $year = $cgivars{"YEAR"};
    $day = $cgivars{"DAY"};
    $host = $cgivars{"HOST"};
    $item = $cgivars{"ITEM"};
     
    unless( $month || length($year) || $day ) {
	my( $sec,$min,$hour,$mday,$mon,$wyear,$wday,$yday,$isdst) = localtime( time-7*24*3600 );
	$cgivars{"MONTH"} = $month = $mon+1;
	$cgivars{"YEAR"} = $year = $wyear + 1900;
	$cgivars{"DAY"} = $day = $mday;
    }

    ($month = 1) unless( $month );
    ($year = 1970) unless( length($year) );
    ($day = 1) unless( $day );
    if( $year < 100 ) { 
	$year += 2000;
	$cgivars{"YEAR"} = $year;
    }

    my $time = timelocal(0,0,0,$day,$month-1,$year-1900);

    if( $cgivars{"TIME"} ) {
	$toptime = $cgivars{"TIME"};
    }

    my @files = (["$main::root/var/display.history", time]);
    opendir( DIR, "$main::root/var" );
    my $filename;
    foreach $filename ( readdir( DIR ) ) {
	next unless( $filename =~ /^display.history.\d+/ );
	$filename = "$main::root/var/$filename";
	my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
	    $atime,$mtime,$ctime,$blksize,$blocks)
	    = stat($filename);

	if( (-f $filename) && ($mtime > $time) ) {
	    unshift( @files, [$filename,$mtime] );
	}
    }
    @files = map { $_->[0] } sort { $a->[1] <=> $b->[1] } @files;
    print "Content-Type: text/html\n\n";
    print history( $main::display, $time, $toptime, $host, $item, $skin, \%cgivars, @files );
}



sub history {
    my( $disp, $time, $toptime, $host, $item, $skin, $vars, @files ) = @_;
    my( $read, $text, $i, $maxline );
    my( $lhost, $litem, $lfrom, $lto, $ltime, $ltext, $ldate );
    my( $counter );

    my @text = ();
    my @times = ();
    $maxline = $skin->{"history_maxlines.inc"};
    $i = 0;
    foreach $read (@files) {
	open( IN, "<$read" );
	while( <IN> ) {
	    /^([^\s\t]+)\.([^\:]+): ([a-z]*)->([a-z]*) \(([0-9]+)\)(.*)$/ || next;
	    ( $lhost, $litem, $lfrom, $lto, $ltime, $ltext ) = ( $1, $2, $3, $4, $5, $6 );
	    next if( ($host && ! ($lhost =~ /$host/)) || ($item && ($item ne $litem)) ||
		     ($ltime < $time) );
	    if( $counter ) {
		last unless( --$counter );
	    }
	    elsif( $toptime && ($ltime>$toptime) ) {
		$counter = $maxline-2;
	    }
	    else {
		if( $ltext =~ /^(.*? \d\d\d\d) (.*)$/ ) {
		    ($ldate,$ltext) = ($1,$2);
		}
		else {
		    $ldate = localtime($ltime);
		}
		unshift( @times, $ltime );
		unshift( @text, $disp->replace_vars( $skin, {
		    "HOST" => $lhost,
		    "ITEM" => $litem,
		    "FROM" => $lfrom,
		    "TO" => $lto,
		    "TIME" => $ltime,
		    "DATE" => $ldate,
		    "TEXT" => $ltext 
		    }, $skin->{"history_entry.proto"} ) );
	    }
	}
	close IN;
    }
    splice( @text, $maxline );
    $text = join( "", @text );
    $text =~ s/\|\>/\<BR\>/g;
    $vars->{"TEXT"} = $text;
    $vars->{"UPTIME"} = $ltime;
    if( $#times < $maxline-1 ) {
	$vars->{"DOWNTIME"} = $times[$#times];
    }
    else {
	$vars->{"DOWNTIME"} = $times[$maxline-1];
    }

    foreach $i ( keys %$vars ) {
	$vars->{"ENC_".$i} = $disp->encode( $vars->{$i} );
    }
    return $disp->replace_vars( $skin, $vars, $skin->{"history.proto"} );
}

