#! /bin/sh

#================================================================
# estmanhtml
# Strip a file of UNIX manual and extract its text as HTML.
#================================================================


# set variables
LANG=C ; export LANG
LC_ALL=C ; export LC_ALL
progname="estmanhtml"
[ -z "$ESTORIG" ] && ESTORIG="$1"
tmpfile="/tmp/$progname-$$.xdw"
nulldev="/dev/null"
infile="$1"
outfile="$2"


# show help message
if [ "$1" = "--help" ]
then
  printf 'Strip a file of UNIX manual and extract its text as HTML.\n'
  printf '\n'
  printf 'Usage:\n'
  printf '  estmanhtml [infile] [outfile]\n'
  printf '  estindex register -xsuf .1,.2,.3,.4,.5,.6,.7,.8 \\\n'
  printf '      application/x-troff-man estdochtml casket\n'
  printf '\n'
  exit 0
fi


# function to remove the temporary file
tmpclean(){
  rm -rf "$tmpfile"
}


# function to create the temporary file for input
output(){
  if [ -z "$outfile" ]
  then
    cat
  else
    cat >> "$outfile"
  fi
}


# set the exit trap
trap tmpclean 1 2 3 13 15


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


# create the temporary file
if [ -z "$infile" ]
then
  cat > "$tmpfile"
  infile="$tmpfile"
fi


# output the result
if printf '%s' "$ESTORIG" | grep -i '\.gz$' > "$nulldev"
then
  gzip -cd "$infile"
elif printf '%s' "$ESTORIG" | grep -i '\.bz2$' > "$nulldev"
then
  bzip2 -cd "$infile"
else
  cat "$infile"
fi |
groff -e -g -mandoc -Thtml 2> "$nulldev" |
grep -v '^<a  *href="#' |
output


# clean up the temporary directory
tmpclean


# exit normally
exit 0



# END OF FILE
