#!/usr/bin/ksh
#*******************************************************************************
# E.S.O. - VLT project
#
# "@(#) $Id: docDeroff,v 1.31 2002/06/08 17:20:48 vltsccm Exp $"
#
# who       when      what
# --------  --------  ----------------------------------------------
# psivera  02/02/01  created
# psivera  16/02/01  improved txt output
# psivera  01/03/01  added man page
#

#************************************************************************
#   NAME
#   docDeroff - convert man pages to txt
# 
#   SYNOPSIS
#   docDeroff <input_file>
# 
#   DESCRIPTION
#   This utility converts a man page into a txt file suitable to be
#   imported for example in a WORD document.
#   It is based on the UNIX command "deroff" and a wrapper written "ad hoc"
#   to deal with included files, to reproduce correctly empty lines and
#   to have a better page layout of the output file.
#
#   FILES
#
#   ENVIRONMENT
#
#   RETURN VALUES
#   0 no errors
#   >0 if there are errors
#
#   CAUTIONS
#
#   EXAMPLES
#
#   SEE ALSO
#   deroff
#
#   BUGS     
#
#------------------------------------------------------------------------
#

MANP=$1
#echo $MANP 
if [ -f $MANP ]
then
    INC=`grep '^\.so' $MANP`
    if [ "$INC" != "" ]
    then 
#        echo "INCLUDE MANP CASE"
        TMPMANP=`echo $INC | awk '{ print $2 }'`
        NEWMANP="../man/$TMPMANP"

# First I delete lines beginning with .nf or .LP, than
# seems that deroff doesn't respect the blank lines.
# To have blank lines between sections I have to substitute each blank line
# with a .nf line. Furthermore, after the title (.TH) line, a new line is inserted
	sed '/^\.nf/d' $NEWMANP | sed '/^\.LP/d'    \
                                | sed 's/^$/\.nf/'  \
                                | sed '/^\.TH/s/$/\
				                  /g' > $NEWMANP.tmp

        deroff $NEWMANP.tmp | awk '   { line[NR] = $0 }
                              END     { for (i = 4; i <= NR; i++) print line[i] } ' \
                              > ../doc/`basename $MANP`.txt
        if [ $? -ne 0 ]
        then
            echo "conversion manpages to txt went wrong" 
            echo "does the doc directory exist?"
	    rm -f $NEWMANP.tmp
            exit 1
        fi
	rm -f $NEWMANP.tmp
    else

# First I delete lines beginning with .nf or .LP, than
# seems that deroff doesn't respect the blank lines. 
# To have blank lines between sections I have to substitute each blank line 
# with a .nf line. Furthermore, after the title (.TH) line, a new line is inserted
	sed '/^\.nf/d' $MANP | sed '/^\.LP/d'    \
                             | sed 's/^$/\.nf/'  \
                             | sed '/^\.TH/s/$/\
			                       /g' > $MANP.tmp

        deroff $MANP.tmp | awk '   { line[NR] = $0 } 
                           END     { for (i = 4; i <= NR; i++) print line[i] } ' \
                           > ../doc/`basename $MANP`.txt
        if [ $? -ne 0 ]
        then
            echo "conversion manpages to txt went wrong" 
            echo "does the doc directory exist?"
	    rm -f $MANP.tmp
            exit 1
        fi
	rm -f $MANP.tmp
    fi
    echo "\n\n\n### generated by docDeroff ###" >> ../doc/`basename $MANP`.txt
    exit 0
else
    exit 1
fi

#
# ___oOo___
