#!/usr/bin/perl

# SalvageLog.prune <filename>

# Written by John Myers of Carnegie Mellon University (jgm+@CMU.EDU)
# Minor updates for AFS 3.2 by Joseph Jackson of Transarc, Corp.
#   (jackson+@transarc.com)

# This Perl script processes salvager log files, removing all the
# "uninteresting" lines from it.  Sections that have been elided are
# replaced by ellipsis (...).  Provide the name of the log file as an
# argument to the script.

# Groups of lines are deemed "uninteresting" if they appear similar to the
# results of a successful volume salvage, that is, if they have the form:

#    CHECKING CLONED VOLUME 536873867.
#    user.jackson.backup (536873867) updated 10/27/92 15:25
#    SALVAGING VOLUME 536873865.
#    user.jackson (536873865) updated 11/03/92 14:34
#    Salvaged user.jackson (536873865): 63 files, 864 blocks

# Any non-conforming or extra lines are presumed to be errors, causing the
# unusual line to be printed along with the surrounding block of lines.

$history = '';

while (<>) {
	if (/CHECKING CLONED VOLUME (\d+)\.$/) {
		$oldhistory = '';
		$history .= $_;
		$vid = $1;
		$_ = <ARGV>;
		if (/(\S+)\.(backup|readonly) \($vid\) updated / &&
		    (!$volname || $volname == $1)) {
			$history .= $_;
			$volname = $1;
		}
		else {
			print $history;
			print $_;
			$history = $volname = '';
		}
	}
	elsif (/SALVAGING VOLUME (\d+)\.$/) {
		$oldhistory = '';
		$history .= $_;
		$vid = $1;
		$_ = <ARGV>;
		if (/(\S+) \($vid\) updated / &&
		    (!$volname || $volname == $1)) {
			$history .= $_;
			$volname = $1;
		}
		else {
			print $history;
			print $_;
			$history = $volname = '';
		}

	}
	elsif (/Salvaged $volname \($vid\): /) {
		$oldhistory = $history . $_;
		$volname = '';
		$history = "...\n";
	}
	elsif (/SALVAGING OF PARTITION \/vicep[a-z] COMPLETED$/) {
		print $history;
		print $_;
		$history = $volname = $vid = $oldhistory = '';
	}
	else {
		print $oldhistory ? $oldhistory : $history;
		print $_;
		$history = $volname = $vid = $oldhistory = '';
	}
}
