#!/usr/local/bin/perl
use strict

# Pat's brand-new teamlist adding form.
# Created 9/6/96
# Needs perl5 and CGI.pm

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

$root     = "/mnt/httpd/share";
$listfile = "$root/cgi-data/teamlist/teamlist.html";

$mail_prog = "/usr/lib/sendmail -t";
$mail_replyto = "50k-announce-owner\@mit.edu";
$mail_sendto = "50k-announce-request\@mit.edu";

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

use CGI;

# Begin Program

# Make new CGI object

my $query = new CGI;

# Print out header stuff

print $query->header;
print $query->start_html(
	-title=>'MIT Entrepreneurship Competition Teambuilding List',
	-author=>'pmccormi@mit.edu',
	-BGCOLOR=>'#FFFFFF');

print "<H1>MIT Entrepreneurship Competition<BR> Teambuilding List</H1>\n";

# Now we check our status; do we have form data to process?
#     If we do, then we check to see if there are errors.
#        If there are errors, print them and the form again.
#     Otherwise, if no errors, print out a success message
#          and the list (no form).
# Otherwise, print the intro message, the form, and the list.

if ($query->param('Action') eq 'Add To List') {

    # we have some data

    print "<h2>Entry Results</h2>\n";

    # check data

    if (&check_entry($query) == 0) {  # if no errors

	&print_success($query);

	&add_entry($query);

	if ($query->param('add_to_maillist') eq 'yes') {
	    &subscribe_to_list($query);
	}

	&print_list($query);
    }
    else {  # errors!  (check_entry has already printed them)

	print "<HR><H2>Errors Found</H2>\n";
	print "<P>Please correct the errors above on the form below.\n";

	&print_form($query);
    }

}
else {
    # Normal operation; no data

    &print_intro($query);
    &print_form($query);
    &print_list($query);
}

&print_tail;
print $query->end_html;

# End of Program


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

sub print_intro {
    my($query) = @_;

    print <<END;

<P>This is a list of people who either have an idea and want teammates,
or who want to enter the Competition but want an idea. If you want to be 
added to the list, fill out the short form below. 
<b>All items are mandatory.</b>

<P>
To start the new term, the list was cleared on 9/10/99.  If you 
entered your name on the list before this point, you'll have to do it again.

<p>If you wish to be removed, please send mail to
<a href="mailto:50k-online\@mit.edu">50k-online\@mit.edu</a>.

<P>We want to know how we can improve this service; please send your
comments and anecdotes to the Organizing Team at <A
HREF="mailto:50k-org\@mit.edu">50k-org\@mit.edu</A>. We would love
quotes to promote this service in the future if it proves helpful.

<P><B>PLEASE READ:</B> 

<P>This list is provided for prospective Competition entrants <B>only</B>.
Any use of this list for promotional purposes will jeopardize future
Teambuilding efforts.

END

}

sub print_form {
    my($query) = @_;
    
    print $query->startform;

    print "<p><B>Name:</B>";
    print $query->textfield(
			    -name=>'name',
			    -size=>40);

    print "<p><B>Email Address:</B>";
    print $query->textfield(
			    -name=>'email',
			    -size=>40);
    
    print "<p><B>Phone Numbers:</B>";
    print "\nHome:";
    print $query->textfield(
			    -name=>'home_phone',
			    -size=>15);
    
    print "\nWork:";
    print $query->textfield(
			    -name=>'work_phone',
			    -size=>15);

    print "<p><b>Current MIT Affiliation (course and year):</b>";
    print $query->textfield(
			    -name=>'class',
			    -size=>20);


    print "<p><b>Skills You Can Offer:</b> (Two lines only)";
    print $query->textarea(-name=>'skills',
			   -rows=>2,
			   -columns=>50);

    print "<p><b>Projects You Want To Work On/Projects You Want Help With:</b> (Two lines only)";
    print $query->textarea(-name=>'projects',
			   -rows=>2,
			   -columns=>50);


    print "<p><b>Add Me To The 50k-teambuilding Mailing List:</b> ";

    print $query->checkbox('add_to_maillist','checked','yes','');

    print "<p>";
    print $query->submit('Action','Add To List');
    print $query->reset;
    print $query->endform;
}

