#!/usr/bin/perl

use CGI qw/:standard :html3/;

require ".commonFunctions";

#print header, start_html, "help", end_html;
#exit;

print_form()    unless param;
print_results() if param;

sub print_form {
 print &makeHeader('-title' => 'Submit a file to the DM Archive');
# print header; print start_html('Submit a file to the DM Archive');
  print &makeToolbar("submit");
  print h1('Submit a file to the DM Archive');
    print start_multipart_form(),
#       br,
#       "Contact email address", br,
#       textfield(-name=>'email',-size=>60), br, hr,
       "Author(s) [Last name first. Multiple authors delimited by semicolon, e.g.: Halle, Morris; Marantz, Alec]", br,
       textfield(-name=>'author',-size=>60), br, hr,
       "Title of the paper", br
       textfield(-name=>'title',-size=>60), br, hr,
       "Citation Year", br
       textfield(-name=>'year',-size=>60), br, hr,
       "What file do you want to upload from your computer?", br,
       filefield(-name=>'upload',-size=>60), br, hr,
       "What should this file be named on the archive (e.g., \"paper.pdf\")?", br,
       textfield(-name=>'name',-size=>60), br, hr,
       "Abstract", br
       textarea(-name=>'abstract',-columns=>50, -rows=>10), br, hr,
       "Keywords (optional)", br,
       textfield(-name=>'keywords',-size=>60), br, hr,
       "<br><font color=\"red\">Clicking on the following link will 
cause your broswer to attempt uploading the specified file.</font><br>",
       submit(-label=>'I certify that uploading this file does not infringe copyright'),
       end_form;
    print end_html;
}

sub print_results {
    my $length;
#    my $email = param('email');
    my $author = &EscapeMeta(param('author'));
    my $title = &EscapeMeta(param('title'));
    my $year = &EscapeMeta(param('year'));
    my $file = upload('upload');
    my $name = &EscapeMeta(param('name'));
    my $abstract = &EscapeMeta(param('abstract'));
    my $keywords = &EscapeMeta(param('keywords'));

    $abstract = &EscapeReturns($abstract);
    if (!$file) {
        print header;
        print start_html('No file uploaded!'),
	print h2("Sorry, no information was uploaded.  Did you get the path to the file on your computer correct?  Do you have read access to that file?"),
        print end_html;
	die;
    }
    if ($name) {$filename = $name} else {$filename = $file;}

    unless(open(FILE, ".counter")) {  print header;
        print start_html('Error!');
        print h2("Can't increment DMA #!");
        return;
    }
    $counter = <FILE>;
    close(FILE);
    unlink ".counter";

    unless(open(FILE, ">.counter")) {  print header;
        print start_html('Error!');
        print h2("Can't increment DMA #! (close)$counter");
        return;
    }
    $counter = $counter+1;
    print FILE $counter;
    close(FILE);

#     $counter = "test2";
#    $counter = substr(`echo $password | md5sum | uuencode -m dummy | grep -v begin`, 1, 8);
    unless (mkdir("/com/web/docs/projects/dm/archive/$counter", 777)) {
        print header;
        print start_html('Error!');
        print h2("Can't create new counter. n$counter$password");
        return;
    }
    chmod (0777, "/com/web/docs/projects/dm/archive/$counter");
    unless (open(FILE, ">/com/web/docs/projects/dm/archive/$counter/$filename")) {
        print header;
        print start_html('No file uploaded!'),
	print h2("Can't open $filename: $!"),
        print end_html;
	return;
    }
    while (<$file>) {
        print FILE;
    }
    close(FILE);

    $filesize = `./filesize.cgi /com/web/docs/projects/dm/archive/$counter/$filename`;

    unless (open(INFO, ">/com/web/docs/projects/dm/archive/$counter/info.txt")) {
        print header;
        print start_html('Error!');
	print h2("Can't open info file: $!");
	return;
    }

    $time = `./date.cgi`;
    $timestamp = `./timestamp.cgi`;

    print INFO "Author(s): $author\n";
    print INFO "Title: $title\n";
    print INFO "Filename: $filename\n";
    print INFO "Citation Year: $year\n";
    if ($keywords) { print INFO "Keywords: ", $keywords, "\n"; }
    print INFO "Date submitted: ", $time, "\n";
    print INFO "Timestamp: ", $timestamp, "\n";
    print INFO "File size: $filesize\n";
    print INFO "Abstract: $abstract";
    close(INFO);

    unless (open(INDEX, ">/com/web/docs/projects/dm/archive/$counter/index.html")) {
        print header;
        print start_html('Error!');
	print h2("Can't open info file: $!");
	return;
    }

    unless (open(EDIT, ">/com/web/docs/projects/dm/archive/$counter/.editable")) {
        print header;
        print start_html('Error!');
        print h2("Can't open info file: $!");
        return;
    }
    close(EDIT);

    print INDEX qq!<head><meta HTTP-EQUIV="refresh" CONTENT="0;URL=http://www.ai.mit.edu/projects/dm/archive/cgi-bin/view.cgi?file=$counter"></head>!;
    close(INDEX);

    
#    system("echo | /usr/bin/mutt -s $password root\@semanticsarchive.net");

    print redirect("http://www.ai.mit.edu/projects/dm/archive/$counter");
}

sub EscapeMeta {
  my $bad_chars = qq!$<>#%^\\!;
    my $str = $_[0];
#  return $str;
#  $_[0] = HTML::Entities::decode($_[0]);
  $str =~ s/[\$<>#%\^\\]//go;
  return $str;
}

sub EscapeReturns {
  my $abs = $_[0];
  $abs =~ s/\n/ /gs;
  return $abs;
}
