#!/usr/bin/perl -w
#
# Generate hyperlinked OmniBack reports
#
# 2003-05-09 Dobrica Pavlinusic <dpavlin@pliva.hr>
#

use strict;
use HTML::TreeBuilder;

# location of omnirpt binary
my $omnirpt="/usr/omni/bin/omnirpt";
# output html pages to...
my $dir="/data/mon/omni/";
# master report
#my $master="index.html";
my $master="omni.html";
# how many hours to include in master report?
my $hr = 36;

my $html;
my %errors;

open(OR, "$omnirpt -report list_sessions -timeframe $hr $hr -html |") || die "list_sessions: $!";
while(<OR>) {
	$html .= $_;
}
close(OR);

my $tree = HTML::TreeBuilder->new;
$tree->parse($html);
foreach my $tr ($tree->look_down('_tag', 'tr')) {

	my $el = scalar $tr->content_list;
	if ($el > 10) {
		my @line;
		foreach ($tr->look_down('_tag','td')) {
			push @line,$_->as_text;
		}
		if ($line[1] &&
			$line[1] ne "Completed" &&
			$line[1] ne "In Progress" &&
			$line[1] ne "Queuing"
			) {
#print "## $line[1]\n";
			my $backup_spec = $line[0];
			my $filename = lc($backup_spec);
			$filename =~ s#[^a-z0-9]+#_#g;
			if (! $errors{$backup_spec}) {
				dump_rpt("-report backup_errors -timeframe $hr $hr -datalist $backup_spec",$filename);
				$html =~ s#>$backup_spec<#><a href="$filename.html">$backup_spec</a><#gms;
				$errors{$backup_spec}++;
			}
		}

		my $session = $line[$#line];
		if ($session) {
			my $filename = $session;
			$filename =~ s#/#-#g;
			dump_rpt("-report session_objects -session $session",$filename);
			$html =~ s#>$session<#><a href="$filename.html">$session</a><#gms;
		}
	}
}

$tree->delete;

# dump master report
open(WR, "> $dir/index.html") || die "can't create $dir/index.html: $!";
	print WR $html;
close(WR);

# -report session_flow -timeframe 24 24 -html
#
#


sub dump_rpt {
	my $arg = shift @_;
	my $filename = shift @_;
	$filename .= ".html";

#print "$filename\n";

	# create session report
	open(WSO, "> $dir/$filename") || die "can't create $dir/$filename: $!";
	open(SO, "$omnirpt $arg -html |") || die "$arg: $!";
	while(<SO>) {
		print WSO $_;
	}
	close(SO);
	close(WSO);
}

