#!/usr/bin/perl


################
# User configuarble paramaters go here
#
# Number of days in your tapecycle (that is, number of days between a 0
# dump).
# in my case, tapecycle is 2 weeks (== 2*7 days)
$tapecycle = 14;

# month starts at 1
@Month = ("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
                        "Aug", "Sep", "Oct", "Nov", "Dec");

use Date::Calc qw( Weeks_in_Year Monday_of_Week
                   Date_to_Text Date_to_Text_Long
                   Add_Delta_Days Today Week_of_Year );
 
($y,$m,$d) = Today();

for($i =0; $i < $tapecycle; $i++){
    ($py,$pm,$pd) = Add_Delta_Days($y,$m,$d,$i-$tapecycle);
    $match[$tapecycle-$i] = sprintf "%4d-%02d-%02d", $py,$pm,$pd;
}

print <<Header;
<body bgcolor=white>
<title> backup status for $d-$Month[$m]-$y </title>
<h1> backup status for $d-$Month[$m]-$y </h1>

<table width=100% border=5>
 <tr>
  <th> disk </th>

Header

for($i=0; $i < $tapecycle; $i++){
    print "<th colspan=4> ", $match[$tapecycle-$i] , "</th>\t\t";
}
print "<tr>\n";


while(<>){
    for($i=0; $i < $tapecycle; $i++){
	($date,$host,$disk,$level,$tape,$image,$status) = split;
	if($match[$tapecycle - $i] eq $date){
	    $found{"$host:$disk"} = 1;
	    $backups{"$host:$disk"}[$tapecycle - $i] = "$level:$tape:$image:$status";
	}
    }
}


foreach ( sort keys %found ) {
    ($host,$disk) = split /:/;
    if($host ne $oldhost){
	print "<tr><td colspan=29> <b><big> $host </big></b></td>\n";
	$oldhost = $host;
    }
    # check to see if we have (at least) 1 level 0 dump
    $havezero = 0;
    for($i=0; $i < $tapecycle; $i++){
	if($backups{$_}[$tapecycle - $i] ){
	    ($level,$tape,$image,$status) = split /:/ , $backups{$_}[$tapecycle - $i] ;
	    if(!$level){
		$havezero = 1;
	    }
	}
    }
    if($havezero){
	print "\n<tr><td>$disk</td>";
    } else {
	print "\n<tr><td><font color=red>$disk</font></td>";
    }
    for($i=0; $i < $tapecycle; $i++){
	if($backups{$_}[$tapecycle - $i] ){
	    ($level,$tape,$image,$status) = split /:/ , $backups{$_}[$tapecycle - $i] ;
            # Limited fancy formatting, level 0 dumps are marked Bold,
	    # Failed dumps are marked RED, If you have no level 0 dumps
	    # within tapecycle days, the whole line is marked red (and
	    # italic)
	    if($level){
		$extra = "";
		$extraend = "";
	    } else {
		$extra = "<b>";
		$extraend = "</b>";
	    }
	    if($status eq "FAILED"){
		$extra = "$extra <font color=red>";
		$extraend = "</font> $extra";
		$image = "---";
	    }
	    if(!$havezero){
		$extra = "$extra <font color=red><i>";
		$extraend = "</i></font> $extra";
	    }else{
		$extra = "$extra <font color=black><i>";
		$extraend = "</i></font> $extra";
	    }
	    print "\n<td> $extra $level $extraend</td>\t" ;
	    print "\n<td> $extra $tape $extraend</td>\t" ;
	    print "\n<td> $extra $image $extraend</td>\t" ;
	    print "\n<td> $extra $status $extraend</td>\t" ;
	}else{
	    print "\n<td colspan=4>--\t</td>";
	}
    }
    print "<br>\n";
}

print "</table>"

#
# (c) 2001 Gerhard den Hollander
