#!/s/perl/bin/perl

# Parse forms data
sub ReadParse {
  if (@_) {
    local (*in) = @_;
  }

  local ($i, $loc, $key, $val);

  # Read in text
  if ($ENV{'REQUEST_METHOD'} eq "GET") {
    return 0;
  } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
    read(STDIN, $in, $ENV{'CONTENT_LENGTH'});
  } 

  @in = split(/&/,$in);

  foreach $i (0 .. $#in) {
    # Convert plus's to spaces
    $in[$i] =~ s/\+/ /g;

    # Convert %XX from hex numbers to alphanumeric
    $in[$i] =~ s/%(..)/pack("c",hex($1))/ge;

    # Split into key and value.
    ($key, $val) = split ('=', $in[$i]);

    $in{$key} .= '\0' if (defined($in{$key})); # \0 is the multiple separator
    $in{$key} .= $val;
  }

  return 1;
}

$| = 1;

$qs = $ENV{QUERY_STRING};
$pi = $ENV{PATH_INFO};

print "Content-type: text/plain\n\n";

if (! $qs) {
  &ReadParse;
  if ($in{Name}) {
    $qs = "$in{Name}";
  } else {
    $qs = 'timc';
  }
  $opt = "-$in{Opt}m" if ($in{Opt} =~ /^[lms]$/);
} else {
  if ($pi =~ m?^/([lms]*)$?) {
    $opt = "-$1";
  }
}
if ($opt) {
  exec('/s/std/bin/finger', $opt, $qs);
}
else {
  $opt = "-m";
  exec('/s/std/bin/finger', $opt, "$qs");
}
