#!/usr/bin/perl
# diffgrep - extract single file diffs from multifile diffs.
# -h only print diff headers
# -v reverse match
require 'getopts.pl';

&Getopts('hv'); 

$pat = shift(@ARGV);
$pat =~ s/\'/\\\'/g;
#$expr = "\$_ " . ((defined $opt_v) ? "!" : "=") . "~ \'$pat\';";

$p=((defined $opt_h) ? 0 : 1);

while (<>) {
	if (/^Index/) {
		$pre = "";  
	} 

	if (/^diff/) {	
		$x = $_; 
		if (/$pat/) { $p = 1; } else { $p = 0; } 
		$p = !$p if defined($opt_v); 
		$_ = $x;

#		print "$p\n";

		if ($opt_h) {
			print if $p;
			$p = 0; next;
		}

		next unless $p; 

		undef $pre ;
	}


	next if /^\?/;
	print "$_" if $p;
	$pre .= $_ if defined($pre); 
}
