#!/usr/bin/perl
#
# $Source: /local/web/PSESCAPE-97/perl/RCS/submit-paper.pl,v $
# $Id: submit-paper.pl,v 1.3 1997/05/27 11:43:38 steinhau Exp steinhau $

#
# This is the WWW-interface to paper submission
#
# Author: Steinar Hauan <steinhau@pvv.org>
#

$ENV{'PATH'}    = '/bin:/usr/bin';
$ENV{'IFS'}     = '';
$ENV{'SHELL'}   = '/bin/sh';
	    
unshift (@INC,'/local/web/PSESCAPE-97/perl');

require "cgi.pl";
require "defines.pl";
require "utils.pl";


$THIS_DOC_TYPE = $DOC_TYPES[0];
$THIS_PROG     = "/BIN/PSE:submit-paper";
$FORM_ACTION   = "<FORM ACTION=\"$THIS_PROG\" METHOD=get>\n";

#
# this script is recursive, on 3 levels:
#
#  lvl 1: autentication and selection of ftp file
#  lvl 2: author information/changes
#  lvl 3: logical checks, acceptance
#

if ( ! defined %args )
{
    &PaperInfo;
} else {
    if ( $args{'ptype'} eq '' )
    {
	if ( &FindReference )
	{
	    &GetPaperData;
	}
    } else {
	if ( &FindReference )
	{
	    &AcceptSubmission;
	}
    }
}

exit 0;

################################################################
#    S U B R O U T I N E S
################################################################

sub PaperInfo
{
    &WriteComplexHead(STDOUT,
		      'PSESCAPE-97: paper submission form - part 1',
		      'CENTER',
		      'H1','PSESCAPE-97: paper submission form - part 1');

    print "$FORM_ACTION\n";

    print "<H2>Identify your submission</H2>\n";
    print "Select <B>your file</B> from the following menu: \n";
    print "<SELECT name=psfile>\n";
    foreach $fil (`ls -1t $FTP_ROOT_DIR`)
    {
        chop($fil);
        next unless -f "$FTP_ROOT_DIR/$fil";
        print "<OPTION>$fil\n";
    }
    print "</SELECT> (select by clicking)<P></CENTER>\n";

    print "Reference number:<INPUT TYPE=text NAME=gref SIZE=4><P>\n";
    print "LAST name of corresponding author: ";
    print "<INPUT TYPE=text NAME=contact SIZE=20><P>\n";

    print "<INPUT TYPE=submit VALUE=Proceed> to presentation information\n";
    print "</FORM>\n";
    &WriteAdr;
}

sub FindReference
{

    local($gref)    = $args{'gref'};
    local($contact) = "";

    ($contact = $args{'contact'}) =~ y/A-Z/a-z/;

    local($dir)  = &Num2Dir($gref);
    local($ctrl) = sprintf("%s/abstract.ps\@control",$dir);
    
    undef @errors;

    if ( $args{'gref'} eq '' )
    {
	push(@errors,"No reference number specified");
    }
    if ( $args{'contact'} eq '' )
    {
	push(@errors,"No corresponding author specified");
    }
    if ( ! defined @errors )
    {
	if ( ! -d $dir )
	{
	    push(@errors,"The reference '$gref' does not exist");
	} else {
	    if ( -f "$dir/IGNORE" )
	    {
		push(@errors,"The reference '$gref' is invalid\n");
	    } else {
		if ( -f "$ctrl" )
		{
		    # these variables are INTENTIONALLY made global
		    # to avoid multiple readings of configuration file
		    ($err,%data) = &ReadConfig($ctrl);
		    
		    local($lname) = "";
		    ($lname = $data{'lastname'}) =~ y/A-Z/a-z/;
		    if ( "$lname" ne "$contact" )
		    {
			push(@errors,"Incorrect last name of corresponding author");
			push(@errors,"You entered: '$args{'contact'}' (case NOT important)");
		    }
		    if ( "$data{'review'}" eq "reject" )
		    {
			push(@errors,"Sorry - this abstract was NOT accepted by the IPC");
		    }
		} else {
		    push(@errors,"Internal error: missing control file");
		}
	    }
	}
    }

    if ( defined @errors )
    {
	&HeaderError;
	for $err (@errors){ print "$err<P>\n"; }
	&BotError;
	return 0;
    }
    if ( -f "$dir/paper.ps" )
    {
	&HeaderError;
	print "<CENTER>";
	print "<H1>Reference $args{'gref'} is already submitted</H1>\n";
	print "To ensure consistency, you may only submit <B>once</B> using this interface.<P>";
	print "For updates/changes in the paper, please send a mail to $EMAIL_ADMIN\n";
	print "</CENTER>";
	&BotError;
	return 0;
    }
    if ( "$args{'psfile'}" eq "README" )
    {
	print "<CENTER>";
	print "<H2>You have selected the 'README' file</H2>";
	print "</CENTER><PRE>";
	open(FD,"$FTP_ROOT_DIR/README") || printf "(EMPTY FILE)\n";
	while (<FD>){ print $_; }
	close(FD);
	print "------------------- END OF FILE --------------------\n";
	print "</PRE><CENTER>";
	print "<H4>Please return to the <A HREF=\"/PSESCAPE-97/papers.html\">";
	print "paper information page</A> and read the instructions again.</H4>";
	print "</CENTER>";
	&WriteAdr;
	return 0;
    }
    if ( -f "$FTP_ROOT_DIR/.$args{'psfile'}.lock" )
    {
        print "<CENTER>";
        print "<H2>You have selected the '$args{'psfile'}' file</H2>";
        print "</CENTER>";
        print "This file is 'locked', meaning someone (probably you?) ";
        print "already completed an electronic cover-letter for this file.";
        print "The file will be moved into our spooling system within 20 minutes.";
	print "<P><CENTER>";
	print "<B>Submission changes/updates/questions should be mailed separately</B><P>\n";
	print "Our email adress is: $EMAIL_ADMIN\n";
        print "</CENTER><P>";
        &WriteAdr;
        return 0;
    }
    return 1;
}

