#!/bin/sh
# Man-cgi : a Common Gateway Interface which converts the output of
#           the "man" command to HTML
#
VERSION="1.16S"
# Local, Solaris version by David Adams
#
# Author  : Panagiotis J. Christias <christia@theseas.ntua.gr>
#
# Usage   : it goes in the /cgi-bin/ directory. When called without 
#           arguments (http://www-server/cgi-bin/man-cgi) it outputs a
#           "Front Page" with a section list and query form. When called
#           with arguments, the first argument is the topic (e.g. ls)
#           and the second argument (optional) is the section to look in.
#
#           Examples:
#               http://www.ntua.gr/cgi-bin/man-cgi?ls
#               http://www.ntua.gr/cgi-bin/man-cgi?sleep+3
#
# Notice  : Man-cgi uses the following two images, you may want to get them
#           and put them in your w3 server:
#               http://www.ntua.gr/images/doty.gif
#               http://www.ntua.gr/images/pages.gif
#
# History :
#   1.15    Multiple blank lines are supressed.
#   1.14    Man-cgi now recognizes cross-references that
#           contain periods (e.g. foo.foo(3)).
#   1.13    Really fixed problems occuring in pages which used
#           multi-overstrike for emboldening (hopefully).
#   1.12    Fixed problems occuring in pages which used multi-overstrike
#           for emboldening. Now Man-cgi works any number of overstrike.
#   1.11    Fixed problem with hyphenated cross-references.
#   1.10 and earlier, no history at that time...
#
# Last update: Mon Jul 16 2018
#       Version 1.16S release
# Last update: Fri May 5 1995
#              Fri June 2 1995 David Adams
#	Version 1.15S release

### CONFIGURATION SECTION : change the following variables to fit your needs ###

# The HTTPD server may not pass the environment variables to the script.
# You should add your system path here :
PATH=/usr/bin
export PATH

# The same for the man path here :
MANPATH=/local/man:/usr/man:/usr/openwin/man
manpath_ext1=/fs1/local/SUNWspro/man
name_ext1="SUNWspro"
manpath_ext2=/local/uniras/6v3b/man
name_ext2="UNIRAS 6v3b"
manpath_ext3=/local/gino/man
name_ext3="GINO 4.0a"
manpath_ext4=/local/motif/man
name_ext4="Motif 1.2.1e"

# The full URL of the Man-cgi :
MANCGI='http://www.soton.ac.uk/lcgi/man-cgi'
#MANCGI='http://www.soton.ac.uk:8080/Cgi/man-cgi'
#MANCGI='http://www.soton.ac.uk:8080/Cgi/return-cgi'

# The URL of the two images :
DOT=http://www.soton.ac.uk/Icons/balls/smallred.gif
PAGES=http://www.soton.ac.uk/cp/icons/pages.gif

### END OF CONFIGURATION SECTION ###############################################

SECTION=$2
COMMAND=$1
EXT1=`echo $3 | fgrep "1"`
EXT2=`echo $3 | fgrep "2"`
EXT3=`echo $3 | fgrep "3"`
EXT4=`echo $3 | fgrep "4"`

echo "Content-type: text/html"
echo ""

