#!/usr/local/bin/perl

#use DBI module
use DBI qw(:sql_types);

my ($databaseName, $databaseUser, $databasePw, $dbh);
my ($stmt, $sth, @newRow);

$validAdd = "kingj\@unr.nevada.edu";
$content = "Content-Type: TEXT/PLAIN; charset=US-ASCII";
$separater = "#";
$comment = "!";

$databaseName = "DBI:mysql:AndromedaGradInfo_DB:mysql";
$databaseUser = "andromeda";
$databasePw = "gradinfo";

$dbh = DBI->connect($databaseName, $databaseUser, $databasePw) ||
	die "Connect failed: $DBI::errstr\n";

$sourceFlag = 0;
$contentReady = 0;
$format = 0;

@line=<stdin>;

$j = 0;
foreach $i (@line) {
	$j++;
	if (index($i, $separater) == 0 && $j>1)
		{
		$format = $j;
		}
	}

if ($format < $j && $format > 0)
	{
	$format = 1;
	}

$start_ans = 0;
foreach $i (@line)
{
   if (index($i, $comment) != 0)
   {	
      if ($format == 1 && $contentReady == 1)
      {
	if (index ($i, $separater) == 0)
	{
		$start_ans = 1;
	}

	else
	{
		if ($start_ans == 1)
		{
		   $ans_string = $ans_string.$i;
		}
		else
		{
		   $ques_string = $ques_string.$i;
		}
	}
      }
	if (index($i, $validAdd) >= 0)
	{
		$sourceFlag = 1;
	}
	
	if ($sourceFlag == 1 && index($i, $content) >= 0)
	{
		$contentReady = 1;
	}
   }
}

if ($format == 1)
{
   $dbh->do("insert into andromeda_faq (question, answer) values ('".$ques_string."', '".$ans_string."')");
}

$dbh->disconnect();
