#!/usr/bin/perl

# code creates a "globalcity" newspaper front page
# by sawad brooks
# august 15, 2002 

use Socket;
use URI;

# my $abs_url  = URI->new_abs($rel_url, $base_url);


@space = ("www.nytimes.com", "www.guardian.co.uk", "www.asahi.com");

# open(FILE,">global.html") || die("Cannot Open File");
# select(FILE); $| = 1; select(STDOUT);

select(STDOUT); $| = 1;

print "Content-type: text/html\n";
print "\n";

srand;
$cityindex = int(rand 3);

$ctr = 0;
$offset = 0;

print "<html lang=\"ja\">";
print "<head>
<META http-equiv=\"Content-Type\" content=\"text/html; charset=EUC-JP\"></head>";

while ($ctr < 3) {
	print "<div style=\"position: absolute; left: $offset","px; top: 0px\">";
	# print "connecting $space[$cityindex] ...";		
	@stream = &GetHTTP($space[$cityindex], "/");
	foreach $s (@stream) { 
		print $s; 
		# print "."; 
		}	
	print "</div>";

	$cityindex++;
	if ($cityindex > 2) { $cityindex = 0; }

	$ctr++;
	$offset = $offset + 200;
	}
	

print "<head><title>THREE POINTS IN SPACE - WITH HELP FROM SASSEN</title></head>";


exit;


sub GetHTTP {

	local($hostname, $doc) = @_;

	# print STDOUT $hostname, $doc;
	
	local($port, $iaddr, $paddr, $proto, $line, @output);

	# we ignore the "host:port" notation, and assume http=80 everytime.

	socket(SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die "socket(): $!\n";
	$paddr = sockaddr_in(80, inet_aton($hostname));
	connect(SOCK, $paddr) || die "connect(): $!\n";
	select (SOCK); $| = 1; select(STDOUT); 

	# send the HTTP-Request
	print SOCK "GET $doc  HTTP/1.0\n\n";

	# now read the entire response:
	
	do {
		$line = <SOCK>
	} until ($line =~ /^\r\n/);

	@output = <SOCK>;

	close(SOCK);   

	return @output;
}
