#!/usr/local/perl5/bin/perl

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

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

$listfile = "/u2/graphics/WWW/classes/6.837/F97/FinalProject/6837-teamlist.html";
$maintainer = "jslevene\@graphics.lcs.mit.edu";

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

use CGI;

# Begin Program

# Make new CGI object

$query = new CGI;

# Print out header stuff

print $query->header;
print $query->start_html(
	-title=>'6.837 Team Building List',
	-author=>'pmccormi@mit.edu',
	-BGCOLOR=>'#FFFFFF');

print "<H1>MIT 6.837 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);

	&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 want to join up with 
someone to make a team. You don't have to have an idea.

<p>Fill out what you have to offer in "Skills", and what
you're interested in under "Projects". Keep it short.

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

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

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

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

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

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


    ($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:$maintainer">$maintainer</a></tt>.

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

<p>
<a href="http://graphics.lcs.mit.edu/classes/6.837/F97/homepage.html">
Back to 6.837 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";
    }

    $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>Skills:</B>\n";
    print  $query->param('skills');
    print  "<DT><B>Projects:</B>\n";
    print  $query->param('projects');
    print  "</DL><HR>\n";

    select STDOUT;

    close(TEAMLIST);

}

sub print_tail {
    print <<END;

<h5><i>
<a href="mailto:jslevene\@graphics.lcs.mit.edu">Patrick McCormick</a>
wrote this script.<BR>
Contact the maintainer at
<a href="mailto:$maintainer">
<tt>$maintainer</tt>
</a>
with any questions or comments.
</i></h5>

END
}



