#!/usr/local/bin/perl
#
#	Repair allows to print the backup informations for a specified machine.
#
#	Usage: Repair -f <log file> -m <machine name> -p
#
#  	ISSCO (c) 1996, gilbert ROBERT, Gilbert.Robert\@issco.unige.ch

sub Usage {
	print "Usage: Repair -f <log file> -m <machine name> -p \n";
	print "\t . <machine name> can be a regular expression \n";
	print "\t . '-p' option allows to print all backup entries\n";
	print " ISSCO (c) 1996, gilbert ROBERT, Gilbert.Robert\@issco.unige.ch\n\n";
	exit 0;
}

$FileName = "/usr/local/pck/backup/LOG96/backup.log";
$line=0;
$indent_index=-1;
$^ = "top";
$~ = "OUT";
$| = 1;
$old_index = -1;
$Error     = 0;
$Write     = 0;
$PrintAll  = 2;

while ($ARGV[0] =~ /^-/ ) {
   $_ = shift;
   if (/^-f/) {
        $FileName = shift; 
   }
   if (/^-p/) {
        $PrintAll = 1;
   }
   if (/^-m/) {
	$Mach = shift;	
   }
   if (/^-h/) {
        &Usage;
   }
}

open(FILEI,$FileName)|| die "Command aborted ...Can't open file $FileName";

while(<FILEI>) {
	$line++;
	chop;
	$Sent = $_;	
	$Sent =~ s/[\t ]+/\t/g;
	if ( /([0-9]*) Mb/ ) {
		$size = $1;		
		$Total += $size;
		$size .= "MB";
		if ($Write == 1) { write;	}
	}
	if (/aborted/) {
		$ErrList[$Error++] = "BACKUP ABORTED/".$line."/".$init_index."/".$indent_index."/".$machine."/".$date;
	}
	if (/^[0-9]/) {
		($init_index,$machine,$device,$fs,$date) = split(/\t/,$Sent);
		$indent_index++;
		if ($init_index == $old_index) {
		    $ErrList[$Error++] = "INDEX ERROR/".$line."/".$init_index."/".$indent_index."/".$machine."/".$date;
		}
		if ($machine=~ /$Mach/ || $PrintAll == 1) {
			$Write = 1;
		}
		else {
			$Write = 0;
		}
		$old_index = $init_index;
	}
}

close(FILEI);

print "\t ------------------------------------------------------------------------------\n";
print "\t Backup in brief: $indent_index indexes,\t $Total Mb backuped\n";
print "\t ------------------------------------------------------------------------------\n\n";

$^= "errortop";
$~= "EOUT";
$-= 0;
if ($Error >= 0) {
	print "\n\tWARNING Some index problems or backup aborted have been detected\n\n";
	for ($i=1;$i<=$#ErrList;$i++) {
		($errorType,$line,$init_index,$indent_index,$machine,$date) = split('/',$ErrList[$i]);
		write;
	}
	print "\n";
}


format top =
================================================================================================
initial|new   | Machine           Device           FileSystem           Date                Size
index  |index | name      
------------------------------------------------------------------------------------------------
.
 
format OUT =
@||||| @||||| @||||||||||| @<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<  @<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<
$init_index, $indent_index, $machine, $device, $fs, $date, $size
.

format errortop =
================================================================================================
Error Type     | Line    |  Initial Index   | New Index   |   Machine   |     Date 
------------------------------------------------------------------------------------------------
.
 
format EOUT =
@<<<<<<<<<<<<< @||||||||  @|||||||||||||||| @|||||||||||| @||||||||||||| @|||||||||||||||||||||
$errorType, $line, $init_index, $indent_index, $machine, $date
.
