#!/bin/sh
VMDIR=/Users/eliot/Cog/oscogvm/macbuild
VM="Contents/MacOS/Croquet"
case `basename $0` in
*amtvm)	APP=AssertMT.app;;
*dmtvm)	APP=DebugMT.app;;
*fmtvm)	APP=FastMT.app;;
*vmtvm)	APP=VanillaMT.app;;
*mtvm)	APP=CroquetMT.app;;
*avm)	APP=Assert.app;;
*dvm)	APP=Debug.app;;
*fvm)	APP=Fast.app;;
*vvm)	APP=Vanilla.app;;
*)		APP=Croquet.app
esac
ARGS=
EXEC=exec
if [ $# -ge 2 -a "$1" = "-?" ]; then
	EXEC=echo
	shift
fi
while true; do
	test -z "$2" -o "$2" = "--" && ATEND=1 || ATEND=0
#	echo '$ATEND='$ATEND';$1='$1';$IMAGE='$IMAGE
	case "$1" in
	--)	if [ -f "$IMAGE" -a -r "$IMAGE" ]; then
			shift; ARGS="$ARGS" "$@";
			$EXEC "$VMDIR/$APP/$VM" $ARGS "$IMAGE" "$@"
			exit 0 # for the EXEC=echo variant
		else
			echo usage: $0 image 1>&2; exit 1
		fi;;
	/*.image)	IMAGE="$1";;
	"")	echo usage: $0 image 1>&2; exit 1;;
	*.image)	IMAGE="`pwd`/$1";;
	/*.)	test -f "$1image" -a $ATEND = 1 && IMAGE="$1image";;
	*.)		test -f "$1image" -a $ATEND = 1 && IMAGE="`pwd`/$1image";;
	/*)	test -f "$1.image" -a $ATEND = 1 && IMAGE="$1.image";;
	*)	test -f "$1.image" -a $ATEND = 1 && IMAGE="`pwd`/$1.image"
	esac
	if [ -f "$IMAGE" -a -r "$IMAGE" ]; then
		shift
		$EXEC "$VMDIR/$APP/$VM" $ARGS "$IMAGE" "$@"
		exit 0 # for the EXEC=echo variant
	fi
	ARGS="$ARGS $1"
	shift
done