sub GetPaperData
{

    local($gref)    = $args{'gref'};

    # the variables $err and %data are global and read in FindReference
    local($session) = "SESSION: $data{'session'}";
    local($refstr)  = "Reference number: $gref";

    local($warn)  = "";
    local($fname) = "$FTP_ROOT_DIR/$args{'psfile'}";
    local($ftype) = &IdentifyFile($fname);

    if ( ! &AcceptFile($fname) )
    {
        $warn = "<HR><BLINK><B>WARNING: Filetype `$ftype` identified</B></BLINK>";
    }

    &WriteComplexHead(STDOUT,
		      'PSESCAPE-97: paper submission form - part 2',
		      'CENTER',
		      'H1','PSESCAPE-97: paper submission form - part 2',
		      'H2',$session,
		      'H2',$refstr,
		      '',$warn);

    print "$FORM_ACTION\n";

    # transfer data previously entered
    print "<INPUT TYPE=hidden NAME=contact VALUE=$args{'contact'}>\n";
    print "<INPUT TYPE=hidden NAME=gref VALUE=$args{'gref'}>\n";
    print "<INPUT TYPE=hidden NAME=psfile VALUE=$args{'psfile'}>\n";

    print "Select preferred form of presentation";
    print "<SELECT NAME=ptype>\n";

    local(@ptypes) = ( 'oral','poster');
    for $opt (@ptypes)
    {
	print "<OPTION>$opt\n";
    }
    print "</SELECT> (select from menu by clicking)<P>\n";

    print "Input final paper title:\n";
    print "<CENTER><INPUT TYPE=text NAME=title SIZE=60></CENTER><P>\n";
    
    print "Enter names of <B>ALL</B> authors in the same order as they appear on the paper:<P>\n";
    print "<CENTER><INPUT TYPE=text NAME=authors SIZE=60></CENTER><P>\n";
    print "<HR>\n";

    local($adr) = &FormatAdr($data{'adress'});

    print "<H2>Contact information</H2>\n";
    print "<PRE>\n";
    print "$data{'firstname'} $data{'lastname'}\n\n";
    print "email: $data{'email'}\n";
    print "phone/fax: $data{'phone'} / $data{'fax'}\n";
    print "\n";
    print "$adr";
    print "</PRE>\n";
    
    print "Comments/changes in contact information:<P>\n";
    print "<TEXTAREA TYPE=TEXT NAME=comment ROWS=4 COLS=60></TEXTAREA><P>\n";
    print "<HR>\n";

    print "<INPUT TYPE=submit VALUE=Send> information\n";
    print "</FORM>\n";
    &WriteAdr;
}

