#!/bin/sh

prefix=/usr
exec_prefix=${prefix}
datadir=${prefix}/share

# argument checking
if [ ${#} -ne "2" ]; then
	echo "Usage: ${0} <word document> <text output file>"
	exit 1
fi

# first, test for wvWare
which wvWare >/dev/null 2>&1
if [ ${?} -ne "0" ]; then
    echo "Could not find required program 'wvWare'"
    exit 1
fi

# start out optimistic
USING_LYNX=1
which lynx >/dev/null 2>&1
if [ ${?} -ne "0" ]; then
	echo "Could not find required program 'lynx'"
	echo "Not using lynx. Ouput will be pretty bad."
	USING_LYNX=0
fi

if [ ${USING_LYNX} -ne "0" ]; then

    # intermediate file
    TMP_FILE=${2}.html

    wvWare --nographics -x ${datadir}/wv/wvHtml.xml "${1}" > "${TMP_FILE}"
    if [ ${?} -ne "0" ]; then
	echo "Could not convert into HTML"
	exit 1
    fi

    # lynx actually does quite well
    TERM=vt100 lynx -dump -force_html "${TMP_FILE}" > "${2}"
    if [ ${?} -ne "0" ]; then
	    echo "Could not convert into Text"
	    exit 1
    fi

    # clean up
    rm -f "${TMP_FILE}"

else
    # fall back onto our cruddy output
    # this is, admittedly, better than running
    # 'strings' on the word document though :)
    wvWare --nographics -x ${datadir}/wv/wvText.xml "${1}" > "${2}"
fi
