#!/usr/local/bin/perl
require "./cgi-lib.pl";

##################################################################
# This is a Question/Answer Board. The purpose of this is to let #
# users ask questions and receive answers over the web. This     #
# script can be used and modified at will, but please leave this #
# header, and comments. And do not sell this script without      #
# prior concent.                                                 #
##################################################################
# Setup:                                                         #
# Create 3 data files. One is for the questions, one is for the  #
# answers, and one is for a count. chmod these files to 666, and #
# the script to 755. Setup the variables below, and thats all.   #
#                                                                #
# 1997, Kevin Meltzer | kmeltz@cris.com | www.cris.com/~kmeltz   #
##################################################################

print &PrintHeader;
&ReadParse;

#########################################
# Set paths to data files
#########################################
$answers = "./ans.txt";                   # Answer data file (666)
$questions = "./quest.txt";               # Question data file (666)
$many = "./many.txt";                     # Count data file (666)
$page = 'http://www.iwate-pu.ac.jp/cgi-bin/p/acro-ito/qa.pl';   # Path to this script


#########################################
# Check to see what needs to be done
# by where its comming from
#########################################
$where = $ENV{'HTTP_REFERER'};


# See if it answering a question
if ($in{'answer'} ne '' && $in{'sub_question'} eq '') {
&sub_ans;
}
# See if its a submitting of a question
elsif ($in{'sub_question'} ne '' && $in{'answer'} eq '') {
&sub_ques;
}


#sub redirect {
#print "Location: $page\n\n";
#}

#########################################
# Start HTML
#########################################
print<<END_HTML;
   <html>
   <title>Question/Answer</title>
   <body bgcolor="#FFFFFF"background="http://p-www.iwate-pu.ac.jp/~acro-ito/background_textures/grey004.gif" nosave>
   <center><h1>Questions Posted</h1></center>
END_HTML

# open anser file
open (LOG, "$answers") || die "Can't open $answers $!\n";
@LOG = <LOG>;
close LOG;

# join answers in to an array and seperate 
$ant = join('',@LOG);
@string = split(/\n/,$ant);

# open question file
open (QUEST, "$questions") || die "Can't open $questions $!\n";
@QUEST = <QUEST>;
close QUEST;

$manys = 1;
$many_foo = 1;

# join questions in an array and seperate them
$quests = join('',@QUEST);
@questions = split(/\n/,$quests);

# This section is sort of redundant of the next one
# but is needed for the menu on top
print "<center>";
foreach $question (@questions) {
@foo = split(/\|/,$question);
$qnum = $foo[0];            # Question Number

print "\[<a href=\"\#$qnum\">$qnum</a>\]";
}
print "</center>\n";
### End Menu

# seperate data sections on questions
foreach $question (@questions) {
@foo = split(/\|/,$question);
$qnum = $foo[0];            # Question Number
$qtext = $foo[1];           # Question
$qname = $foo[2];           # Askers name
$qdate = $foo[3];           # Date of question

#print "<hr>$qnum\. $qtext<p>\n";
print "<hr><b><a name=\"$qnum\">$qnum</a>\. Question from $qname posted on $qdate:</b><p>\n";
print "$qtext<p>\n";
$manys++;
# seperate data sections on answers
@nums = ();
foreach $answer (@string) {
@blah = split(/\|/,$answer);
$num = $blah[0];
$text = $blah[1];
$many_foo++;

if ($num eq $qnum) {
print "<b>Answer\:</b><p> $text<p>\n";
}
#if ($num ne $qnum && $many eq $many_foo && $qnum ne 1) {
elsif ($qnum ne $num && $manys eq $many_foo && $qnum ne 1) {
print "(No Answer)<p><hr>\n";
}
}
}


#########################################
# Question form
#########################################

