#!/usr/bin/perl
##---------------------------------------------------------------------------##
##  File:
##      mancgi.pl
##  Author:
##      Emanuele Lombardi       lele@mantegna.casaccia.enea.it
##  Description:
##      mancgi is a Perl cgi wich, with man2html4cgi,
##	makes a centralized multi hosts unix MAN utility
##	on a single web browser
##  Usage:
##      http://your.web.server/cgi-bin/mancgi.pl
##
##  Notes:
##      the user running the web-server must have sh as login shell
##      Not any other due to the redirection of std-err made as 2&1
## 
##---------------------------------------------------------------------------##
##  Copyright (C) 1997  Emanuele Lombardi lele@mantegna.casaccia.enea.it
##
##  This program is free software; you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation; either version 2 of the License, or
##  (at your option) any later version.
##  
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##  
##  You should have received a copy of the GNU General Public License
##  along with this program; if not, write to the Free Software
##  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##---------------------------------------------------------------------------##
use CGI;
$query = new CGI;

$man2html="./man2html4cgi";          

     #Path of the script which translates from man to html 
     #man2html4cgi is a sligthtly modified version of man2html (v2.1.0) of 
     #Earl Hood, ehood@convex.com
     #If not an absolute path it is relative to dir /cgi-bin

$remotehosts=" ";
#$remotehosts=([mantegna,cicero,seneca,cray,canaletto,'pippo-due']);  

     #available hosts to get man from.; If it is set to null string the only
     #host will be the one on which the web server is running

$user=" ";                        
#$user="lele";                        
     #username to be used for rsh on remote hosts (if any)
     #(must have .rhosts set for rsh command coming from the user 
     #which belongs the web server from the machine on which it runs)


##On some machine the web server does not set the environment variable $MANPATH.
##In such cases you cannot use tha man command since it would give only the
##pages in /usr/man. If this appens you always need to rsh a user even if the
##host is the same:            rsh -l $user $host man 
##The selected user $user MUST set the MANPATH environment variable
##in its .cshrc (or at least in /etc/csh.cshrc).

$titlecmd="command";
$titlepag="at page";
$titlehost="on host";
$manbutton="man";
$aproposbutton="apropos";
$helpbutton='?';

##### YOU SHOULD HAVE NO NEED TO MODIFY BELOW THIS LINE
##### APART CHANGING THE LOOK OF THE MAN BROWSER

$thiscgi=$query->script_name();      
     #name of this script as seen from the web server

($thishost)=split(/\./,$query->server_name); 
     #name of this host (web server is running on it)

if ($remotehosts == "") {$remotehosts=([$thishost])}

print $query->header;
print $query->start_html(-title=>"mancgi",
			 -author=>'lele@mantegna.casaccia.enea.it');
@names=$query->keywords;
$host1=@names[0];
$command1=@names[1];
$page1=@names[2];
print $query->startform;
print "$titlehost: ";
print $query->popup_menu(-name=>'host',
			 -values=>$remotehosts,
			 -default=>$host1);
print "$titlecmd: ";
print $query->textfield(-name=>'command',
			size=>10,maxlength=>25,default=>$command1);
print " ";
print $query->submit(-name=>'choice',-value=>$manbutton);
print " ";
print $query->submit(-name=>'choice',-value=>$aproposbutton);
print $query->submit(-name=>'choice',-value=>$helpbutton);
print " ";
print "<hr>";
$names="(@names)";
if (&clean_name($names)) {
    if ($host1 ne "") {
	if ($command1 ne "") {
	    &TABLE($host1,$command1,$page1,$titlecmd);
	    $lele="|cat -r|" . $man2html . " -cgiurl " . $thiscgi . "\?$host1+ ";
##COMMENTED OUT SINCE THE WEB SERVER HAS NOT THE MANPATH ENVIRONMENT VARIABLE
##	    if ($host1 eq $thishost) {
##		print `man - $page1 $command1 2>&1 $lele`;
##	    } else {
		print `(rsh -l $user $host1 man - $page1 $command1 2>&1) $lele`;
##	    }
	}
    } 
}
print $query->endform;

if ($query->param) {
    $choice=$query->param('choice');
    $command=$query->param('command');
    $page="";
    $host=$query->param('host');
#    print "$host=HOST";
    if ($command ne "") {
#	print " COMMAND=$command";
	if (&clean_name($command)) {
#	    print " TRE ";
	    ($command, $page)=split(/ /,$command);
	    $lele="|cat -r|" . $man2html . " -cgiurl " . $thiscgi . "\?$host+ ";
#	    print "<br> QUATTRO $host $thishost $lele";
	    if ($choice eq $aproposbutton) {
		&TABLE($host,$command,"","apropos ");
##COMMENTED OUT SINCE THE WEB SERVER HAS NOT THE MANPATH ENVIRONMENT VARIABLE
##		if ($host eq $thishost) {
##		    print `man - -k $command $lele -k`;
##		} else {
		    print `(rsh -l $user $host man - -k $command 2>&1) $lele -k `;
##		}
	    } elsif ($choice eq $helpbutton) {
		&HELP}
	    else {
		&TABLE($host,$command,$page,$titlecmd);
##COMMENTED OUT SINCE THE WEB SERVER HAS NOT THE MANPATH ENVIRONMENT VARIABLE
##		if ($host eq $thishost) {
##		    print `(man - $page $command 2>&1) $lele`;
##		} else {
		    print `(rsh -l $user $host man - $page $command 2>&1) $lele`;
##		}
	    }
	}
    } else {
	if ($choice eq $helpbutton) {&HELP}
    }
} else {&HELP}
print $query->end_html;

sub TABLE {
    print '<CENTER><TABLE ALIGN=ABSCENTER BORDER=1 
               CELLSPACING=10 CELLPADDING=10 WIDTH="100%" HEIGHT="50" >';
    print "\<TR\>\n";
    print "<TD>$titlehost <b>@_[0]<b></TD>\n";
    print "<TD>@_[3] <b>@_[1]</b></TD>\n";
    if (@_[2] ne "") {print "<TD>$titlepag<b> @_[2]</b></TD>\n"}
    print "</TR>\n";
    print "</TABLE></CENTER>\n" ;
}

sub HELP {
    print "<p><center>HOW TO USE THIS MAN BROWSER</center>";
    print "<p><ul><li>select from the left-most pop-up menu the host from which you want man pages";
    print "<li>type in the input field the name of the command you want help about";
    print "<br>(place optional page after cmd) ";
    print "<li>if you want:<ul><li>the man page about the command simply press RETURN or press the <b>$manbutton</b> button";
    print "<li>the apropos (man -k) page about the command press the <b>$aproposbutton</b> button";
    print "</ul></ul>";
    print "<p><center>To see again this help page press the <b>$helpbutton</b> button</center>";
    print '<p><center>bye bye from the author <a href="mailto://lele@mantegna.casaccia.enea.it">(lele)</a></center></b>';

}

sub clean_name {
    local($name) = @_;
    while ($name =~ m/[\;\/\\\[\]]/g ){
	print "<p><center><b>input $titlecmd contains invalid characters</b></center>";
	return "";
    }
    return $name;
}





