#!/usr/bin/perl
#
#Provide a link to a new Dr. D sound clip each time, with a 
#few "empty boxes" thrown in..
#
#Or, if "all" is specified as the query string, display a list of all the 
#clips.

$dbloc='dr_demento.db';
$soundurl='sounds/';

BEGIN { push (@INC,'../include/') }
require "cgi-lib.pl";
print &PrintHeader;
use Db;
Db::load($dbloc);

$contents='';

if ($ENV{QUERY_STRING} eq 'all') {
	print <<eof;
<html>
<head><title>All Dr. Demento Sound Clips</title></head>
<body>
<h1>All Dr. Demento Sound Clips</h1>
<P>There's no need to get a box of <A HREF="index.html">Dr. Demento
cereal</A>, for your free sound clip..</P>
<P>Here are all the sound clips from Dr. Demento that are on this server:</P>
<ul>
eof
	foreach $key (sort(keys(%Db::db))) {
		if ($Db::db{$key}{desc} ne '') {
			print "<LI><A HREF=\"$soundurl$key\">$Db::db{$key}{desc}</A> from <i>$Db::db{$key}{songname}</i> by $Db::db{$key}{artist}";
		}
		else {
			print "<LI><i><A HREF=\"$soundurl$key\">$Db::db{$key}{songname}</A></i> by $Db::db{$key}{artist}";
		}
	}
	print '</ul>';
	&PrintFooter;
	print '</body></html>';
}
else {

	print <<eof;
<html>
<head><title>Dr. Demento Cereal</title></head>
<body>
<h1>Inside, you find..</h1>
eof
	srand($$ ^ time); #seed random number generator.
	@dbkeys=keys(%Db::db);
	$num=int(rand($#dbkeys+1));
	$key=$dbkeys[$num];
	if ($num > $#dbkeys) {
		$contents="Nothing in this box. What a rip off!";
	}
	else {
		if ($Db::db{$key}{desc} ne '') {
			$contents="<A HREF=\"$soundurl$key\">$Db::db{$key}{desc}</A> from <i>$Db::db{$key}{songname}</i> by $Db::db{$key}{artist}";
		}
		else {
			$contents="<i><A HREF=\"$soundurl$key\">$Db::db{$key}{songname}</A></i> by $Db::db{$key}{artist}";
		}
	}
	print $contents,<<eof;
<P>Looks like <A HREF="dr_demento.cgi">just down the shelf</A>,
there's another box of <A HREF="index.html">Dr. Demento cereal</A>..
<P><img src="dr_d_cereal_tipped.gif" alt="A tipped over cereal box">
eof
	&PrintFooter;
	print "</body></html>";
}
