#!/usr/local/bin/perl

require 'parseform.pl';

srand(time());
%queuenames = ('serious', '/var/local/www/oracle/seriousq',
		'silly', '/var/local/www/oracle/sillyq');
%archivenames = ('serious', '/var/local/www/oracle/serious.archive',
		'silly', '/var/local/www/oracle/silly.archive');

print("Content-Type: text/html\n\n");

read(STDIN, $pdata, $ENV{'CONTENT_LENGTH'});
%fields = &parseform($pdata);

if($fields{'question'}){
	&submit_question;
	&give_question;
}
elsif($fields{'answer'}){
	&submit_answer;
	print("<h1>Thank You</h1><hr>\n");
	&main_page;
}
elsif($fields{'gimme'}){
	&give_question;
}
else{
	&main_page;
}

sub main_page{
	print <<ENDOFTEXT;
<h1>Ask A Question</h1>
<form method=POST>
<fnord>
Your email: <input name="email"><p>
<select name="type">
<option value="silly">Silly Questions
<option value="serious">Serious Questions
</select>
Your question:<p>
<textarea cols=40 rows=17 name="question"></textarea>
<p><input type="submit" value="Ask Question">
</form>
<a href="/silly.html">Archive of Silly Questions</a>
<a href="/serious.html">Archive of Serious Questions</a>
ENDOFTEXT
}

sub submit_answer {
	$queue = $queuenames{$fields{'type'}};

	$/ = "";
	open(QUEUE, $queue);
	while(<QUEUE>){
		($who, $question) = split("", $_);
		if($fields{'q'} ne $question){
			push(@questions, $who."".$question);
		}
		else{

		}
	}
	close(QUEUE);

	unlink($queue);
	open(QUEUE, ">>$queue");
	for $q (@questions){
		print(QUEUE $q) if $q =~ /\S/;
	}
	close(QUEUE);

	$archive = $archivenames{$fields{'type'}};
	open(ARCH, $archive);
#	print("<li>Debug-- Reading old archive\n");
	$/ = "\n";
	while(<ARCH>){
#		print("<li>Got item [$_] out of list ($inlist)\n");
		$inlist=0 if m,\</dl\>,;
		push(@arch, $_) if $inlist;
		$inlist=1 if /\<dl\>/;
	}
	close(ARCH);

	unlink($archive);

	open(ARCH, ">>$archive");
	&archive_header;
	print(ARCH "<dt>$fields{'q'}\n<dd>$fields{'answer'}\n");
	for $a (@arch){
		print(ARCH "$a\n");
	}
	&archive_footer; 
}

sub archive_header {
	print(ARCH "<h1>Here's the archive:</h1>\n");
	print(ARCH "<dl>\n"); } 
sub archive_footer {
	print(ARCH "</dl>\n"); }

sub question_header {
	print <<ENDOFTEXT; 
<h1>Please answer the following question:</h1>
<hr>
ENDOFTEXT
}

sub question_footer {
	print <<ENDOFTEXT; 
<hr>
<form method=POST>
<input name="q" value="$questions[$n]" type=hidden>

<input name="type" value="$fields{'type'}" type=hidden>
<textarea cols=40 rows=17 name="answer"></textarea><p>
<input type="submit" value="Answer this question">
</form>
<form method=POST>
<input name="gimme" value="gimme" type=hidden> 
<input name="type" value="$fields{'type'}" type="hidden">
<input type="submit" value="Give me a different question">
</form>
<a href="/silly.html">I don't want to answer this, let's see past questions</a>
ENDOFTEXT
}

sub give_question{
	$queue = $queuenames{$fields{'type'}};

	$/ = "";
	open(QUEUE, $queue);
	while(<QUEUE>){
		($who, $question) = split("", $_);
		push(@questions, $question);
	}
	close(QUEUE);
	$n = int(rand($#questions));

	&question_header;
	print $questions[$n];
	&question_footer;
}

sub submit_question {
	# form has the following fields:
	# email address ('email')
	# question type ('type', serious or silly)
	# question ('question')

	$queue = $queuenames{$fields{'type'}};

	open(QUEUE, ">>$queue");
	print(QUEUE $fields{'email'}, "", $fields{'question'}, "");
	close(QUEUE);
}
