#!/v/openpkg/sw/bin/perl
##
##  contribution.cgi
##

#   requirements
require 5;
use IO::All;
use CGI;
use CGI::GuruMeditation (-name => "OpenPKG tracker", -debug => 1);
use String::Divert;
use IO::File;
use Date::Format;
use HTML::Entities;
use strict;
use warnings;

#   program information
my $progname = "contribution";
my $progvers = "0.1.0";

#   establish CGI query object
my $cgi = new CGI;

#   establish HTML output object
my $html = new String::Divert;
$html->folder('{#%s#}', '\{#([a-zA-Z_][a-zA-Z0-9_-]*)#\}');
$html->overload(1);
my $io = new IO::File "<contribution.html" or die;
my $canvas = ''; $canvas .= $_ while (<$io>);
$io->close();
$canvas =~ s/<!-- HEAD -->/$html->folder("head")/sie;
$canvas =~ s/<!-- SIDE -->/$html->folder("side")/sie;
$canvas =~ s/<!-- BODY -->/$html->folder("body")/sie;
$html->append($canvas);

#   provide inner HTML canvas
$html->divert("head");
$html .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"contribution.css\"/>\n";
$html->undivert();
$html->divert("body");
$html .= "<div class=\"contribution\">\n";
$html->fold();
$html .= "</div>\n";
$html->divert();

#   determine parameters
my $name      = $cgi->param("name") || '';
my $email     = $cgi->param("email") || '';
my $entity    = $cgi->param("entity") || '';
my $submit    = $cgi->param("submit") || '';

#   read agreement
my $agreement < io("oca-1.2.txt");
$agreement =~ s/^  //mg;

#   dispatch operation
if ($name eq '' or $email eq '' or $email !~ m/^.+\@(?:[^.]+\.)+[^.]+$/ or $submit eq '') {
    ##
    ##  SHOW FORM
    ##

    $html .= "<div class=\"agreement\">\n";
    $html .= $cgi->start_form(-method => "POST", -action => $cgi->url(-relative => 1));
    $html .= "<span class=\"error\">ERROR: Please see below!</span>" if ($submit ne '');
    $html .= "<pre>\n";
    $agreement =~ s/(http:\/\/[^\s>\)]+)/<a href="$1">$1<\/a>/sg;
    $html .= $agreement;
    $html .= "Signature\n";
    $html .= "---------\n";
    $html .= "\n";
    $html .= "</pre>\n";
    $html .= "<table class=\"signature\">\n";

    $html .= "  <tr><td>Full Name:</td><td>";
    $html .= $cgi->textfield(-name => "name", -value => $name);
    if ($submit ne '' and $name eq '') {
        $html .= "<br/><span class=\"error\">ERROR: Name field not allowed to be empty</span>\n";
    }
    $html .= "  </td></tr>\n";

    $html .= "  <tr><td>Email Address:</td><td>";
    $html .= $cgi->textfield(-name => "email", -value => $email);
    if ($submit ne '' and $email eq '') {
        $html .= "<br/><span class=\"error\">ERROR: Email field not allowed to be empty</span>\n";
    }
    elsif ($submit ne '' and $email !~ m/^.+\@(?:[^.]+\.)+[^.]+$/) {
        $html .= "<br/><span class=\"error\">ERROR: Email field not valid</span>\n";
    }
    $html .= "  </td></tr>\n";

    $html .= "  <tr><td>Entity:</td><td>";
    $html .= $cgi->textfield(-name => "entity", -value => '');
    $html .= " (unless individual person)";
    $html .= "  </td></tr>\n";

    $html .= "  <tr><td>\n";
    $html .= "  </td><td>\n";
    $html .= "<br/>\n";
    $html .= $cgi->submit(-name => "submit", -value => "ACCEDE", -class => 'accede');
    $html .= $cgi->submit(-name => "submit", -value => "RESIGN", -class => 'resign');
    $html .= "  </td></tr>\n";

    $html .= "</table>\n";
    $html .= "</div>\n";
    $html .= $cgi->end_form();
}
else {
    ##
    ##  PROCESS FORM
    ##

    #   generate mail
    my $mail = '';
    $mail .= "From: \"$name\" <$email>\n";
    $mail .= "Reply-To: $email\n";
    $mail .= "Subject: OpenPKG Contributor Agreement: $submit\n";
    $mail .= "To: openpkg-contribution\@openpkg.org\n";
    $mail .= "Cc: $email\n";
    $mail .= "\n";
    $mail .= $agreement;
    $mail .= "Signature\n";
    $mail .= "---------\n";
    $mail .= "\n";
    $mail .= "Full Name:     $name\n";
    $mail .= "Email Address: $email\n";
    $entity ||= "(individual)";
    $mail .= "Entity:        $entity\n";
    my $date = time2str("%Y-%m-%d %H:%M:%S", time(), "UTC");
    $mail .= "Date:          $date (UTC)\n";
    $mail .= "Agreement:     $submit\n";
    $mail .= "\n";

    #  send out mail
    open(MAIL, "|/v/openpkg/sw/sbin/sendmail -oi -f '$email' -oee -t") || die;
    print MAIL $mail;   
    close(MAIL);

    if (open(LOG, ">>contribution.log")) {
        my $version = "?.?";
        if ($agreement =~ m/document\s+version\s+(\d+\.\d+)/s) {
            $version = $1;
        }
        printf LOG "%s %s %s %s <%s>\n", $date, $version, $submit, $name, $email;
        close(LOG);
    }

    $html .= "Thanks for your <b>$submit</b> submission of the <b>OpenPKG Contributor Agreement</b>.\n";
    $html .= "The following Email was submitted towards the OpenPKG project.\n";
    $html .= "Expect to receive a response from the OpenPKG Petidomo mailing list manager for confirmation of your Email address.\n";
    $html .= "Once your explicit confirmation is received by the OpenPKG Peitdomo mailing list manager, your submission is finally delivered to the OpenPKG Project.\n";
    $html .= "<p/>\n";
    $html .= "<pre id=\"email\">\n";
    $mail =~ s/\&/&amp;/sg;
    $mail =~ s/</&lt;/sg;
    $mail =~ s/>/&gt;/sg;
    $html .= $mail;
    $html .= "</pre>\n";
}

#   send output 
$html->overload(0);
$html->undivert(0);
$html = $html->string();
print $cgi->header(
    -type           => "text/html",
    -Content_length => length($html),
    -X_User_Agent   => sprintf("%s/%s", $progname, $progvers),
    -Cache_Control  => "max-age=0",
    -expires        => "+0s",
) . $html;

#   die gracefully
exit(0);

