#!/bin/ksh

# Defaults
IceCmd=`whence $0`
IceCmdDir=`dirname $IceCmd`
StableVersion=02.05

use64="Yes"
version=$StableVersion
args=""

# Parse Comand Line
while [ $# -gt 0 ]
do
	case $1 in
		-use32bit | -32 | -32bit )
			use64=""
			;;
		-use64bit | -64 | -64bit )
			use64="Yes"
			;;
		-version | -v )
			shift
			version=$1
			;;
		-h | -help | --help )
			echo Usage : `$0` "[ -64 -32 -version ##.## ] commands"
			exit
			;;
		*)
			nw=`echo $1 | wc -w`
			if [ $nw -gt 1 ]
			then
				args="$args \"$1\""
			else
				args="$args $1"
			fi
			;;
	esac

	shift
done

ICE_ROOT=${ICE_ROOT:-""}
if [ "$ICE_ROOT" = "" ] 
then
	ICE_ROOT=$IceCmdDir
fi

# ICE_ROOT Must Be Set By Now
ICE_ROOT=${ICE_ROOT:?"Environment Variable Unset : Exiting"}

#
# Set the Support and Release Directories
#
ICE_SUPPORT_ROOT=${ICE_SUPPORT_ROOT:-""}
if [ "$ICE_SUPPORT_ROOT" = "" ] 
then
	ICE_SUPPORT_ROOT=$ICE_ROOT/Support$version
fi
ICE_RELEASE_ROOT=${ICE_RELEASE_ROOT:-""}
if [ "$ICE_RELEASE_ROOT" = "" ] 
then
	ICE_RELEASE_ROOT=$ICE_ROOT/Release$version
fi
if test -r $ICE_SUPPORT_ROOT
then
	set Status="OK"
else
	echo "Unusable Support Directory $ICE_SUPPORT_ROOT"
	exit
fi
if test -r $ICE_RELEASE_ROOT
then
	set Status="OK"
else
	echo "Unusable Release Directory $ICE_RELEASE_ROOT"
	exit
fi

PATH=${PATH-""}
LD_LIBRARY_PATH=${LD_LIBRARY_PATH-""}
LD_LIBRARYN32_PATH=${LD_LIBRARYN32_PATH-""}
LD_LIBRARY64_PATH=${LD_LIBRARY64_PATH-""}

