#!/usr/bin/perl
#
#   cmpbp [file]...
#
# Compare files with their backup versions. Tell which ones differ. At
# present,  this  program  only  recognizes  the  "*-" style of making
# backups.  When the need arises, I'll probably make it recognize  the
# $ENV{'BACKUP'} variable that Cp/Lc/Ln/Mv/Rm uses. Note the check for
# D_cmpbp and V_cmpbp in the environment, which should be a  debug  or
# verbose  level.  At level 1, we tell which files differ; at level 2,
# we tell the result for every file.
#
# The exit status is the  number  of  files  that  differ  from  their
# backup; zero means that they're all the same.

($me = $0) =~ s'.*/'';
$dbg = $ENV{"D_$me"} || $ENV{"V_$me"} || 1;
if (!@ARGV) {
	@ARGV = split(/\s/,`ls *-`);
}

$diff = 0;	# This is our exit status.

for $f (@ARGV) {
	if ($f =~ '-$') {
		$b = $f;
		$f =~ s'-$'';
	} else {
		$b = "$f-";
	}
	if ($f =~ '\.[ao]$') {
		print "Ignore $f\n" if $dbg>1;
		next;
	}
	print "Files $f $b\n" if $dbg>1;
	if (!open(D,"diff $f $b |")) {
		print "$me: Can't run \"diff $f $b \" [$!]\n";
		next;
	}
	$line = <D>;
	if ($line) {
		print "Diff: $f $b\n" if $dbg>0;
		++ $diffs;
	} else {
		print "Same: $f $b\n" if $dbg>1;
	}
}

exit $diffs;
