#!/bin/sh
# Launch Microsoft Word either by itself or with the provided file name to open.

# Fully qualifies pathname of the Windows Application to launch
PROGRAM="C:\\Programme\\Microsoft Office\\OFFICE11\\WINWORD.EXE"

# If launched with no parameters, just run the application and exit.
if [ $# = 0 ]; then
wine "$PROGRAM"
exit 0
fi

# Get the file to open and the directory where it resides.
FILE_NAME=`basename "$1"`
DIR_NAME=`dirname "$1"`

# Convert directory from relative to fully qualified path.
if [ `echo $DIR_NAME | cut -c 1` != "/" ]; then
DIR_NAME=`pwd`/$DIR_NAME
fi

# Change to the directory, run winepath to see if the directory maps to a fake
# windows drive. WINEPATH_ERRS is 1 if not and 0 if yes.
cd "$DIR_NAME"
winepath "$DIR_NAME" > /tmp/winepath.chk 2>&1
WINEPATH_ERRS=`cat /tmp/winepath.chk | grep "Warning" | wc -l`

# If we cannot edit the file, show error box and terminate.
if [ $WINEPATH_ERRS -gt 0 ]; then
kdialog --title "WINE/Microsoft Office Error!" --error \
"ERROR:
Attempting to open a file using a Windows Application where the file is not in a location accessible b$
- Move the file ($1) to a valid location.
- Ensure that ($DIR_NAME) is accessible to WINE.
Fake Windows drives are configured as softlinks (ln -s) in the directory (~/.wine/dosdevices). The cu$
$(ls -l ~/.wine/dosdevices | tr -s " " | cut -d " " -f 9-) "
exit 1
fi

# Finally run the application.
wine "$PROGRAM" "$FILE_NAME"