#!/bin/sh
#
# Script to make drag and drop in KDE possible
# set -x
#
CONFS=($(kde-config --path config | tr ':' ' '))
CONFDIR=$HOME/${CONFS#$HOME/}
CONF_FILE=${CONFDIR%/}/kantiwordrc

if [ -r $CONF_FILE ]; then
	ENCODING=$(grep encoding $CONF_FILE | tr -d '[:blank:]' | cut -d '=' -f 2)
fi

DESKTOP=$(kde-config --userpath desktop)

if [ $# -lt 2 ]
then
    echo "Would you like to install an icon on your Desktop, where you"
    echo "will be able to drop an *.doc file on to? It will be displayed"
    echo "as a PDF document or as plain text (if no PDF viewer available)."
    echo -n "[Y/n]: > "
    read n
    if [ "x$n" = "xy" ] || [ "x$n" = "xY" ] || [ "x$n" = "x" ]
    then
        if [ -r /etc/papersize ]
       then
           n=$(cat /etc/papersize)
       fi
       if [ "x$n" != "xa4" ] && [ "x$n" != "xletter" ]
       then
           echo
           echo "Type"
           echo "'a' for output papersize A4,"
           echo "'l' for letter or"
           echo "'n' for cancel."
           echo -n "[a/l/N]: > "
           read n
       fi
        if [ "x$n" = "xa" ] || [ "x$n" = "xa4" ]; then
           cp -f /usr/share/antiword/kantiword.eu.desktop $DESKTOP/kantiword.desktop
       elif [ "x$n" = "xl" ] || [ "x$n" = "xletter" ]; then
           cp -f /usr/share/antiword/kantiword.us.desktop $DESKTOP/kantiword.desktop
       fi
    fi
    exit 0
fi

# Determine the temp directory
if [ -d "$TMPDIR" ] && [ -w "$TMPDIR" ]
then
	tmp_dir=$TMPDIR
elif [ -d "$TEMP" ] && [ -w "$TEMP" ]
then
	tmp_dir=$TEMP
else
	tmp_dir="/tmp"
fi                        

# Try to create the temp files in a secure way
if [ -x /bin/tempfile ]
then
	out_file=$(/bin/tempfile -d "$tmp_dir" -p antiword -s ".pdf") || exit 1
	err_file=$(/bin/tempfile -d "$tmp_dir" -p antiword -s ".err")
	txt_file=$(/bin/tempfile -d "$tmp_dir" -p antiword -s ".txt")
	if [ $? -ne 0 ]
	then
		rm -f "$out_file"
		exit 1
	fi
elif [ -x /bin/mktemp ]
then
	out_file=$(/bin/mktemp -q -p "$tmp_dir" antiword.pdf.XXXXXXXXX) || exit 1
	err_file=$(/bin/mktemp -q -p "$tmp_dir" antiword.err.XXXXXXXXX)
	txt_file=$(/bin/mktemp -q -p "$tmp_dir" antiword.txt.XXXXXXXXX)
	if [ $? -ne 0 ]
	then
		rm -f "$out_file"
		exit 1
	fi
else
	# Creating the temp files in an un-secure way
	out_file=$tmp_dir"/antiword.$$.pdf"
	err_file=$tmp_dir"/antiword.$$.err"
	txt_file=$tmp_dir"/antiword.$$.txt"
fi

# filename is empty, user had clicked on icon, so print a help message
# in err_file
if [ -z $2 ]
then
    cat <<_EOF >"$err_file"
You should drag any *.doc file and drop it on this icon for
displaying. Kantiword can not do anything with an empty filename.

_EOF
fi

# Determine the paper size
paper_size=$1
shift

# Make a PDF file 
antiword -a $paper_size -i 0 "$@" 2>"$err_file" >"$out_file"
if [ $? -ne 0 ]
then
	# Something went wrong
	if [ -r "$err_file" ] && [ -s "$err_file" ]
	then
           if [ -x /usr/bin/konsole ]
           then
               /usr/bin/konsole --caption "Error from Antiword" -e less "$err_file"
           else
               /usr/bin/X11/xterm -T "Error from Antiword" -e less "$err_file"
           fi
	fi
	# Clean up
	rm -f "$out_file" "$err_file" "$txt_file"
	exit 1
fi

# Show the PDF file
if [ -x /usr/bin/kpdf ]
then
    /usr/bin/kpdf "$out_file"
elif [ -x /usr/bin/xpdf ]
then
    /usr/bin/xpdf "$out_file" -paper=$paper_size
elif [ -x /usr/bin/gv ]
then
    /usr/bin/gv "$out_file" --nocenter --media=$paper_size
else
    # no PDF viewer available, so display as plain text
    antiword "$@" 2>"$err_file" >"$txt_file"
    if [ -x /usr/bin/konsole ]
    then
        /usr/bin/konsole --caption "Text output from Antiword" -e less "$txt_file"
    else
        /usr/bin/X11/xterm -T "Text output from Antiword" -e less "$txt_file"
    fi
fi

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