print "<hr><center><h1>Post a Question</h1></center><FORM METHOD=\"POST\"  ACTION=\"qa.pl\">\n";                               
print "<b>Your name: </b><INPUT TYPE=\"text\" NAME=\"who\" VALUE=\"\"><br>\n";
print "<b>Ask a question: </b><br> ";
print "<textarea name=\"sub_question\" Rows=8 cols=70 value=\"\"></textarea>\n";
print "<input type=\"submit\" value=\"Submit Question\">\n";
print "<input type=\"reset\" value=\"Clear Form\">\n";   
print "</form>\n";

#########################################
# Answer form
#########################################
print "<form method=post action=\"qa.pl\"><br>\n";
print "<b>Answer a question: </b><br>";
print "<select name=\"question\">\n";

# Show questions available
foreach $question (@questions) {
@foo = split(/\|/,$question);
$qtext = $foo[1];
$qnum = $foo[0];

print "<option>$qnum\. $qtext\n";
}

print "</select>\n";
print "<textarea name=\"answer\" Rows=8 cols=70></textarea>\n";
print "<input type=\"submit\" value=\"Submit Answer\">\n";
print "<input type=\"reset\" value=\"Clear Form\">\n";   
print "</form></BODY></HTML>\n";

#########################################
# Subroutine to handle answers
#########################################
sub sub_ans {
$quest = $in{'question'};
$ans = $in{'answer'};
$file = $answers;

# Sine the drop-down box lists the questions as NUM. QUESTION it
# also makes that the form input name so we know what question is
# being answered. We need to split the number from the question,
# take out the period (.) and log them.

@question = split(/\./,$quest);
$foo = $question[0];

open (FILE, ">>$file") || die "Can't open $file $!\n";
print FILE "$foo\|$ans\n";
close FILE;

#########################################
# Thanks for answering html
# Un-comment section below if you wish to
# have a thank-you page.
#########################################
#print<<END;
#        <html>
#        <title>Thank You</title>
#        <body bgcolor="#FFFFFF">
#        <center><h1>Thank You</h1></center>
#        <center>Thank you for your answer. <a href="qa.pl">Return</a> to the Question/Answer Board.</center>
#        </body></html>
#END
#exit;
}

#########################################
# Subroutine to handle questions
#########################################
sub sub_ques {
$question = $in{'sub_question'};
$who = $in{'who'};
$date = &date_time;
open (LOG, "$many") || die "Can't open $many $!\n";
flock (LOG, 2);
$how_many = <LOG>;
flock (LOG, 8);
close LOG;


open (COUNT, ">$many") || die "Can't open $many $!\n";
flock (COUNT, 2);
$how_many++;
print COUNT $how_many;
flock (COUNT, 8);
close COUNT;

open (QLOG, ">>$questions") || die "Can't open $question to write $!\n";
print QLOG "$how_many\|$question\|$who\|$date\n";
close QLOG;

##########################################
# Thanks for question html
# Un-comment section below if you wish to
# have a thank-you page.
##########################################
#print<<DONE;
#        <html>
#        <title>Thank You</title>
#        <body bgcolor="#FFFFFF">
#        <center><h1>Thank You</h1></center>
#        <center>Thank you for submitting a question. Please check back for responses to your question. <a href="qa.pl">Return</a> to the Question/Answer Board.</center>
#        </body></html>
#DONE
#exit;
}


##########################################
# Subroutine to get date
##########################################


sub date_time {
   # Syntax: &date_time;
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

   @abr_months = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sept","Oct","Nov","Dec");
   @abr_days = ('Sun','Mon','Tues','Wed','Thurs','Fri','Sat');

   # Returns as DayName, MonthName MonthDay, Year at hour:minute:second
   # if ($hour < 10) { $hour = "0$hour"; }
   if ($min < 10) { $min = "0$min"; }
   if ($sec < 10) { $sec = "0$sec"; }

    $date_time =  "$abr_days[$wday] $abr_months[$mon] $mday, 19$year at $hour\:$min\:$sec EST";
}



