#!/bin/sh -- A comment mentioning perl, to prevent looping
eval 'exec perl -S $0 ${1+"$@"}'
	if 0;

# Perl script to eliminate from 'sccs prt' output those delta
# commentaries made, or not made, by the specified users.  E.g.
# since 931022 | prt_user -v 'ns|lbieber' | prt_brief
#
# THIS SAMPLE PROGRAM IS BEING PROVIDED "AS IS" AND ONLY AS A COURTESY TO
# THE RECIPIENT.  SUN MAKES NO WARRANTY OR REPRESENTATION, EITHER EXPRESS 
# OR IMPLIED WITH RESPECT TO THIS SAMPLE PROGRAM INCLUDING QUALITY, PERF- 
# FORMANCE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON- 
# INFRINGEMENT.  IN NO EVENT WILL SUN BE LIABLE FOR ANY DIRECT, INDIRECT,  
# SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR  
# INABILITY TO USE THIS SAMPLE PROGRAM.

$usage = "Usage: prt_user [-v] regexp [file ...]\n";

# Process the arguments.
$except = 0;   # set to 1 if -v, meaning all but these
die $usage unless @ARGV >= 1;
$regexp = shift @ARGV;
if ($regexp eq "-v") {
	$except = 1;
	$regexp = shift @ARGV;
}

### maybe_show should be called whenever we realize we've hit the
### end of a delta commentary.  It prints the previous commentary
### if it fits the criteria, and prepares for the next round.
sub maybe_show {
	return unless @header;
	local($match) = ($user =~ /^$regexp$/o);
	$match = ! $match if $except;
	print @header, @comment if $match;
	@header = ();
	@comment = ();
}

while (<>) {
	if (m|^SCCS/s\..*:$|) {
		&maybe_show;
		print @space;
		print;
		@space = ();
	} elsif (m|^D\s.*\s..:..:..\s+(\S+)\s+[0-9]*|) {
		&maybe_show;
		@header = @space;
		$user = $1;
		push(@header, $_);
		@space = ();
	} elsif (m|^$|) {
		push(@space, $_);
	} else {
		push(@comment, @space);
		push(@comment, $_);
		@space = ();
	}
}
&maybe_show;
print @space;