sub AcceptSubmission
{
    undef @errors;

    if ( $args{'authors'} eq '' )
    {
	push(@errors,"Author namelist missing");
    }
    if ( $args{'title'} eq '' )
    {
	push(@errors,"No paper title given");
    }
    
    # clean up comment field - remove newlines and double white-space
    $args{'comment'} =~ s/\\n/ /g;
    $args{'comment'} =~ s/(\s+)/ /g;
    if ( $args{'comment'} eq '' ){ $args{'comment'} = "NONE"; }


    if ( defined @errors )
    {
	&HeaderError;
	print "<H2>The following errors were found</H2>\n";
	for $err (@errors){ print "$err<P>\n"; }
	&BotError;
	return 0
    }
    
    &WriteComplexHead(STDOUT,
		      'PSESCAPE-97: paper submission form - part 3',
		      'CENTER',
		      'H1','PSESCAPE-97: paper submission form - part 3');
    
    # open control file
    $cfname = "$CONTROL_DIR/$args{'gref'}.$$";
    if ( ! open(CTRL,"> $cfname") )
    {
        print "Internal error creating control-file ($!)<P>\n";
        print "Please notify $EMAIL_ADMIN\n";
        return;
    }
    # create submission lockfile
    system("touch $FTP_ROOT_DIR/.$args{'psfile'}.lock");

    # write control file data
    for $tag (sort @PAPER_TAGS)
    {
	if ( $args{$tag} ne '' )    { print CTRL "$tag: $args{$tag}\n"; }
	elsif ( $data{$tag} ne '' ) { print CTRL "$tag: $data{$tag}\n"; }
	else                        { print CTRL "$tag: NONE\n"; }
    }
    close(CTRL);

    # give user feedback
    print "<H2>SUBMISSION ACCEPTED</H2>\n";
    print "The following data has been stored:\n";
    print "<DL>\n";
    print "  <DD><B>TITLE:</B> $args{'title'}\n";
    print "  <DD><B>REF:</B> $args{'gref'}\n";
    print "  <DD><B>AUTHOR:</B> $data{'lastname'}, $data{'firstname'}\n";
    print "</DL>\n";
    print "A request for <B>$args{'ptype'} presentation</B> has been noted.<P>\n";

    print "An automatic confirmation will be sent to\n";
    if ( $data{'email'} ne 'NONE' )
    {
        print "<B>$data{'email'}</B> by electronic mail.\n";
	$receiver = $data{'email'};
    } else {
        print "<B>faxnumber $data{'fax'}</B>.\n";
	$receiver = sprintf("%s.%s@%s.fax",$data{'firstname'},
			    $data{'lastname'},$data{'fax'});
    }
    print "<P>\n";
 
    print "After manual inspection, you will also receive a human response.";
    print "This will usually take a few days<P>";
    print "<P>\n";

    print "<H5>If for some reason, you do not receive the confirmations, ";
    print "please email $EMAIL_ADMIN</H5>\n";
    print "<P>\n";

    print "<CENTER><B>\n";
    print "N.B. Remember to send the Copyright transfer form separately";
    print "</CENTER></B>\n";
    
    &WriteAdr;

    # send automatic confirmation
    local($header) = "PSESCAPE-97: paper confirmation";
    local($author) = "$data{'firstname'} $data{'lastname'}";

    if ( !open(FD,"| $MAIL -s \"$header\" $receiver") )
    {
	print "<H1>Internal mailer-error ($!), please notify $EMAIL_ADMIN</H1>\n";
	return;
    }
	
    local($mail) = &SubstTemplate("$TEMPLATE_DIR/confirm-paper.template",
				  "FILE",$args{'psfile'},
				  "PAPER_TITLE",$args{'title'},
				  "PAPER_REF",$args{'gref'},
				  "PAPER_AUTHOR",$author);
    print FD "$mail";
    close(FD);
}


sub WriteHeader
{
    &WriteComplexHead(STDOUT,
                      'PSESCAPE-97: paper submission check',
                      'CENTER',
                      'H1','PSESCAPE-97: paper submission check');
}

sub HeaderError
{
    &WriteHeader;
    print "<H2>INPUT ERROR DETECTED</H2><P>\n";
}

sub BotError
{
    print "<HR>\n";
    print "<H3>Your submission has NOT been accepted - please reenter data</H3>";
    print "<P>\n";
    print "In case of persistant errors, please email $EMAIL_ADMIN\n";
    &WriteAdr;
}
