#!/usr/bin/perl -w

#######################################################################
# Adapted from GO-absproof                                            #
#######################################################################

use CGI qw/:standard :html -no_xhtml/;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Net::SMTP;
use strict;

my $query = new CGI;
my $vals = $query->Vars;
my @errs;

if (! $vals->{contactName} || $vals->{contactName} !~ /\S+/)
{	push @errs, "Please include a contact name.";
}

if (! $vals->{contactEmail} || $vals->{contactEmail} !~ /\S+/ || $vals->{contactEmail} !~ /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/)
{	push @errs, "Please check that your email address is entered correctly.";
}

if (! $vals->{contactText} || $vals->{contactText} !~ /\S+/)
{	push @errs, "Please include a message.";
}

if (! $vals->{contactTopic} || $vals->{contactTopic} !~ /\S+/)
{	# just fill in the topic as general
	$vals->{contactTopic} = 'general';
}

if (@errs)
{	&dienice(\@errs);
}

if ( $vals->{contactName} eq $vals->{contactEmail} && $vals->{contactName} eq $vals->{contactText} )
{	start();
	print h2("Success!");
	print p("Thanks for contacting the Gene Ontology.");
	end();
	exit;
}

sendmail($vals);

start();
print h2("Success!");
print p("Your message has been successfully sent to the Gene Ontology helpdesk, and you have been emailed a copy for your records. Thank you for contacting the Gene Ontology; your query will be answered soon.");
print p("Return to the <a href='http://www.geneontology.org'>GO website</a>");
end();

exit;

##################################################################

sub readFile
{
	open(FH, $_[0]) or die "Can't open file $_[0]!";
	my @data = <FH>;
	close FH;
	return @data;
}

sub sendmail
{
	my $params = shift;

	$ENV{PATH} = "/usr/lib";

	my $titles = {
		annot => 'Annotation',
		ont => 'Ontology',
		sw => 'Software',
		tools => 'Tools',
		general => 'General query',
		errors => 'Error',
	};

	my $sub = $titles->{ $params->{contactTopic} } || "General query";
	# send the message to the list

#	open (MAILGOHELP , "| /bin/mail -s 'GO Help query (from website)' -f " . $params->{contactEmail} . " go-helpdesk\@geneontology.org") || die "Could not open mailer\n";
	open (MAILGOHELP , "| /bin/mail -s 'GO Help query (from website)' go-helpdesk\@geneontology.org -- -f " . $params->{contactEmail}) || die "Could not open mailer\n";
	print MAILGOHELP "\nEmail: " . $params->{contactEmail} . "\nName: $sub (from " . $params->{contactName} . ")\nText: " . $params->{contactText} . "\n";
	close(MAILGOHELP);

	open (MAILUSER , "| /bin/mailx -s '$sub (from " . $params->{contactName} . ")' " . $params->{contactEmail} . " -- -f " . $params->{contactEmail}) || die "Could not open mailer\n";
	print MAILUSER "\nEmail: " . $params->{contactEmail} . "\nName: $sub (from " . $params->{contactName} . ")\nText: " . $params->{contactText} . "\n";
	close(MAILUSER);

#testing
	open (MAILGOHELP , "| /bin/mail -s 'GO Help query (from website)' cherry\@stanford.edu -- -f " . $params->{contactEmail}) || die "Could not open mailer\n";
	print MAILGOHELP "\nEmail: " . $params->{contactEmail} . "\nName: $sub (from " . $params->{contactName} . ")\nText: " . $params->{contactText} . "\n";
	close(MAILGOHELP);
#testing
}

sub dienice
{	my $errs = shift;
	$errs = [ $errs ] if ! ref $errs;
	start();
	print h2("Error");
	print p("Your message could not be delivered. Please correct the following details and resubmit it.");
	print ul(
		li($errs)
	);
	print p("Please use your browser's BACK button to return to the form and submit the correct data.");
	end();
	exit;
}

sub start
{
	my @head = readFile("../html/header.html");
#	my @head = readFile('/Users/gwg/go/www/header.html');
	print header,
	start_html(-head => meta({-http_equiv => 'Content-Type', -content => 'text/html; charset=iso-8859-1'}),
		 -style => { -src =>'http://www.geneontology.org/stylesheets/stylesheet.css'},
		 -title => 'Query Submission Results');
	print $_ foreach (@head);
	print '<div id="main">',
	h1('Query Submission Results'), '<div class="block">';
}

sub end
{
	print '</div></div>';
	my @foot = readFile("../html/footer.shtml");
#	my @foot = readFile('/Users/gwg/go/www/footer.shtml');
	print $_ foreach (@foot);
	print end_html;
}