sub print_list {
    my($query) = @_;
    my($date);

    # This just opens the teamlist file, dumps it, and leaves
    
    print "<HR>";

    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
             $atime,$mtime,$ctime,$blksize,$blocks)
                 = stat($listfile);


    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
                                                      localtime($mtime);

    $mon = $mon + 1;
    
    $date = "$mon/$mday/$year";

    print "<i>Last Updated: $date</i>\n<HR>\n";

    open (TEAMLIST, "$listfile");
    print <TEAMLIST>;
    close(TEAMLIST);
}

sub print_success {
    print <<EOM;

<P> 
Your entry has been successfully added to the list. If you wish
your entry to be removed or changed, please send mail to
<tt><a href="mailto:50k-online\@mit.edu">50k-online\@mit.edu</a></tt>.

<p>
Please note that all entries are subject to editing or removal by
the Teambuilders List staff.

<p>
<a href="http://web.mit.edu/50k/www/">Back to MIT Entrepreneurship Competition Home</a>

EOM

}

sub check_entry {
    my($query) = @_;
    my($bad);

    $bad = 0;

    if ($query->param('name') !~ /\S+/) {
	$bad = 1;
	print "<p><b>Error</b>: Name field is blank\n";
    }

    if ($query->param('email') !~ /\S+/) {
	$bad = 1;
	print "<p><b>Error</b>: E-mail field is blank\n";
    }
    elsif ($query->param('email') !~ /\@/) {
	$bad = 1;
	print "<p><b>Error</b>: E-mail field doesn't have \@ sign.\n";
    }

    if ($query->param('home_phone') !~ /\S+/) {
	$bad = 1;
	print "<p><b>Error</b>: Home Phone field is blank\n";
    }

    if ($query->param('class') !~ /\S+/) {
	$bad = 1;
	print "<p><b>Error</b>: Class Affiliation field is blank\n";
    }

    $bad;    # return whether or not entry is good
}

sub add_entry {
    my($query) = @_;
    my($email);

    # This opens the teamlist and appends to it

    open (TEAMLIST, ">>$listfile");

    select TEAMLIST;

    $email = $query->param('email');

    print  "<P><DL>\n";
    print  "<DT><B>Name:</B>\n";
    print  $query->param('name');
    print  "\n<DT><B>E-mail:</B>\n<A HREF=\"mailto:$email\">$email</A>\n";
    print  "<DT><B>Home Phone:</B>\n";
    print  $query->param('home_phone');
    print  "\n<DT><B>Work Phone:</B>\n";
    print  $query->param('work_phone');
    print  "\n<DT><B>Class:</B>\n";
    print  $query->param('class');
    print  "<DT><B>Skills:</B>\n";
#    print  "<BLOCKQUOTE>";
    print  $query->param('skills');
#    print  "</BLOCKQUOTE>\n";
    print  "<DT><B>Projects:</B>\n";
#    print  "<BLOCKQUOTE>";
    print  $query->param('projects');
#    print  "</BLOCKQUOTE>\n";
    print  "</DL><HR>\n";

    select STDOUT;

    close(TEAMLIST);

}

sub print_tail {
    print <<END;

<h5><i>
&copy 1998, 1999 The MIT Entrepreuneurship Competition
<br>
Contact the Webmasters at
<a href="mailto:50k-online\@mit.edu">
<tt>50k-online\@mit.edu</tt>
</a>
with any questions or comments.
</i></h5>

END
}

sub subscribe_to_list {
    my($query) = @_;

    # open the process
    open(MAIL, "|-") || exec($mail_prog);

    $address = $query->param('email');
    
    print MAIL 
"To: $mail_sendto
Subject: WWW Teambuilding subscription
Reply-To: $mail_replyto

SUBSCRIBE $address\n
";
}
