#!/afs/athena/contrib/perl/p

require 'sys/socket.ph';

@url = ($ARGV[0]);

while($url = shift(@url)) {

print("Fetching $url\n");
$doc = &wwwfetch($url);
#$doc =~ s/<a [^>]*>([^<]+)<\/a>/\[4m\1\[0m/gi;
#$doc =~ s/\<[^\>]*>//g;
if($doc =~ /.*$ARGV[1].*/i){
	print("Match at $url: $&\n");
}
while($doc =~ /href *= *"?([^"> ]+)"?/ig){
	$link = $1;
	if($link=~/http/){
		push(@url, $link);
	}
	elsif($link =~ /^\//){
		$url =~ /(http:\/\/[^\/]+)\//;
		$base = $1;
		$fin = $base.$link;
		push(@url, $fin);
	}
	else{
		$url =~ /(.+\/)[^\/]*$/;
		$base = $1;
		$fin = $base.$link;
		push(@url, $fin);
	}
}
}

sub wwwfetch {
    local($URL) = @_;
    $URL =~ /http:\/\/([^\/]+)\/(.*)/;
    $hostport = $1; $path = $2;

    ($them, $port) = split(/:/, $hostport);
    $port = 80 unless $port;
#    print("Connecting to $them on port $port to get $path\n");
    $AF_INET = 2;
    $SOCK_STREAM = 1;
    
    $sockaddr = 'S n a4 x8';
    
    chop($hostname = `hostname`);
    
    ($name,$aliases,$proto) = getprotobyname('tcp');
    ($name,$aliases,$port) = getservbyname($port,'tcp')
        unless $port =~ /^\d+$/;;
    ($name,$aliases,$type,$len,$thisaddr) =
        gethostbyname($hostname);
    ($name,$aliases,$type,$len,$thataddr) = gethostbyname($them);
    
    $this = pack($sockaddr, &AF_INET, 0, $thisaddr);
    $that = pack($sockaddr, &AF_INET, $port, $thataddr);
    # Make the socket filehandle.
 
    if (socket(S, &AF_INET, &SOCK_STREAM, $proto)) { 
    }
    else {
	return(0);
    }
    
# Give the socket an address.
 
    if (bind(S, $this)) {
    }
    else {
	return(0);
    }
    
    select(S); $|=1;
    select(STDOUT);
    if (connect(S,$that)) {
	print(S "GET /$path perlWWW\n\n\n");
	while(<S>){
	    $resp .= $_;
	}
	close(S);
	$resp;
    }
    else{
	return(0);
    }
}
