#!/usr/bin/perl5

use CGI;
CGI::ReadParse(*vars);
$|=1;

# $|=1 makes output unbuffered... just a matter of personal preference

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

# decide which page to display or action to perform based on the value of
# the "page" CGI variable

page1() if !$vars{'page'};
page2() if $vars{'page'} eq '2';
page3() if $vars{'page'} == 3;
page4() if $vars{'page'} == 4;


sub page1 {
print <<"TheEnd";
<HTML>
<HEAD>
<TITLE>The Bridge of Death</TITLE>
</HEAD>
<BODY>
<FORM ACTION="scene23.cgi" METHOD=post>
<P>
Who would cross the Bridge of Death must answer me these questions three,
ere the other side he see.
</P>
<P>
What... is your name?
<SELECT NAME="name">
	<OPTION VALUE="Launcelot" SELECTED>Sir Launcelot of Camelot
	<OPTION VALUE="Robin">Sir Robin of Camelot
	<OPTION VALUE="Galahad">Sir Galahad of Camelot
	<OPTION VALUE="Arthur">Arthur, King of the Britons
</SELECT>
<INPUT TYPE=SUBMIT VALUE="Answer">
<INPUT TYPE=HIDDEN NAME="page" VALUE=2>
</FORM>
</BODY>
</HTML>
TheEnd
;
}

sub page2 {
print <<"TheEnd";
<HTML>
<HEAD>
<TITLE>The Bridge of Death</TITLE>
</HEAD>
<BODY>
<FORM ACTION="scene23.cgi" METHOD=post>
What...is your quest?
<BR>
TheEnd
;
print "<INPUT TYPE=SUBMIT VALUE=\"";
if ($vars{'name'} ne 'Galahad') {
	print "To seek the Holy Grail\n";
} else {
	print "I seek the Grail\n";
}
print "\">\n";
# Yeah, I know there's really nothing to get from the user at this point

print <<"TheEnd";
<INPUT TYPE=HIDDEN NAME="name" VALUE="$vars{'name'}">
<INPUT TYPE=HIDDEN NAME="page" VALUE=3>
</FORM>
</BODY>
</HTML>
TheEnd
;
}

sub page3 {
print <<"TheEnd";
<HTML>
<HEAD>
<TITLE>The Bridge of Death</TITLE>
</HEAD>
<BODY>
<FORM ACTION="scene23.cgi" METHOD=post>
TheEnd
;
if ($vars{'name'} eq 'Launcelot') {
print <<"TheEnd";
What... is your favorite color?<BR>
<INPUT TYPE=SUBMIT VALUE="Blue">
TheEnd
;
}
elsif ($vars{'name'} eq 'Robin') {
print <<"TheEnd";
What...is the capitol of Assyria?<BR>
<INPUT TYPE=SUBMIT VALUE="I don't know that!">
TheEnd
;
}
elsif ($vars{'name'} eq 'Galahad') {
print <<"TheEnd";
What... is your favorite color?<BR>
<INPUT TYPE=SUBMIT VALUE="Blue. No, yel..">
TheEnd
;
}
elsif ($vars{'name'} eq 'Arthur') {
print <<"TheEnd";
What... is the air-speed velocity of an unladen swallow?
<INPUT TYPE=SUBMIT VALUE="What do you mean? An African or European swallow?">
TheEnd
;
}
print <<"TheEnd";
<INPUT TYPE=HIDDEN NAME="name" VALUE=$vars{'name'}>
<INPUT TYPE=HIDDEN NAME="page" VALUE=4>
</FORM>
</BODY>
</HTML>
TheEnd
;
}

sub page4 {
print <<"TheEnd";
<HTML>
<HEAD>
<TITLE>The Bridge of Death</TITLE>
</HEAD>
<BODY>
TheEnd
;
if ($vars{'name'} eq 'Launcelot') {
	print "Right.  Off you go.";
} elsif ($vars{'name'} eq 'Robin' || $vars{'name'} eq 'Galahad') {
	print "Auuuugh!";
} elsif ($vars{'name'} eq 'Arthur') {
	print "Huh? I-- I don't know that. Auuuuuugh!";
}
print <<"TheEnd";
</BODY>
</HTML>
TheEnd
;
}
