#!/usr/local/bin/perl

# send output to this address
$MAILTO = "ullman\@engr.orst.edu";

# path of form on www.engr.orst.edu
$formpath="http://www.engr.orst.edu/~ullman/concurrent1.html";

# info from from
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

open(MAILCMD,"|/bin/mail $MAILTO");
@pairs = split(/&/, $buffer);
#set up return mail for sender
($SENDER_NAME, $RETURN_ADDR) = split(/=/, $pairs[1]);
$RETURN_ADDR =~ tr/+/ /;
$RETURN_ADDR =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
open(MAILRTN,"|/bin/mail $RETURN_ADDR");
print MAILRTN "This mail was automatically generated by your concurrent engineering\n";
print MAILRTN "assessment submission.\n\n";
print MAILRTN "Your submissions are listed below in the following format:\n\n";
print MAILRTN "\tCategory Number_Sub Category Letter_Sub Category Level=Your Response\n";
print MAILRTN "\tFor Example:\n\n";
print MAILRTN "\t\tI_A_Task=Seldom\n\n";
print MAILRTN "\t\tis what you should see if you answered \"Seldom\" to the\n";
print MAILRTN "\t\tTask sub-category of section I A,  Team Integration\n\n";
print MAILRTN "Please direct questions about this mail to $MAILTO\n\n";
print MAILRTN "YOUR SUBMISSIONS:\n\n";

foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $name =~ tr/+/ /;
    $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    print MAILCMD "$name = $value\n";
    print MAILRTN "$name = $value\n";
}

close(MAILRTN);
close(MAILCMD);

# print header
printf ("Content-type: text/html%c",10);
printf ("%c",10);
printf ("<html><body>\n");

# print out response form
printf ("Your message was sent to $MAILTO<br>");
printf ("<a href=\"$formpath\">Go back to concurrent engineering page.</a>\n");
printf ("</body></html>");
