#!/bin/bash
# Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies).
# All rights reserved.
# This component and the accompanying materials are made available
# under the terms of the License "Eclipse Public License v1.0"
# which accompanies this distribution, and is available
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
#
# Initial Contributors:
# Nokia Corporation - initial contribution.
#
# Contributors:
#
# Description:
# raptor script
# add mingw to the PATH if we are running Cygwin on Windows
#

# If SBS_HOME is not set in the environment then work it out
# from the path to this script
if [ -z "$SBS_HOME" ] ; then
	temp=$0
	SBS_HOME=$(cd ${temp%/*} && echo $PWD)
	export SBS_HOME=${SBS_HOME%/bin}
fi

# Ensure that the host type is set for Raptor:
eval $($SBS_HOME/bin/gethost.sh -e)

if [ -z "$HOSTPLATFORM" ]; then
	echo "Error: HOSTPLATFORM could not be determined." 1>&2
	exit 1
fi

if [ ! -d "$SBS_HOME/$HOSTPLATFORM_DIR" ]; then
cat 1>&2 <<EOERROR
Error: sbs has not been installed with support for your platform: "${HOSTPLATFORM}".

The utilites for your platform should be in "$SBS_HOME/$HOSTPLATFORM_DIR" but sbs
cannot find them there.

sbs is supported on:
	win32
	linux i386 libc2_3 (Redhat 4)

sbs has been known to work (but is not supported) on:
	linux x86_64 libc2_5  (e.g. Centos/Redhat 5.3 64-bit)
	linux i386 libc2_8  (e.g. Fedora 9 32-bit)
	linux x86_64 libc2_10 (e.g. Fedora 11 64-bit)

Even with the appropriate utilities it may be necessary to install 32-bit
compatibility versions of some libraries (e.g. glibc) on these platforms,
particularly for 3rd party tools which are not built natively such as
compilers.

It may be possible to build and install the utilities for your platform by
entering $SBS_HOME/util and running
	make -k
A full development environment is required however.
EOERROR
	exit 1
fi

if [ "$OSTYPE" == "cygwin" ]; then
	__MINGW__=${SBS_MINGW:-$SBS_HOME/$HOSTPLATFORM_DIR/mingw}
	__CYGWIN__=${SBS_CYGWIN:-$SBS_HOME/$HOSTPLATFORM_DIR/cygwin}

	# Command for unifying path strings. For example, "c:\some\path" and
	# "/cygdrive/c/some/path" will both be converted into "c:/some/path".
	u="$__CYGWIN__/bin/cygpath.exe -m"
	
	SBS_HOME=${SBS_HOME//\\//}
	export SBS_HOME=$($u "$SBS_HOME")
	
	__MINGW__=$($u "$__MINGW__")
	__CYGWIN__=$($u "$__MINGW__")
	export PATH=${__MINGW__}/bin:${__CYGWIN__}/bin:$SBS_HOME/$HOSTPLATFORM_DIR/bin:$PATH

	# Tell Cygwin not to map unix security attributes to windows to
	# prevent raptor from potentially creating read-only files:
	export CYGWIN='nontsec nosmbntsec'

	# The python and PYTHONPATH used by Raptor are determined by, in order of precedence:
	#  1. the SBS_PYTHON and SBS_PYTHONPATH environment variables (if set)
	#  2. the python shipped locally with Raptor (if present)
	#  3. the python on the system PATH and the PYTHONPATH set in the system environment

	__LOCAL_PYTHON__=$SBS_HOME/win32/python264/python.exe

	if [ -n "$SBS_PYTHON" ]; then
		__PYTHON__=$SBS_PYTHON
	elif [ -f "$__LOCAL_PYTHON__" ]; then
		__PYTHON__=$__LOCAL_PYTHON__
		export SBS_PYTHON=$__PYTHON__
		export PYTHONPATH=
	else
		__PYTHON__=python.exe
	fi
	__PYTHON__=$($u "$__PYTHON__")		

	if [ -n "$SBS_PYTHONPATH" ]; then
		export PYTHONPATH=$($u "$SBS_PYTHONPATH")
	fi
else
	export PYTHONPATH=${SBS_PYTHONPATH:-$SBS_HOME/$HOSTPLATFORM_DIR/python262/lib}
	PATH=$SBS_HOME/$HOSTPLATFORM_DIR/python262/bin:$SBS_HOME/$HOSTPLATFORM_DIR/bin:$PATH
	LD_LIBRARY_PATH=$SBS_HOME/$HOSTPLATFORM_DIR/python262/lib:$SBS_HOME/$HOSTPLATFORM_DIR/bv/lib:$LD_LIBRARY_PATH

	export PATH LD_LIBRARY_PATH
	__PYTHON__=python
fi


# call raptor_start.py with the arguments

RAPTOR_START="$SBS_HOME/python/raptor_start.py"

if [ -e "$RAPTOR_START" ]; then
	# run the source version
	${__PYTHON__} "$RAPTOR_START" "$@"
elif [ -e "$RAPTOR_START"c ]; then
	# run the compiled version
	${__PYTHON__} "$RAPTOR_START"c "$@"
else
	echo "Cannot start SBSV2 - $RAPTOR_START not found." 1>&2
	echo "Check your SBS_HOME environment variable." 1>&2
fi
