#!/bin/sh 

if [ ${#} -ne "1" ]; then
	echo "Usage: ${0} <word document>"
	exit 1
fi

# check our requirements
type wvPS >/dev/null 2>&1
if [ ${?} -ne "0" ]; then
	echo "Error: required program 'wvPS' was not found"
	exit 1
fi

if [ -z "$GV" ]; then
	# determine the viewer application:
	# - see (let mailcap decide)
	# - ggv (GNOME Ghostview)
	# - kghostview (KDE Ghostview)
	# - gv (Xaw3d Ghostview)
	# - ghostview (the classic Ghostscript frontend)
	for GV in see ggv kghostview gv ghostview; do
		type "$GV" >/dev/null 2>&1 && break
	done
	if [ ${?} -ne "0" ]; then
		# unrecoverable error
		echo "Could not find a suitable PostScript viewer."
		echo "Please install ggv, kghostview, gv, or ghostview"
		exit 1
	fi
fi

# temporary target directory
t_dir=${TMPDIR:-/tmp}/wvMime-$USER-$$
mkdir -m 700 "$t_dir" || exit
trap 'rm -rf "$t_dir"' 0 1 2 3 15

# PS file
name=`basename "$1" .doc`.ps

wvPS --targetdir="$t_dir" "$1" "$name"
if [ ${?} -ne "0" ]; then 
	echo "Could not translate into Postscript" 
	exit 1 
fi 

# call our ghost-viewer
$GV "$t_dir/$name"
