#! /bin/sh

#================================================================
# extfxmantohtml
# Convert man (troff-andoc) into HTML.
#================================================================


# set variables
PATH="$PATH:/usr/local/bin:$HOME/bin:." ; export PATH
MANWIDTH=262144 ; export MANWIDTH
progname="estfxmantohtml"


# check arguments
if [ $# -lt 1 ]
then
  printf '%s: usage: %s infile [outfile]\n' "$progname" "$progname" 1>&2
  exit 1
fi
infile="$1"
outfile="$2"
if [ -n "$ESTORIGFILE" ] && [ -f "$ESTORIGFILE" ]
then
  infile="$ESTORIGFILE"
fi


# check the input
if [ "!" -f "$infile" ]
then
  printf '%s: %s: no such file\n' "$progname" "$infile" 1>&2
  exit 1
fi


# initialize the output file
if [ -n "$outfile" ]
then
  rm -f "$outfile"
fi


# function to output
output(){
  if [ -n "$outfile" ]
  then
    cat >> "$outfile"
  else
    cat
  fi
}


# limit the resource
ulimit -v 262144 -t 10 2> "/dev/null"


# output the result (it's cheap hack for formatting...)
case "$infile" in
*.gz)
	zcat "$infile" 2> "/dev/null" | nkf -w | man2html 2> "/dev/null" | \
	    sed -e '1,2d' -e '/Return to Main/d' -e '/\<HR\>/,/Time: /d' | \
	    output
	;;
*)
	nkf -w "$infile" | man2html | \
	    sed -e '1,2d' -e '/Return to Main/d' -e '/\<HR\>/,/Time: /d' | \
	    output
	;;
esac
#zcat "$infile" 2> "/dev/null" | nkf -w | output

# exit normally
exit 0



# END OF FILE
