#!/bin/bash
#
# by Chih-Wei Huang
#                        31 July 1998
#              Updated   20 September 1998
#

BINPATH="/bin:/usr/bin:/usr/local/bin"
PATH="$BINPATH:`pwd`"

if [ $# = 0 ]; then
    echo "Usage: $0 [options] file1 [file2, ...]"
    exit 1
fi

UMASK=`umask`
umask 022

unset LANG
unset LC_CTYPE
unset LC_ALL

unalias b2a
B2A="`type -path b2a`"
if [ -z "$B2A" ]; then
    echo "b2a utility not found!"
    exit 1
fi

PATH=$BINPATH

unalias dos2unix
DOS2UNIX="`type -path dos2unix`"
if [ -z "$DOS2UNIX" ]; then
    DOS2UNIX=cat
fi

options=""
filelist=""
DEBUG=0

TMP=/tmp/bg5sgmltools.$$
trap 'rm -rf $TMP' 2 3 15
mkdir $TMP

for opt in $*; do
    if [ "`echo $opt | cut -c1`" = "-" ]; then
        if [ "$opt" = "-d" ]; then
            DEBUG=1
        else
            options="$options $opt"
        fi
    else
        filelist="$filelist $opt"
    fi 
done


function checksrcfile()
{
    if [ ! -e $1 ]; then
        if [ ! -e $1.sgml ]; then
            echo "File $1 doesn't exist!"
            return 0
        fi
        src=$1.sgml
    fi
    return 1
}

function encodingbg5()
{
    name=`basename $1 .sgml`
    # Filter out some buggy macros in latex output
    sed 's/<nidx>/<!--nidx>/g; s/<\/nidx>/<\/nidx-->/g' $1 | \
    $DOS2UNIX | $B2A -e -o $TMP/$name.sgml
}

function cleartmp()
{
    [ "$DEBUG" != "1" ] && rm -rf $TMP
    umask $UMASK
    exit 0
}

