#!/bin/bash

CWD="`dirname \"$0\"`"

STUDIO_STRING=studio
BUILD_STRING=build
VISION2_DEMO_STRING=vision2_demo
CONSOLE_STRING=console

# Launch X Server on Tiger, on Leopard the system will start it automatically
if [ `uname -r | sed 's/\..*//'` == 8 ]; then
    if [ -z "$DISPLAY" ]; then
        echo 'Setting DISPLAY environment variable to default value.'
        export DISPLAY=:0.0
    fi

    if [ -z "`ps -wx -ocommand | grep -e '[X]11'`" -a ! -f ~/.xinitrc ]; then
        echo 'Opening X11 directly, with modified copy of xinitrc to prevent redundant xterm window.'
        echo "rm -f ~/.xinitrc" > ~/.xinitrc
        sed 's/xterm/# xterm/' /usr/X11R6/lib/X11/xinit/xinitrc >> ~/.xinitrc
        open -a X11
    fi
fi

# Setup Environment Variables and create settings files

# Load user-settings from .profile
if [ -f ~/.profile ]
then
   source ~/.profile
fi

export ISE_EIFFEL=$CWD/../../..
if [ `uname -p` == "powerpc" ]; then
	export ISE_PLATFORM=macosx-ppc
elif [ `uname -p` == "i386" ]; then
	if (( `uname -r|cut -f1 -d.` >= 10 )) && [[ `sysctl hw.cpu64bit_capable` == "hw.cpu64bit_capable: 1" ]]; then
		export ISE_PLATFORM=macosx-x86-64
	else
		export ISE_PLATFORM=macosx-x86
	fi
fi
export ISE_PROJECTS=$HOME
export PATH=$ISE_EIFFEL/studio/spec/$ISE_PLATFORM/bin:$PATH

# Set GOBO environment variables
export GOBO=$ISE_EIFFEL/library/gobo/svn
export GOBO_EIFFEL=ise
export GOBO_CC=gcc
export PATH=$GOBO/bin:$PATH
if [ $1 = $STUDIO_STRING ]
then
	export PS1="Eiffel Console$ "
	/usr/X11R6/bin/xterm -title "EiffelStudio Console (Do not Close)" -geometry 80x25 -e sh -c "$ISE_EIFFEL/studio/spec/$ISE_PLATFORM/bin/estudio"
fi

if [ $1 = $BUILD_STRING ]
then
	$ISE_EIFFEL/esbuilder/spec/$ISE_PLATFORM/bin/esbuilder
fi

if [ $1 = $VISION2_DEMO_STRING ]
then
	$ISE_EIFFEL/vision2_demo/spec/$ISE_PLATFORM/bin/vision2_demo
fi

if [ $1 = $CONSOLE_STRING ]
then
	cd $HOME
	export PS1="Eiffel Console$ "
	/usr/X11R6/bin/xterm -title "Eiffel Console" -e sh
fi
