#!/local/bin/perl5
#
# Process an user update that comes as a Web form
#

use FileTable;
use POSIX;
use English;

print STDERR "Uid is $UID euid is $EUID gid is $GID\n";

# Autoresponsible - in some states, the responsible is always the
# same person
%autoresponsible = (
		    "Author", "Author",
		    "WG", "Author",
		    "AD", "Area Directors",
		    "IESG", "IESG",
		    "Last Call Req", "IESG Secretary",
		    "Last Call", "Community",
		    "Writeup", "Area Directors",
		    "Discuss", "IESG member",
		    "Revision", "Author",
		    "RFC Editor", "RFC Editor",
		    "RFC", " ",
		    "Dead", " ");
# the states with multiple possible responsibles are
# Author, WG, Wait, Review, Attention


open(LOG, ">>logs/eventlog");
print LOG scalar(localtime), " update.cgi\n";
# Get the input - if cgi-bin, or argument, if not
if ($ARGV[0]) {
    $buffer = $ARGV[0];
    $debug = 1;
    print STDERR "Debug mode\n";
} elsif ($ENV{"QUERY_STRING"}) {
    $buffer = $ENV{"QUERY_STRING"};
    print STDERR "Query mode - read $buffer\n";
} else {
    read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
    print STDERR "Read $buffer\n";
}
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    print STDERR "Setting $name to $value\n" if $debug;
    $entry{$name} = $value;
}
#
# Start off the HTML. Anything that happens is recorded in a comment
#
print "Content-type: text/html\n\n";

print "<HTML><HEAD><TITLE>Apps Area Update Ack</TITLE></HEAD><BODY>\n";
print "<!--\n"; # Comment to hide the trace stuff

for $key (sort(keys(%entry))) {
    print "$key: $entry{$key}\n";
}

# Try to process command
$drafts = FileTable::open("db/drafts", O_RDWR);
$key = $entry{name};
die "No name in data -> no record!\n" if !$key;

# Find action. I don't like this, but it's what works...
if ($entry{"delete"}) {
    print "Action: DELETE\n";
    $draft = $drafts->get($key);
    if ($draft) {
	$draft->clear;
	$draft->store;
	print LOG "Draft $key deleted\n";
    } else {
	print STDERR "Draft not found for $key\n";
	exit 1;
    }
    print <<EoF;
-->
<h1>Entry has been deleted</h1>
EoF
&backpointers;

} elsif ($entry{"enter"}) {
    print "Action: ENTER\n";
    $draft = $drafts->get($key, O_CREAT);
    for $field ($drafts->fields) {
	if ($entry{$field}) {
	    if ($draft->{$field}) {
		if ($draft->{$field} ne $entry{$field}) {
		    my $uf = $draft->{$field};
		    print "Changing $field from $uf to $entry{$field}\n";
		}
	    } else {
		print "Adding $field: $entry{$field}\n";
	    }
	} elsif ($draft->{$field}) {
	    print "Deleting $field\n";
	}
	$draft->{$field} = $entry{$field};
    }
    # Clean up the record
    for $field ($draft->fields) {
	if ($draft->{$field} eq "") {
	    delete $draft->{$field};
	}
    }
    # Make "Responsible" consistent with "Status" so that
    # I don't have to bother with both
    if ($autoresponsible{$draft->{status}}) {
	$draft->{responsible} = $autoresponsible{$draft->{status}};
    }
    addadmin($draft);
    print LOG "Draft $key updated by $ENV{REMOTE_HOST}\n";
    $draft->store;
    print "-->\n"; # End comment around script trace    

    print <<EoF;
<h1>Your record is updated</h1>
<p>
EoF
    &backpointers;
} # end of ENTER branch of update
# Since this is quick, just execute the update script
# eeh...this takes 20 seconds on this slow box. Better not.
#system("./listdrafts > www/draft-list.html &");

exit 0;

# Add admin fields - recdate and method - to a record
sub addadmin {
    my $record = shift;

    $record->{recdate} = scalar(localtime);
    $record->{method} = "www";
    $record->{ident} = "script=$0 ip=$ENV{REMOTE_ADDR} host=$ENV{REMOTE_HOST} user=$ENV{REMOTE_IDENT}";
}

    
   
sub backpointers {
    print <<EoF;
<a href="/draft-list.html"><img src="/gifs/shark-gull.gif"> Back to (updated) draft list</a><br>
<a href="/index.html"><img src="/gifs/shark-gull.gif"> Back to Apps main page</a><br>
<form method=get action="/cgi-bin/apps/runscript/list.cgi">
List drafts that contain <input name="name" size=20>
</form> 

</body></html>
EoF
}
