#!/usr/bin/perl
#
#NAME
#  NewURL - add new URL to abcbot's database.
#
#SYNOPSIS
#  NewURL URL..
#  NewURL <URLfile
#
#DESCRIPTION
#  Given one or more URLs on the command line or in stdin,  we  create
#  an  entry for the host and fire up a run of abcbot to scan the file
#  for ABC tunes or links to other possible tunes.
#
#FILES
#  add/*   Where new URLs are put for future use.
#  hst/*   Where per-host data is kept.
#
#AUTHOR
#  John Chambers <jc@trillian.mit.edu>
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

require &LocalSetup(2);	# Figure out localization stuff
print "$P: Started with V=$V.\n" if $V>0;

$! = 1;

$botdir = $ABCdir{bot};
$botpth = "$webdir/$botdir";
if (-d $botpth) {
	chdir $botpth;
} else {
	print STDERR "$P: bot dir \"$botpth\" doesn't exist!\n" if $V>0;
}
for $url (@ARGV) {
	print "$P: url: \"$url\"\n" if $V>2;
	if (($prot,$host,$file) = ($url =~ m"^(\w+)://([-_:.\w]+)(/.*)")) {
		print "$P: prot \"$prot\" host \"$host\" file \"$file\"\n" if $V>1;
		if (lc($prot) =~ /http/) {
			&try(lc($prot),lc($host),$file);
		} else {
			print STDERR "$P: Unknown protocol \"$prot\" in \"$url\"\n" if $V>0;
		}
	} else {
		print STDERR "$P: Can't parse \"$url\"\n" if $V>0;
	}
	++$urls;
}
if ($urls < 1) {
	for $line (<>) {
		chomp $line;
		print "$P: line \"$line\"\n" if $V>2;
		if (($url,$prot,$host,$file) = ($line =~ m"^\s*((\w+)://([\w:]+)(/\S*))")) {
			print "$P: url: \"$url\"\n" if $V>2;
			print "$P: prot \"$prot\" host \"$host\" file \"$file\"\n" if $V>2;
			if (lc($prot) =~ /http/) {
				&try(lc($prot),lc($host),$file);
			} else {
				print STDERR "$P: Unknown protocol \"$prot\" in \"$url\"\n" if $V>0;
			}
		} else {
			print STDERR "$P: Can't parse \"$url\"\n" if $V>0;
		}
		++$urls;
	}
}

exit 0;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #

sub LocalSetup { $Vtest = shift;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Figure out where we're running, and try to require a *cgilocal.pm file  for #
# local  settings.   The  end  result of this is to initialize a long list of #
# global variables.  The return value is the name of the cgilocal file, which #
# we usually feed to the 'require' command.                                   #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	$ENV{PATH} = '/bin:/sbin:/usr/bin:/usr/sbin';
	push @INC, '.';
	$testing = '0';	# Set to 1 while testing
	umask 0002;		# Output files must be group writable
	$| = 1;			# Unbuffered STDOUT
	$" = ',';		# Used in verbose messages
	($P = $0 || 'NewURL') =~ s".*/"";
	(($ENV{"V_$P"} || $ENV{"D_$P"} || $testing) . ' 1') =~ /(\d+)/;	# Verbose level
	$V = $1;
	($ENV{REMOTE_ADDR} || '0.0.0.0') =~ m/^\s*([\d.]+)\s*$/;
	$RA = $1;
	if (defined($x = $data{V}) && ($x = /^\s*(\d+)\s*$/)) {
		$V = $1;
		&lsend("V=$V (from data).\n") if $V>3;
	} elsif ($RA eq '207.172.223.184') {	# My home machine
		$V = $Vtest if $V<$Vtest;
	} elsif ($RA =~ /^192\.168\./) {	# My home network
		$V = $Vtest if $V<$Vtest;
	}
	local($ss,$mm,$hh,$DD,$MM,$YY) = gmtime(time);		# Current date/time
	$ymd = sprintf("%d-%02d-%02d",1900+$YY,1+$MM,$DD);	# CCYY-MM-DD
	$hms = sprintf("%02d:%02d:%02d",$hh,$mm,$ss);		# HH:MM:SS
	$hostname = `/bin/hostname`;		# What does this machine call itself?
	$hostname =~ s/^\s*([-.\w]*)([\r\s]*)$/$1/;		# Strip off domain info
	($host = $1) =~ s/\..*//;			# Extract first field of name
	print "$P: V=$V on $host at $ymd $hms\n" if $V>1;
	($cwd = `/bin/pwd`) =~ s/[\r\s]+$//;
	$hstloc = $host . "-cgilocal.pm";
	print "$P: hstloc=\"$hstloc\" cwd=\"$cwd\"\n" if $V>1;
	$cgiloc = (-f $hstloc) ? $hstloc : 'cgilocal.pm';
	print "$P: cgiloc=\"$cgiloc\"\n" if $V>1;
	return $cgiloc;
}

sub try {my $F='try'; local($prot,$host,$file) = @_;
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
	$host =~ s":80$"";
	if (open(HST,">>add/$host")) {
		print HST "# $host\n";
		print HST "\n0 U D:1 $file\n";
	} elsif (open(HST,">>hst/$host")) {
		print HST "\n0 U D:1 $file\n";
	} else {
		print STDERR "$P: Can't write \"hst/$host\" ($!)\n" if $V>0;
	}
	close HST;
	$ENV{"V_U"} = $V;
	$cmd = "./U '$host'";
	system $cmd;
}
