#!/usr/local/bin/perl

#------------------------------------------------------------------------------
# Webmonkey Movie Database v1.0
# Written by Colin Ferm
# ferm@wired.com
#
# Feel free to use this for your own purposes, changing it as you wish. Please
# leave this header in all modifications, noting the changes made, and adding
# your name to the author field. That's all we ask, and that you send on a URL
# to wherever it's used, just so I can see all the neat things people might
# do with it.
#
# 1.0 - First written for a Webmonkey article by Colin Ferm 11/24/98
#------------------------------------------------------------------------------

#------------------------------------------------------------------------------
# Below is the standard input-getter thingy, which will grab the input regardless
# of whether it's "get" or "post," and will save it to $in.
#------------------------------------------------------------------------------ 


my $in;
if ($ENV{'REQUEST_METHOD'} eq "GET") {
	$in = $ENV{'QUERY_STRING'};
} else {
	$in = <STDIN>;
}

#------------------------------------------------------------------------------
# The first line pulls out the "+"s that should be spaces, and the second
# line converts URI characters back to nonalphanumeric characters.
# The line after that splits the input into %movie.
#------------------------------------------------------------------------------

$in =~ s/\+/ /g;
$in =~ s/%(..)/pack("c",hex($1))/ge;
my %movie = split (/=/, $in);

#------------------------------------------------------------------------------
# These lines read the database in to the variable @data and close the file.
#------------------------------------------------------------------------------

open (DATA, "/usr/people/ferm/movies.dat");
my @data = <DATA>;
close (DATA);

#------------------------------------------------------------------------------
# This first print prints out the top of the document with the <body> tags
# and just generally sets the flavor of the look of the page.
#------------------------------------------------------------------------------

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

<html>
<body bgcolor="#000000" text="#ffffff">
<center><h2>Results</h2></center><p>
<center><hr size=1 width=50%></center>
END

#------------------------------------------------------------------------------
# This for loop is the real meat of the script. It runs through the array
# looking for the title, then prints the relevant info out (depending on if
# it's there or not).
#------------------------------------------------------------------------------

for ($i = 0; $i <= $#data; $i++) {
	if ($data[$i] =~ /-- $movie{'title'} --/i) {
		print <<END;
<center><b><font size="+1">$data[$i + 1]</font></b></center><br>
$data[$i + 2]<br>
END
		last;
	} elsif ($i == $#data) {
		print "<b><i>Sorry! I haven't reviewed that one yet!</i></b>";
	}
}

#------------------------------------------------------------------------------
# This prints out the footer of the document.
#------------------------------------------------------------------------------

print <<END;
<center><hr size=1 width=50%></center>
</body>
</html>
END