#! /bin/sh 
#
# sgml2txt
# Greg Hankins, 27 August 1995
#
# Based on the original 'format' and 'qroff' scripts by Tom Gordon 
# and Alexander Horz.
# 
# $BF|K\8lBP1~HG(B by $B>.Ln(B $BE0(B ono@jf.gee.kyoto-u.ac.jp 1996$BG/(B3$B7n(B16$BF|(B

# Don't make any changes here, it is all done by install!
LINUXDOCBIN=/usr/bin
LINUXDOCLIB=/usr/lib/linuxdoc-sgml

TABS="-8"		      # expand replaces tabs with 8 spaces
CHAR="japan"		      # use Japanese character set 
STYLE=			      # Use standard Linuxdoc-SGML style
COL="no"                      # use col, or sed equivalent
BLANK=1			      # omit blank line over one
DASHI=-ifmttxt

trap "CleanUp" 0 1 2 3 9 15

usage () {
echo " Usage: sgml2txt [-f] [-a/-l] [-man/-cat] [-style <s>] [-t <n>] <filename> ";
echo "  -f         filter out reverse paper motions (underlines, etc)";
echo "  -a         use only ASCII character set (default Japanese)";
echo "  -l         use latin1 character set (default Japanese)";
echo "  -man       create a groff -man format document (won't run groff)";
echo "  -cat       create a groff -man formated document";
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 "  -b <n>     omit blank line over <n> (default $BLANK)";
echo "  <filename> SGML source file, .sgml extension is optional";
echo 
echo "  Output will appear in <filename>.txt, <filename>.man if -man is used";
echo "  or <filename>.cat if -cat 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;;
	      -a)	CHAR="groff"; shift;;
	      -l)	CHAR="latin1"; shift;;
	      -man)	DASHI=-iman; shift;;
	      -cat)	DASHI=-icat; shift;;
	      -b)	BLANK=$2; shift; shift;;
              --)       shift;
			break;;
        esac
done

# SGML_PATH for sgmls - must be exported
if [ $CHAR = "latin1" ]; then
	SGML_PATH=$LINUXDOCLIB/dtd/%N.dtd:$LINUXDOCLIB/dtd/%P.dtd:$LINUXDOCLIB/rep/latin1/%N
else
	SGML_PATH=$LINUXDOCLIB/dtd/%N.dtd:$LINUXDOCLIB/dtd/%P.dtd:$LINUXDOCLIB/rep/groff/%N
fi
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
	if [ $CHAR = "latin1" ]; then
		STYLE=$LINUXDOCLIB/rep/latin1/${style}mapping;
	else
		STYLE=$LINUXDOCLIB/rep/groff/${style}mapping;
	fi
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.";;
        -icat) 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) if [ "$DASHI" = "-iman" -o "$DASHI" = "-icat" ]; then
			echo "Sorry, manual page in laten1 is not available.";
			rm -f /tmp/sgml2txt$$tmp;
			exit 1;
		else
			cat /tmp/sgml2txt$$tmp | $LINUXDOCBIN/genertoc | \
			$LINUXDOCBIN/sgmlsasp $STYLE \
			$LINUXDOCLIB/rep/latin1/mapping | \
			expand $TABS | sed -f $LINUXDOCLIB/preroff.sed | \
			groff -T latin1 -t -ms | \
			$LINUXDOCBIN/cutblank $BLANK > $TXTFILE;
		fi;;
	japan)	if [ "$DASHI" = "-iman" -o "$DASHI" = "-icat" ]; then
			cat /tmp/sgml2txt$$tmp | $LINUXDOCBIN/sgmlsasp \
			$STYLE $LINUXDOCLIB/rep/man/jmapping | expand $TABS | \
			sed -f $LINUXDOCLIB/preroff.sed > $MANFILE;
		else
			cat /tmp/sgml2txt$$tmp | $LINUXDOCBIN/genertoc | \
			$LINUXDOCBIN/sgmlsasp $STYLE \
			$LINUXDOCLIB/rep/groff/jmapping | \
			expand $TABS | sed -f $LINUXDOCLIB/preroff.sed | \
			groff -T nippon -t -ms | \
			$LINUXDOCBIN/cutblank $BLANK > $TXTFILE;
		fi;;
	*)	if [ "$DASHI" = "-iman" -o "$DASHI" = "-icat" ]; then
			cat /tmp/sgml2txt$$tmp | $LINUXDOCBIN/sgmlsasp \
			$STYLE $LINUXDOCLIB/rep/man/mapping | expand $TABS | \
			sed -f $LINUXDOCLIB/preroff.sed > $MANFILE;
		else
			cat /tmp/sgml2txt$$tmp | $LINUXDOCBIN/genertoc | \
			$LINUXDOCBIN/sgmlsasp $STYLE \
			$LINUXDOCLIB/rep/groff/mapping | \
			expand $TABS | sed -f $LINUXDOCLIB/preroff.sed | \
			groff -T ascii -t -ms | \
			$LINUXDOCBIN/cutblank $BLANK > $TXTFILE;
		fi;;
esac

if [ $DASHI = "-icat" ]; then
	case $CHAR in
		japan)
			groff -T nippon -man $TXTFILE > /tmp/sgml2txt$$tmp;
			mv /tmp/sgml2txt$$tmp $TXTFILE;;

		groff)
			groff -T ascii -man $TXTFILE > /tmp/sgml2txt$$tmp;
			mv /tmp/sgml2txt$$tmp $TXTFILE;;
	esac
fi

if [ $COL = "yes" -a $DASHI != "-iman" ]
then
# col hoses ISO-8859-1 and Japanese characters! 
	case $CHAR in
		groff)
			cat $TXTFILE | col -bx > /tmp/sgml2txt$$tmp;
			mv /tmp/sgml2txt$$tmp $TXTFILE;;

		*)
			cat $TXTFILE | jcolb | nkf -e > /tmp/sgml2txt$$tmp;
			mv /tmp/sgml2txt$$tmp $TXTFILE;;
	esac
fi

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