#!/usr/bin/perl
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#   uunudge [-opt].. [system]..                                               #
#   uunudge                                                                   #
# This program has the above three forms.  If given args that  are  names  of #
# uucp neighbors, it runs thru the list one at a time and  initiates  a  UUCP #
# session with each system named.  The options are:                           #
#     -a   contact all uucp neighbors.                                        #
#     -<n> for <n> an integer, repeats the task <n> times (default=1).        #
# If there are no command-line args, then we try all of  our  uucp  neighbors #
# for which we have work in /usr/spool/uucp/.                                 #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
#
# Copyright (c) 1992 by John Chambers ...{bcs.org,mit-eddie}minya.uucp!jc

$A = 0;
$D = $ENV{'V_uunudge'};	# Debug output wanted?
@H = ();
$, = " ";
$[ = 1;
for $a (@ARGV) {
	if ($a =~ /^-/) {
		if ($a =~ /^-[aA]/) {
			$A = 1;
			print "A: $A\n" if $D > 1;
		} elsif ($a =~ /^-(\d+)/) {
			$N = $1;
			print "N: $N\n" if $D > 1;
		} else {
			printf STDERR "Unknown option \"$a\" ignored.\n";
		}
	} else {
		@H = ( @H , $a);
		print "H: @H\n" if $D > 1;
	}
}
if ($A) {		# Does the caller want all neighbors? 
	print "Contact all neighbors ...\n" if $D;
	chop(@H = `uuname`);
}
if (! @H) {		# Do we have any neighbors to call?
	chop(@F = `find /usr/spool/uucp -name 'D.*' -print`);
	for $h (@F) {
		print "h: $h\n" if $D > 1;
		$h =~ s'/usr/spool/uucp/'';
		$h =~ s'/.*'';
		print "h: $h\n" if $D > 1;
		$X{$h} ++;
		print "X:",%X,"\n" if $D > 1;
	}
	@H = keys(%X);
	print "H: @H\n" if $D > 1;
}
print "Nudge",$#H,"systems:",@H,"\n" if $D; 
$N = 1 if !$N;
for ($i=1; $i <= $N; $i++) {
	for $s (@H) {
		print("Sys: ",$s,"...\n") if $D;
		truncate("/usr/spool/uucp/.Status/$s",0);
#		system("ls -l /usr/spool/uucp/.Status/$s");
		@S = ('/usr/lib/uucp/uucico','-r1','-f','-s'.$s);
#		print("Cmd:",@S,"\n") if $D;
		system(@S);
		print("Sys: ",$s,"done.\n") if $D;
	}
}