if [ $# -eq 2 ] ; then
  if [ $2 = "00" ] ; then
    read a

    a=`echo $a | sed -e 's/%2[cC]/\,/g' -e 's///' -e 's/\(.*\)$/\1&\'`

    COMMAND=`echo $a | sed -e 's/^command=//' \
		-e 's/&.*$//'`
    SECTION=`echo $a | sed -e 's/^.*&section=//' \
		-e 's/&.*$//'`

    EXT1=`echo $a | fgrep '&EXT1'`
    EXT2=`echo $a | fgrep '&EXT2'`
    EXT3=`echo $a | fgrep '&EXT3'`
    EXT4=`echo $a | fgrep '&EXT4'`
  fi;
fi;

case "$COMMAND" in
*/*) COMMAND=""
     ;;
esac

if [ "$SECTION" = "ANY" ] ; then
  SECTION=""
fi;

EXTRA=""
if [ -n "$EXT1" ] ; then
  MANPATH=${manpath_ext1}:${MANPATH}
  EXTRA=${EXTRA}1
fi;
if [ -n "$EXT2" ] ; then
  MANPATH=${manpath_ext2}:${MANPATH}
  EXTRA=${EXTRA}2
fi;
if [ -n "$EXT3" ] ; then
  MANPATH=${manpath_ext3}:${MANPATH}
  EXTRA=${EXTRA}3
fi;
if [ -n "$EXT4" ] ; then
  MANPATH=${manpath_ext4}:${MANPATH}
  EXTRA=${EXTRA}4
fi;
export MANPATH
if [ -n "$EXTRA" ] ; then
  EXTRA="+${EXTRA}"
fi;

if [ $# -ne 0 ] ; then
  cat <<END
<HTML>
<HEAD>   
<TITLE>UNIX man pages : $COMMAND ($SECTION)</TITLE>
</head>
<BODY>
END

if [ "$SECTION" = "KEY" ] ; then

cat <<END
<h2>Search on the Keyword: "${COMMAND}"</h2>
<hr>
END

man -k $COMMAND 2> /dev/null | \
  sed \
	  -e 's/</«/g' \
	  -e 's/>/»/g' \
	  -e 's/\&/§/g' \
	  -e "s#\(\([0-9A-z][-.,0-9A-z]*\) (\([1-9][A-z]*\))\)#<A HREF=\"$MANCGI\?\2+\3$EXTRA\">\1</A>#g" \
   \
	  -e 's/§/\&amp;/g' \
          -e 's/«/\&lt;/g' \
          -e 's/»/\&gt;/g' | \
awk 'BEGIN { print "<PRE>" }
     /^$/     { if(!blank) { print; blank=1 } }
     /^..*$/  { print; blank=0 }
     END   { print "</PRE>" }'

else

if [ -n "$SECTION" ] ; then
    SECTION="-s $SECTION"
fi;


man $SECTION $COMMAND 2> /dev/null | \
  sed \
          -e '/-$/N
{
s/\([0-9A-z][-,0-9A-z]*\)-\n\(  *\)\([0-9A-z][-,0-9A-z]*([1-9][A-z]*)\)/\1\3\
\2/
}' \
          -e '/-$/N
{
s/\([0-9A-z][-,0-9A-z]*\)-\n\(  *\)\([0-9A-z][-,0-9A-z]*([1-9][A-z]*)\)/\1\3\
\2/
}' \
          -e '/-$/N
{
s/\([0-9A-z][-,0-9A-z]*\)-\n\(  *\)\([0-9A-z][-,0-9A-z]*([1-9][A-z]*)\)/\1\3\
\2/
}' \
          -e '/-$/N
{
s/\([0-9A-z][-,0-9A-z]*\)-\n\(  *\)\([0-9A-z][-,0-9A-z]*([1-9][A-z]*)\)/\1\3\
\2/
}' \
	  -e 's/</«/g' \
	  -e 's/>/»/g' \
	  -e 's/\&/§/g' \
   \
	  -e '/^[A-Z]/s/.//g' \
	  -e 's/^[A-Z][- ,A-Z]*$/<H2>&<\/H2>/' \
   \
	  -e 's/\([^_]\)\([^\1]\)/\2/g' \
	  -e 's/_|/|/g' \
          -e 's/\(.\)\1/\1/g' \
          -e 's/_\(.\)\1/<b><i>\1<\/i><\/b>/g' \
	  -e 's/_\(.\)/<cite>\1<\/cite>/g' \
	  -e 's/.\(.\)/<b>\1<\/b>/g' \
   \
          -e 's#</b><b>.##g' \
          -e 's#</b>.<b>##g' \
          -e 's#.##g' \
   \
	  -e 's/<\/cite><cite>//g' \
	  -e 's/<\/b><b>//g' \
	  -e 's/<\/i><i>//g' \
	  -e 's/<\/i><\/b><b><i>//g' \
   \
	  -e 's/^  \([A-Z][ ,0-9A-z]*\)$/  <b>\1<\/b>/' \
	  -e 's/^   \([A-Z][ ,0-9A-z]*\)$/  <b>\1<\/b>/' \
   \
	  -e "/^   /s#\(\([0-9A-z][-.,0-9A-z]*\)(\([1-9][A-z]*\))\)#<A HREF=\"$MANCGI\?\2+\3$EXTRA\">\1</A>#g" \
	  -e "/^   /s#\(<cite>\([0-9A-z][-.,0-9A-z]*\)</cite>(\([1-9][A-z]\)*)\)#<A HREF=\"$MANCGI\?\2+\3$EXTRA\">\1</A>#g" \
   \
	  -e "s#<b>+<\/b>#<IMG SRC=$DOT>#" \
   \
	  -e 's/§/\&amp;/g' \
          -e 's/«/\&lt;/g' \
          -e 's/»/\&gt;/g' | \
awk 'BEGIN { print "<PRE>" }
     /^$/     { if(!blank) { print; blank=1 } }
     /^..*$/  { print; blank=0 }
     END   { print "</PRE>" ; if( NR < 5) \
	{ print "<hr>"; \
	printf("<B>NOTE:</b> man page for %s may be in the wrong place.\n",PAGE); \
	printf("<A HREF=\"%s?%s+ANY+1234\">Try all sections and all optional pages</a>\n",URL,PAGE); } }' \
     PAGE=$COMMAND URL=$MANCGI

fi;

cat <<END
<hr>
<h3><A href="$MANCGI"><IMG SRC=$PAGES ALT="">
Solaris Man Pages</a></h3>
END

else
  cat <<END
<HTML>
  <HEAD>
  <TITLE>UNIX man pages</TITLE>
  </HEAD>
  <BODY>
  <H1><IMG SRC=$PAGES ALT=""> UNIX ON-LINE Man Pages</H1>
  <hr><P>
  These are the UNIX man pages for the <b>Solaris</b> service, 
  converted to html on the fly by a shell script.
  <P>
  
  Here are the intro pages of each section:
  <P>
  <UL>
  <LI><A HREF="$MANCGI?intro+1">User Commands</A>
  <LI><A HREF="$MANCGI?intro+1m">Maintenance Commands</A>
  <LI><A HREF="$MANCGI?intro+1+1">Sun FORTRAN</A>
  <LI><A HREF="$MANCGI?intro+2">System Calls</A>
  <LI><A HREF="$MANCGI?intro+3">C Library Functions</A>
  <LI><A HREF="$MANCGI?intro+4">File Formats</A>
  <LI><A HREF="$MANCGI?intro+5">Headers, Tables, and Macros</A>
  <LI><A HREF="$MANCGI?intro+6">Games and Demos</A>
  <LI><A HREF="$MANCGI?intro+7">Special Files</A>
  <LI><A HREF="$MANCGI?intro+9">Device Driver Interfaces</A>
  <LI><A HREF="$MANCGI?intro+9E">DDI and DKI Driver Entry Points</A>
  <LI><A HREF="$MANCGI?intro+9F">DDI and DKI Kernel Functions</A>
  <LI><A HREF="$MANCGI?intro+9S">DDI and DKI Data Structures</A>
  </UL>
  <hr>
  You can also request a man page by name and (optionally) by section,<br>
  or perform a keyword search:
  <P>
  <FORM METHOD=POST ACTION=$MANCGI?00+00>
  Set command name :  <INPUT NAME=command SIZE=24>
  <INPUT TYPE=submit VALUE="Fetch man page">
  <P>Select section number or keyword search: <SELECT NAME=section>
  <OPTION Value="KEY"> Keyword
  <OPTION VALUE="ANY" SELECTED> Any Section
  <OPTION VALUE="1,1M,1C,1F,1S,1B" > Sections 1
  <OPTION VALUE="2" > Section 2
  <OPTION VALUE="3,3C,3S,3X,3I,3R,3T,3N,3M,3K,3G,3E,3B" > Sections 3
  <OPTION VALUE="4,4B" > Section 4
  <OPTION VALUE="5" > Section 5
  <OPTION VALUE="6" > Section 6
  <OPTION VALUE="7" > Section 7
  <OPTION VALUE="9F,9S,9E,9" > Sections 9
  <OPTION VALUE="l" > Section l
  <OPTION VALUE="n" > Section n
  </SELECT>
  <P>Optional extra pages:<br>
  <INPUT NAME="EXT1" TYPE="checkbox" VALUE="on"> ${name_ext1}     
  <br>
  <INPUT NAME="EXT2" TYPE="checkbox" VALUE="on"> ${name_ext2}
  <br>
  <INPUT NAME="EXT3" TYPE="checkbox" VALUE="on"> ${name_ext3}
  <br>
  <INPUT NAME="EXT4" TYPE="checkbox" VALUE="on"> ${name_ext4}
  <br>
  <INPUT TYPE="reset" VALUE="Reset">
  </FORM>
END
fi
cat <<END
  <hr>
  <ADDRESS>
  &#169; 1994-2018 <A HREF="http://www.softlab.ntua.gr/~christia/man-cgi.html">Man-cgi $VERSION</A>, Panagiotis Christias &lt;christia@theseas.ntua.gr&gt;
  <br>
  1995 Modified for Solaris 2.3, David Adams, &lt;d.j.adams@soton.ac.uk&gt;
  </ADDRESS>
</body>
</html>
END
