#!/local/bin/perl

sub checkout {
    if ($forreal) {
	system("co",@_) || ($rval=0);
    }
}

$forreal = 1;
foreach (@ARGV) {
	if ($_ eq "-n") {
		$forreal = 0;
	} else {
		print STDERR "$0: unrecognized argument \"$_\"\n";
		exit 1;
	}
}

$|=1;

@rcsfiles = ( (<RCS/*,v>), (<*,v>) );

@basename = @rcsfiles;

foreach (@basename) {
	s-^RCS/--;
	s-,v$--;
}

# print join(' ',@basename),"\n";

$rval=0;

for ($i=0; $i<@basename; $i++) {
	$rcsf = $rcsfiles[$i];
	if ( ! -r $rcsf ) {
		print STDERR "$rcsf unreadable\n";
		$rval = 1;
		next;
	}
	@_ = stat($rcsf);
	$rcstime = $_[9];

	$file = $basename[$i];
	if ( ! -e $file ) {
		print "$file does not exist.\n";
		&checkout($file);
		next;
	}
	if ( ! -o $file ) {
		print STDERR "$file not owned\n";
		$rval = 1;
		if ( $forreal ) { next; }
	}
	@_ = stat($file);
	$mtime = $_[9];
	if ($rcstime>$mtime) {
		print "$basename[$i] out of date.\n";
		&checkout($file);
	}
}

exit $rval
