#! /usr/bin/perl
#
# process vsfs form results

# redirect stderr to /dev/null and make output unbuffered
#
open(STDERR, ">/dev/null") or &fail("Can't redirect STDERR: $!"); $| = 1;

# Get the input
#
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
#
@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;
  $value =~ s/\s+//g;
  $data{$name} = $value; 
}

# send results to the checker
#
open( PROC,"| /os/prog/vsfs/post.sh") || &fail("post.sh: $!");
keys %data; # reset
while( my($k,$v) = each %data) { print PROC "U $k $v\n"; }
close( PROC);
exit;

# HTML
#
sub html_header {
    $title = $_[0];
    print "Content-type: text/html\n\n<html>\n<head>\n<title>$title</title>\n";
    print "</head>\n<body>\n<h1>$title</h1>\n<p>\n";
}

sub html_trailer {
    print "</body>\n</html>\n";
}

# failed to open a required file
#
sub fail {
    &html_header("Open Failure");
    print "Failed to open $_[0]\n";
    &html_trailer;
    exit;
}