#! /bin/sh 
#
# sgml2txt
# Greg Hankins, 27 August 1995
#
# Based on the original 'format' and 'qroff' scripts by Tom Gordon 
# and Alexander Horz.
# 

# Don't make any changes here, it is all done by install!
LINUXDOCBIN=linuxdocbin
LINUXDOCLIB=linuxdoclib

TABS="-8"		      # expand replaces tabs with 8 spaces
CHAR="groff"		      # use ASCII (groff) character set 
STYLE=			      # Use standard Linuxdoc-SGML style
COL="no"                      # use col, or sed equivalent
DASHI=-ifmttxt

trap "CleanUp" 0 1 2 3 9 15

usage () {
echo "  Usage: sgml2txt [-f] [-l] [-man] [-style <s>] [-t <n>] <filename> ";
echo "  -f         filter out reverse paper motions (underlines, etc)";
echo "  -l         use latin1 character set (default ASCII)";
echo "  -man       create a groff -man format document (won't run groff)";
echo "  -style <s> use groff/<s>mapping backend in addition to default";
echo "             groff/mapping";
echo "  -t <n>     tabstops each <n>th col (default 8)";
echo "  <filename> SGML source file, .sgml extension is optional";
echo 
echo "  Output will appear in <filename>.txt, or <filename>.man if -man is used"
exit 1 
}

CleanUp() {
    rm -f /tmp/sgml2txt$$tmp
}

# check argc
if [ $# = 0 ]
then
	usage
fi

# do they need help?
case "$1" in
	"-h" | "--help" | "-help") usage
	exit 1
esac

# getopt
for i in $*
do
        case $i in
	      -f)       COL="yes"; shift;;
	      -style) 	style=$2;shift; shift;;
	      -t) 	TABS="-"$2; shift; shift;;
	      -l)	CHAR="latin1"; shift;;
	      -man)	DASHI=-iman; CHAR="man"; shift;;
              --)       shift;
			break;;
        esac
done

# SGML_PATH for sgmls - must be exported
SGML_PATH=$LINUXDOCLIB/dtd/%N.dtd:$LINUXDOCLIB/dtd/%P.dtd:$LINUXDOCLIB/rep/$CHAR/%N
export SGML_PATH

if [ -f $LINUXDOCLIB/dtd/${style}.dcl ]; then
	SGMLDECL=$LINUXDOCLIB/dtd/${style}.dcl
elif [ -f $LINUXDOCLIB/dtd/sgml.dcl ]; then
	SGMLDECL=$LINUXDOCLIB/dtd/sgml.dcl
# else just use sgmls's defaults
fi

if [ "$style" != "" ]; then
	STYLE=$LINUXDOCLIB/rep/$CHAR/${style}mapping; 
fi

# check to see if there is a source file
FILE=$1
if [ -f $FILE.sgml ] 
then
	SGMLFILE=$FILE.sgml
elif [ ! -f $FILE ] 
then
	echo "sgml2txt: can't find $FILE or $FILE.sgml" >&2
	exit 1
else
	SGMLFILE=$FILE
fi

# make name
case $DASHI in
        -iman) MANFILE=`basename $SGMLFILE .sgml`.man
               echo "Making $MANFILE from $SGMLFILE.";;
        *)     TXTFILE=`basename $SGMLFILE .sgml`.txt
               echo "Making $TXTFILE from $SGMLFILE.";;
esac


# format
case $CHAR in
        latin1) cat $SGMLFILE | sed -f $LINUXDOCLIB/latin1.sed | \
                $LINUXDOCBIN/sgmls $DASHI $SGMLDECL > /tmp/sgml2txt$$tmp;;
        *)      $LINUXDOCBIN/sgmls $DASHI $SGMLDECL $SGMLFILE > \
                /tmp/sgml2txt$$tmp;;
esac
if [ $? = 1 ]
then
        echo "SGML parsing error, no formatting done..."
        exit 1
fi

# if there are no SGML parse errors, continue...

case $CHAR in
        latin1) cat /tmp/sgml2txt$$tmp | $LINUXDOCBIN/genertoc | \
		$LINUXDOCBIN/sgmlsasp $STYLE $LINUXDOCLIB/rep/latin1/mapping | \
		expand $TABS | sed -f $LINUXDOCLIB/preroff.sed | \
		groff -T latin1 -t -mgs | cat -s > $TXTFILE;;
	man)	cat /tmp/sgml2txt$$tmp | $LINUXDOCBIN/sgmlsasp \
		$STYLE $LINUXDOCLIB/rep/man/mapping | expand $TABS | \
		sed -f $LINUXDOCLIB/preroff.sed > $MANFILE;;
	*)	cat /tmp/sgml2txt$$tmp | $LINUXDOCBIN/genertoc | \
		$LINUXDOCBIN/sgmlsasp $STYLE $LINUXDOCLIB/rep/$CHAR/mapping | \
		expand $TABS | sed -f $LINUXDOCLIB/preroff.sed | \
		groff -T ascii -t -mgs | cat -s > $TXTFILE;;
esac

if [ $COL = "yes" ]
then
# col hoses ISO-8859-1 characters! 
	case $CHAR in
		latin1)
		cat $TXTFILE | sed -e 's/\(.\)\1/\1/g' \
		-e 's/_\(.\)/\1/g' > /tmp/sgml2txt$$tmp;
		mv /tmp/sgml2txt$$tmp $TXTFILE;;

		man)
		;;

		*)
		cat $TXTFILE | col -bx > /tmp/sgml2txt$$tmp;
		mv /tmp/sgml2txt$$tmp $TXTFILE;;
	esac
fi

rm -f /tmp/sgml2txt$$tmp
exit 0
