#!/opt/gnu/bin/perl5 -w

use CGI;
use MIME::Lite;
use Net::SMTP;

my $query = new CGI;
my $path = "/var/html/largefiles/iau";

#---------------------
# Get and check input.
#---------------------

&blank_name unless $query->param('username');
&blank_mail unless $query->param('email');
&blank_affil unless $query->param('affil');
&blank_abstr unless $query->param('abstract');
&blank_ptype unless $query->param('p_type');

my $user = $query->param('username');
my $email = $query->param('email');
my $affil = $query->param('affil');
my $abstr = $query->param('abstract');
my $ptype = $query->param('p_type');

#----------------------------
# Append file with form data.
#----------------------------

open (OUTPUT, ">>/var/html/largefiles/iau/presentation_list.html");
print OUTPUT "<tr><td>$user</td><td><a href=mailto:$email>$email</a></td><td>$affil</td><td>$ptype</td><td>$abstr</td></tr>\n";
close(OUTPUT);

#-----------------------------------
# Confirmation message for the user.
#-----------------------------------

print $query->header();
print "<b>IAU General Assembly XXVII - Joint Discussion #12</b><br><br>\n";
print "Thank you $user for submitting your presentation information.<br>\n";
print "If you have any concerns or corrections, please contact <A href=mailto:wiklind\@stsci.edu>Tommy Wiklind</A>.\n";
print "<br><br>\n";
print "<b>Your information:</b><br><br>\n";
print "Email: $email<br>\n";
print "Affiliation: $affil<br><br>\n";
print "Type of presentation: $ptype<br>\n";
print "Abstract: $abstr\n";
print "Return to the <A href=http://www.stsci.edu:8082/institute/conference/iau09/>IAU General Assembly XXVII - Joint Discussion #12</A> page.\n";

#------------
# Send email.
#------------

my $opo = "wiggs\@stsci.edu";
my $subject="JD12 Presentation";
my $body = "
Name: $user

Email: $email

Affiliation: $affil

Type of Presentation: $ptype

Abstract:

$abstr.";

my $mime_type = 'TEXT';

my $mime_msg = MIME::Lite->new(
From => $email,
To   => $opo,
Subject => $subject,
Type => $mime_type,
Data => $body);

my $ServerName='smtp.stsci.edu';
my $message=$mime_msg->body_as_string();

$smtp=Net::SMTP->new($ServerName);
$smtp->mail($opo);
$smtp->to($opo);
$smtp->data();
$smtp->datasend("Subject: $subject\n");
$smtp->datasend($message);
$smtp->dataend();
$smtp->quit();

my $mime_msg2 = MIME::Lite->new(
From => $opo,
To   => $email,
Subject => $subject,
Type => $mime_type,
Data => $body);

my $ServerName2='smtp.stsci.edu';
my $message2=$mime_msg2->body_as_string();

$smtp=Net::SMTP->new($ServerName2);
$smtp->mail($email);
$smtp->to($email);
$smtp->data();
$smtp->datasend("Subject: $subject\n");
$smtp->datasend($message2);
$smtp->dataend();
$smtp->quit();

#----------------
# Error messages.
#----------------

sub blank_name
{
    print "Content-type: text/html\n\n";
    print "<b>Missing Information:</b><br><br>";
    print "You did not tell us your name.<br><br>";
    print "Please return to the form and enter your name.";
    exit;
}

sub blank_mail
{
    print "Content-type: text/html\n\n";
    print "<b>Missing Information:</b><br><br>";
    print "You did not give us your E-mail address.<br><br>";
    print "Please return to the form and enter your E-mail address.";
    exit;
}            

sub blank_affil
{
    print "Content-type: text/html\n\n";
    print "<b>Missing Information:</b><br><br>";
    print "You did not give us your affiliation.<br><br>" ;
    print "Please use the \"back\" button on your Web browser to return<br>" ;
    print "to the form and enter your institution. Thanks..." ;
    exit;
}

sub blank_abstr
{
    print "Content-type: text/html\n\n";
    print "<b>Missing Information:</b><br><br>";
    print "You did not give us your abstract.<br><br>";
    print "Please return to the form and enter the abstract.";
    exit;
}

sub blank_ptype
{
    print "Content-type: text/html\n\n";
    print "<b>Missing Information:</b><br><br>";
    print "You did not give us your presentation type.<br><br>";
    print "Please return to the form and select the type.";
    exit;
}