PATH=$ICE_ROOT:$PATH
PATH=$ICE_SUPPORT_ROOT/bin:$PATH
PATH=$ICE_RELEASE_ROOT/bin:$PATH
PATH=$ICE_RELEASE_ROOT/lib/python:$PATH
ICE_HOST=${ICE_HOST:-`uname -s`}
case $ICE_HOST in
	IRIX64 )
		Version=`uname -r`
		LD_LIBRARYN32_PATH=$LD_LIBRARYN32_PATH:$ICE_SUPPORT_ROOT/IRIXN32/lib
		LD_LIBRARYN32_PATH=$LD_LIBRARYN32_PATH:$ICE_SUPPORT_ROOT/IRIXN32/lib/vtk
		LD_LIBRARYN32_PATH=$LD_LIBRARYN32_PATH:$ICE_RELEASE_ROOT/IRIXN32/lib
		LD_LIBRARY64_PATH=$LD_LIBRARY64_PATH:$ICE_SUPPORT_ROOT/IRIX64/lib
		LD_LIBRARY64_PATH=$LD_LIBRARY64_PATH:$ICE_SUPPORT_ROOT/IRIX64/lib/vtk
		LD_LIBRARY64_PATH=$LD_LIBRARY64_PATH:$ICE_RELEASE_ROOT/IRIX64/lib
		if [ "$use64" = "Yes" ]
		then
			PATH=$ICE_SUPPORT_ROOT/IRIXN32/bin:$PATH
			PATH=$ICE_SUPPORT_ROOT/IRIX64/bin:$PATH
			PATH=$ICE_RELEASE_ROOT/IRIXN32/bin:$PATH
			PATH=$ICE_RELEASE_ROOT/IRIX64/bin:$PATH
		else
			ICE_HOST=IRIXN32
			PATH=$ICE_SUPPORT_ROOT/IRIX64/bin:$PATH
			PATH=$ICE_SUPPORT_ROOT/IRIXN32/bin:$PATH
			PATH=$ICE_RELEASE_ROOT/IRIX64/bin:$PATH
			PATH=$ICE_RELEASE_ROOT/IRIXN32/bin:$PATH
		fi
			
		;;
	IRIX )
		Version=`uname -r`
		if test $Version -lt 6; then
			ICE_HOST=IRIX32
		fi
		LD_LIBRARYN32_PATH=$LD_LIBRARYN32_PATH:$ICE_SUPPORT_ROOT/IRIXN32/lib
		LD_LIBRARYN32_PATH=$LD_LIBRARYN32_PATH:$ICE_SUPPORT_ROOT/IRIXN32/lib/n32
		LD_LIBRARY_PATH=$LD_LIBRARYN32_PATH:$ICE_SUPPORT_ROOT/IRIXN32/lib
		LD_LIBRARY_PATH=$LD_LIBRARYN32_PATH:$ICE_SUPPORT_ROOT/IRIXN32/lib/vtk
		LD_LIBRARYN32_PATH=$LD_LIBRARYN32_PATH:$ICE_RELEASE_ROOT/IRIXN32/lib
		LD_LIBRARY_PATH=$LD_LIBRARYN32_PATH:$ICE_RELEASE_ROOT/IRIXN32/lib
		PATH=$ICE_SUPPORT_ROOT/IRIXN32/bin:$PATH
		PATH=$ICE_RELEASE_ROOT/IRIXN32/bin:$PATH
		;;
	AIX )
		if [ "$use64" = "Yes" ]
		then
			ICE_64HOST="$ICE_HOST"64
			LIBDIR=$LIBDIR:$ICE_SUPPORT_ROOT/$ICE_64HOST/lib
			LIBDIR=$LIBDIR:$ICE_SUPPORT_ROOT/$ICE_64HOST/lib/vtk
			LIBDIR=$LIBDIR:$ICE_RELEASE_ROOT/$ICE_64HOST/lib
			PATH=$ICE_SUPPORT_ROOT/$ICE_HOST/bin:$PATH
			PATH=$ICE_SUPPORT_ROOT/$ICE_64HOST/bin:$PATH
			PATH=$ICE_RELEASE_ROOT/$ICE_64HOST/bin:$PATH
		else
			LIBDIR=$LIBDIR:$ICE_SUPPORT_ROOT/$ICE_HOST/lib
			LIBDIR=$LIBDIR:$ICE_SUPPORT_ROOT/$ICE_HOST/lib/vtk
			LIBDIR=$LIBDIR:$ICE_RELEASE_ROOT/$ICE_HOST/lib
			PATH=$ICE_SUPPORT_ROOT/$ICE_HOST/bin:$PATH
			PATH=$ICE_RELEASE_ROOT/$ICE_HOST/bin:$PATH
		fi
		;;
	Linux )
		LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ICE_SUPPORT_ROOT/$ICE_HOST/lib
		LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ICE_SUPPORT_ROOT/$ICE_HOST/lib/vtk
		LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ICE_RELEASE_ROOT/$ICE_HOST/lib
		PATH=$ICE_SUPPORT_ROOT/$ICE_HOST/bin:$PATH
		PATH=$ICE_RELEASE_ROOT/$ICE_HOST/bin:$PATH
		;;
	*)
		if [ "$use64" = "Yes" ]
		then
			ICE_64HOST="$ICE_HOST"64
			LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ICE_SUPPORT_ROOT/$ICE_64HOST/lib
			LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ICE_SUPPORT_ROOT/$ICE_64HOST/lib/vtk
			LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ICE_RELEASE_ROOT/$ICE_64HOST/lib
			PATH=$ICE_SUPPORT_ROOT/$ICE_HOST/bin:$PATH
			PATH=$ICE_SUPPORT_ROOT/$ICE_64HOST/bin:$PATH
			PATH=$ICE_RELEASE_ROOT/$ICE_64HOST/bin:$PATH
		else
			LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ICE_SUPPORT_ROOT/$ICE_HOST/lib
			LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ICE_SUPPORT_ROOT/$ICE_HOST/lib/vtk
			LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ICE_RELEASE_ROOT/$ICE_HOST/lib
			PATH=$ICE_SUPPORT_ROOT/$ICE_HOST/bin:$PATH
			PATH=$ICE_RELEASE_ROOT/$ICE_HOST/bin:$PATH
		fi
		;;
esac

export ICE_ROOT
export ICE_HOST
export PATH
export LD_LIBRARY_PATH
export LD_LIBRARYN32_PATH
export LD_LIBRARY64_PATH
export LIBDIR
export ICE_SUPPORT_ROOT
export ICE_RELEASE_ROOT

if test -r $ICE_SUPPORT_ROOT/.icerc
then
	. $ICE_SUPPORT_ROOT/.icerc
fi
if test -r $HOME/.icerc
then
	. $HOME/.icerc
fi

## Overlay Shell Command
exec $args
