#!/bin/bash
#
# by Chih-Wei Huang
#                         6 August 1998
#                Updated  7 November 1998
#

PATH=.:$PATH
FUNCTIONS="`type -path bg5sgmlfunctions`"
if [ -z $FUNCTIONS ]; then
    echo "$0: include file not found!"
    exit 1
fi
. $FUNCTIONS

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

WIDTH=""
NOIDX=0

for opt in $options
do
    case $opt in
        --width*)
        WIDTH=`echo $opt | cut -b2-`
        ;;

        --noidx)
        NOIDX=1
        ;;

        # ignore these options
        --filter | -f | \
        --man | -m | \
        --pass | -P | \
        --dosnames | -h | \
        --split | -s)
        ;;

        *)
        sgmloptions="$sgmloptions $opt"
        ;;
    esac
done


for src in $filelist
do
    checksrcfile $src
    [ $? = 0 ] && continue

    encodingbg5 $src

    pushd . > /dev/null
    cd $TMP

    # Generate the index
    if [ "$NOIDX" = "0" ]; then
        sgml2html $sgmloptions $name.sgml
        [ -e $name.html ] || exit 2
        $B2A -d -f $name.html -o $name.idx.html
        $LYNX $WIDTH -dump -nolist $name.idx.html | \
        csplit -s --prefix=$name- -  /______/+1 {*}
    else
        touch $name-03
    fi

    # Generate the main content
    sgml2html --split=0 $sgmloptions $name.sgml > /dev/null 
    $B2A -d -f $name.html -o $name.txt.html
    $LYNX $WIDTH -dump $name.txt.html | \
    csplit -s --prefix=$name. --keep-files - /______/+1 {1}
    popd > /dev/null

    # Concatenate the result
    cat $TMP/$name.00 $TMP/$name.01 $TMP/$name-03 $TMP/$name.02 > $name.txt
done

cleartmp

