#!/usr/bin/perl
#
#NAME
#  UpdateHosts - update ABC info for list of hosts.
#
#SYNOPSIS
#  UpdateHosts [options].. [host].. &
#
#REQUIRES 

	require "Vopt.pm";

#DESCRIPTION
#  Update the ABC info for a list of hosts.  You can put  host  names
#  on the command line, and we will update only those hosts. If there
#  are no listed hosts, we look in the hst/ and add/ directories, and
#  update all the hosts found there.
#
#  At present, abcbot defaults to scanning a URL if its timestamp  is
#  more than 24 hours old.
#
#  The host names on the command line may be  preceded  by  directory
#  names. We strip off anything before a '/'. We also remove trailing
#  hyphens, so as to ignore backup file names.
#
#  This implements a single pass through the hosts, which will follow
#  links  one  hop.  Files that are more than one hop from a URL will
#  require multiple passes.
#
#OPTIONS
#
#BUGS
#
#SEE ALSO
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>

$| = 1;
($P = $0) =~ s".*/"";
&Vopt($ENV{"V_$P"} || $ENV{"V"} || '2');

@add = (); $adds = 1;	# Scan all hosts in the add directory
@hst = (); $hsts = 1;	# Scan all hosts in the hst directory
@new = (); $news = 0;	# Scan all hosts in the new directory

for $a (@ARGV) {
	if (($flg,$opt) = ($a =~ /^([-+])(.*)/)) {
		print V "$P	Option \"$flg$opt\"\n" if $V>1;
		push @opts, $a;
	} else {
		$a =~ s"\.LCK$"";
		$a = lc($a);
		$a =~ s"^\w*/+"";
		$a =~ s"-+$"";
		$a =~ s"-add$"";
		print V "$P	Host \"$a\"\n" if $V>1;
		push @lst, $a unless $dup{$a};
		++ $dup{$a};
	}
}
@hst = @add = @new = ();
unless (@lst) {
	$" = ',';
	if ($adds) {
		@hst = glob("hst/*");
		print V "$P	hst=(@hst)\n" if $V>4;
	}
	if ($adds) {
		@add = glob("add/*");
		print V "$P	add=(@add)\n" if $V>4;
	}
	if ($news) {
		@new = glob("new/*");
		print V "$P	new=(@new)\n" if $V>4;
	}
	@lst = grep(!/(-|\.LCK)\s*$/,(@hst, @add, @new));
}
for $p (@lst) {		# Merge them into one list
	$p =~ s'^.*/'';	# Strip away the directory name
	$p =~ s'-+$'';	# Trim trailing hyphens
	++$hosts{$p};	# List of hosts to test
}
@lst = sort keys %hosts;
print V "$P	lst=(@lst)\n" if $V>4;
$nhosts = int(@lst);
print V "$P	We have $nhosts hosts.\n"; # if $V>1;

host:
foreach $h (@lst) {
	print V "\n$P	Next host \"$h\"\n" if $V>2;
	$h =~ s"^\w*/+"";			# Remove initial directory name
	&Vopt($V . "log/$h.log");	# Create new logfile for each host
	if ($h =~ /^\d/) {
		print V "$P	HOST \"$h\" ignored (starts with digit).\n" if $V>0;
		next host;
	}
	next if $done{$h};
	print V "$P	HOST \"$h\"\n" if $V>1;
	$logfil = "log/$h";
	$ENV{"V_abcbot"} = "$V$logfil";
	$" = ' ';
	$cmd = "./abcbot " . join(' ',@opts) . " +CURLs $h";
	print V "$P	cmd=\"$cmd\"\n" if $V>2;
	system $cmd;
	++$done{$h};
}
