#!/bin/bash
# File:        etoys
# Author:      Bert Freudenberg
#              and modified by Yoshiki Ohshima for Squeakland-OLPC
#              and modified by Michael Rueger for Etoys.app
# Description: Script to start the Squeak VM binary 
#              with the etoys.image

DIR=/usr/share/Etoys.app
BIN=$DIR/Contents/Linux686
VM=$BIN/etoys
VMOPTIONS="-encoding UTF-8 -plugins $BIN -vm-display-x11 "
IMAGE="$DIR/Contents/Resources/etoys.image"
IMOPTIONS=""
DOCUMENT=""

[ -z "$SQUEAK_SECUREDIR" ] && export SQUEAK_SECUREDIR="$HOME/.etoys"
[ -z "$SQUEAK_USERDIR" ] && export SQUEAK_USERDIR="$HOME/Etoys"

[ ! -d "$SQUEAK_SECUREDIR" ] && mkdir -p "$SQUEAK_SECUREDIR" && chmod 700 "$SQUEAK_SECUREDIR"
[ ! -d "$SQUEAK_USERDIR" ] && mkdir -p "$SQUEAK_USERDIR"

if [ -r "$ALTIMG" ] ; then
  echo Using alternate image "$ALTIMG"
  IMAGE="$ALTIMG"
  export SQUEAK_USERDIR=`dirname $ALTIMG`
fi

while [ -n "$1" ] ; do
	if [ -z "$2" ] ; then
		echo Usage: $0 [-opt value] [arg value]
		exit -1
	fi
	case "$1" in
	        --document)
			case "$2" in
			    /*) DOCUMENT="$2"
				;;
			    *) DOCUMENT="$PWD/$2"
				;;
			esac
 			shift
			;;
		-*) VMOPTIONS="$VMOPTIONS $1 $2"
			shift
			;;
		*)  IMOPTIONS="$IMOPTIONS $1 $2"
			shift
			;;
	esac
	shift
done
# is there a clever way to determine if you are local?
if [ $DISPLAY = ":0.0" ]; then
    VMOPTIONS=$VMOPTIONS" -xshm "
fi



# VM, Image, and Document are non-optional
# Document has to be present even if empty
echo "$VM" $VMOPTIONS "$IMAGE" "$DOCUMENT" $IMOPTIONS
exec "$VM" $VMOPTIONS "$IMAGE" "$DOCUMENT" $IMOPTIONS
