#!/usr/bin/perl -w

# Create a random pokey cartoon
# Waider May 2000
use LWP::UserAgent;

# 1. get the list of cartoons, so we know how many there are.
# http://www.lysator.liu.se/pokey/archive/
$ua = new LWP::UserAgent;
$ua->env_proxy();

$req = new HTTP::Request GET => "http://www.lysator.liu.se/pokey/archive/";
$res = $ua->request( $req );
if ( $res->is_success ) {
  $content = $res->content;
} else {
  die "Can't get index page.\n";
}

# Find the max number off the list
( $max ) = $content =~ m/^.*index(\d+).html\D+$/si;

# Right, now let's make up a cartoon. If it's over four frames, we'd
# better check if the frames exist.
$frames = int( rand( 3 )) + 3; # 4 - 7 frames

print <<"EOH";
Content-Type: text/html\n\n

<html>
<head>
<base href="http://www.lysator.liu.se/pokey/archive/">
</head>
<body>
<center>
<i><h1>POKEY THE PENGUIN!!</h1></i>
<img src="pokey.gif"><p>
EOH

for $i ( 0..$frames ) {
  $i[ $i ] = int( rand( $max ) + 1 );
  if ( $i > 3 ) {
	# check if the frame exists
	$req = new HTTP::Request HEAD => 
	  "http://www.lysator.liu.se/pokey/archive/pokey$i[$i]_" . ( $i+1 ) . ".gif";
	$res = $ua->request( $req );
	if ( $res->is_success ) {
	  # that's okay
	} else {
	  print "<!-- failed to get pokey$i[$i]_" . ( $i + 1 ) . ".gif -->\n";
	  last; # end the cartoon here.
	}
  }
  print "<img src=\"pokey$i[$i]_" . ( $i+1 ) . ".gif\"><p>\n";
}

print <<"EOH";
<i>HELLO <a href="mailto:pokey\@yellow5.com">.  POKEY THE PENGUIN LOVES TO</a> READ YOUR EMAIL</i><p>


POKEY THE PENGUIN IS COPYRIGHT &copy; 1998 THE AUTHORS
</center>
</body>
</html>
EOH
