#!/bin/sh

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

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

if [ "x$o_file" = "x-" ]; then
    o_file=/dev/stdout
fi

USE_DUMP=0
which w3m >/dev/null 2>&1
if [ ${?} -eq "0" ]; then
    USE_DUMP=4
else
    which elinks >/dev/null 2>&1
    if [ ${?} -eq "0" ]; then
	USE_DUMP=3
    else
	which links >/dev/null 2>&1
	if [ ${?} -eq "0" ]; then
	    USE_DUMP=2
	else
	    which lynx >/dev/null 2>&1
	    if [ ${?} -eq "0" ]; then
		USE_DUMP=1
	    fi
	fi    
    fi
fi

tmpdir=$(mktemp -d ${TMPDIR:-/tmp}/wvText.XXXXXXXX) || { echo "$0: can not create temporary directory" >& 2; exit 1; }

if [ $USE_DUMP -eq "1" ]; then
	echo "Could not find required program 'w3m', 'elinks' or 'links'"
	echo "Using lynx. Ouput will be pretty ugly."
elif [ $USE_DUMP -eq "0" ]; then
	echo "Could not find required program 'w3m', 'elinks', 'links', or even 'lynx'"
	echo "Using wvWare -x wvText.xml. Ouput will be pretty bad."
fi

if [ $USE_DUMP -gt "0" ]; then

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

    # intermediate file
    TMP_FILE=`mktemp "$tmpdir/wv-XXXXXX"`
    TMP_FILE=`basename "$TMP_FILE"`

    wvHtml -1 "${1}" --targetdir="${tmpdir}" "${TMP_FILE}" >/dev/null 2>&1
    if [ ${?} -ne "0" ]; then
	echo "Could not convert into HTML"
	rm -rf "${tmpdir}"
	exit 1
    fi

    if [ $USE_DUMP -eq "4" ]; then
	
	w3m -dump -T text/html "${tmpdir}/${TMP_FILE}" > "$o_file"
    elif [ $USE_DUMP -eq "3" ]; then
	# elinks does the best
	elinks -dump -force-html "${tmpdir}/${TMP_FILE}" > "$o_file"
    elif [ $USE_DUMP -eq "2" ]; then
	# links does a pretty good job
	links -dump "${tmpdir}/${TMP_FILE}" > "$o_file"
    else
	# lynx sucks, but does better than wvText.xml
	TERM=vt100 lynx -dump -force_html "${tmpdir}/${TMP_FILE}" > "$o_file"
    fi;

    if [ ${?} -ne "0" ]; then
	    echo "Could not convert into Text"
	    rm -rf "${tmpdir}"
	    exit 1
    fi

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

# clean up
rm -rf "${tmpdir}"
