#!/bin/sh

if [ $# -ne 2 ]; then
    echo "Usage: run_swin application filename"
    exit 1
fi

APP=$1
FILENAME=$2

EXE="win $APP"
FILE=`echo $FILENAME | sed 's/.*\/\(.*\)$/\1/'`
NUMDOTS=`echo $FILE | sed 's/[^.]*//g' | wc -c | awk '{print $1}'`
if [ $NUMDOTS -le 2 ]; then
    if `echo $FILE | awk '{FS="."; error = 0 } length($1) > 8 {error = 1} length($2) > 3 {error = 1} {if (error == 1) {exit
1} else {exit 0}} {error = 0}'`; then
        if [ -z "`fuser -q $HOME/*.hdf`" ]; then
            ARGS="j:\\$FILE"
            PARSED_ARGS="net use j: `dirname $FILENAME`;$EXE -f $ARGS"
	    if [ -x /usr/lib/SoftWindows2/bin/SoftWindows2 ]; then
	            /usr/lib/SoftWindows2/bin/SoftWindows2 -c "$PARSED_ARGS"
	    else
		if [ -x /usr/lib/SoftWindows/bin/SoftWindows ]; then
		    /usr/lib/SoftWindows/bin/SoftWindows -c "$PARSED_ARGS"
		else
		    /usr/bin/X11/xconfirm -noframe -icon error -header "Notice" -B "OK" -t "SoftWindows is not installed on your system" -t "in either /usr/lib/SoftWindows or /usr/lib/SoftWindows2." 1> /dev/null 2> /dev/null
		fi
	    fi
        else
            /usr/bin/X11/xconfirm -noframe -icon error -header "Notice" -B "OK" -t "SoftWindows is already running on your system." -t "You may open this file from within the current SoftWindows session." 1> /dev/null 2> /dev/null
        fi
    else
        /usr/bin/X11/xconfirm -noframe -icon warning -header "Warning!" -B "Accept" -t "Filename or extension is too long!  Not a valid DOS filename!" 1> /dev/null 2> /dev/null
    fi
else
    /usr/bin/X11/xconfirm -noframe -icon warning -header "Warning!" -B "Accept" -t "Too many periods in the filename!  Not a valid DOS filename!" 1> /dev/null 2> /dev/null
fi
