#!/usr/local/bin/perl

select(STDOUT) ;
$| = 1 ;


&new_get_args ;

$arch       = $FORM{'architecture'} ;
$section    = $FORM{'section'} ;
$topic      = $FORM{'topic'} ;
$filter     = "/usr/local/etc/httpd/htbin-post/man2html" ;
$fflags     = "-title $topic -cgiurl http://hawkwind/htbin-post/manlink/architecture=$arch/section=\\\$section\\\$subsection/topic=\\\$title" ;
$manflags   = "" ;
$mancommand = "" ;


if ($section =~ /Keyword/) {
    $manflags = "-k" ;
    $fflags  = " -k " ;
}
elsif ($section =~ /Section (\d\w?)/) {
    $manflags = $1 ;
}

if ($arch eq "HP") {
    $fflags .= " -leftm 1 -topm 8";
    $mancommand   = "man $manflags $topic";
}
else {
    # must be a CONVEX for now
    # this relies on the server's ability to remsh to pixel and do a man
    # this will/would break whenever we switch over
    $mancommand = "remsh imagine man $manflags $topic" ;
}

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

open (MAN, "$mancommand 2> /dev/null |") || exit 1 ;
open (FILTER, "| $filter $fflags >/tmp/foo 2> /dev/null") || exit 1 ;

if (eof(MAN)) {
    print "<HTML>\n" ;
    print "<HEAD>\n" ;
    print "<TITLE>Man failed</TITLE>\n" ;
    print "</HEAD>\n" ;
    print "<BODY>\n" ;
    print "<H1>man failed</H1>\n" ;
    print "This probably indicates that the link that you clicked on does\n" ;
    print "not exist as a man page.  Sorry.  It is an imperfect world.\n" ;
    print "I can't stand it.  Sob!!!!<P>\n" ;
    print "<P>\n" ;
    close MAN ;
    close FILTER ;
    exit 1 ;
}


while (<MAN>) {
    print FILTER ;
}

close FILTER ;
close MAN ;

open (OUT, "/tmp/foo") || exit 1 ;
while (<OUT>) { 
  print ;
}
close OUT ;


sub new_get_args {
    @args = split('/',$ARGV[0]);
    foreach (@args) {
      tr/+/ / ;
      ($name, $value) = split(/=/, $_);
      $FORM{$name} = $value ;
    }
}



sub future_get_args {

   if ($ENV{'REQUEST_METHOD'} eq 'POST')
   {                          
       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;

           print "Setting $name to $value<P>";

           $FORM{$name} = $value;
       }
   }
}

