#!/bin/sh
#!/bin/sh
#
# Script to make drag and drop in KDE possible
#set -x
#

if [ $# -lt 2 ]
then
	exit 0
fi

# Try to create the temp files in a secure way
out_file=`mktemp -q -t ${0##*/}.ps.XXXXXXXXX` || \
	{ echo "$0: Cannot create temporary file" >&2; exit 1; }
err_file=`mktemp -q -t ${0##*/}.err.XXXXXXXXX` || \
	{ echo "$0: Cannot create temporary file" >&2; exit 1; }
trap "rm -f -- \"$out_file\" \"$err_file\"" 0 1 2 3 13 15

# Determine the paper size
paper_size=$1
shift

# Make the PostScript file
antiword -p $paper_size -i 0 "$@" 2>"$err_file" >"$out_file"
if [ $? -ne 0 ]
then
	# Something went wrong
	if [ -r "$err_file" ] && [ -s "$err_file" ]
	then
		konsole --caption "Error from Antword" -e less "$err_file"
	fi
	# Clean up
	rm -f "$out_file" "$err_file"
	exit 1
fi

# Show the PostScript file
gv "$out_file" -nocentre -media $paper_size

# Clean up
rm -f "$out_file" "$err_file"
exit 0
