#!/bin/bash
# Copyright (C) 2006,2007 Lauri Leukkunen <lle@rahina.org>
# Licensed under GPL version 2

function version()
{
	cat $SBOX_DIR/share/scratchbox2/version
	exit 0
}

function usage()
{
	cat <<EOF
sb2 - crosscompiling environment
Usage:
    sb2 [OPTION]... [COMMAND] [PARAMETERS]

If no COMMAND is given, a bash shell in scratchbox2 environment is started.

Options:
    -v           display version
    -d           debug mode: log all redirections
    -h           print this help
    -t TARGET    target to use, use sb2-config -d TARGET to set a default
    -e           emulation mode
    -m MODE      use mapping mode MODE
    -s DIRECTORY load mapping scripts from alternative location

Examples:
    sb2 ./configure
	sb2 make
	sb2 -e make install
	sb2 -m emulate make install
EOF
	exit 2
}

function exit_error()
{
		echo "$@"
		exit 1
}

function sanity_check()
{
	if [ `id -u` = 0 ] ; then
		exit_error "Do not use sbox2 as root!"
	fi
	# check that most important host and target files exist
}

function sboxify_environment()
{
	if [ -r ~/.scratchbox2/config ]; then
		. ~/.scratchbox2/config
	fi

	if [ -z "$SBOX_TARGET" ]; then
		SBOX_TARGET=$DEFAULT_TARGET
	fi

	if [ -z "$SBOX_TARGET" ]; then
		echo "No target specified and none set as default, aborting."
		exit 2
	fi

	if [ ! -e ~/.scratchbox2/$SBOX_TARGET/sb2.config ]; then
		echo "Invalid target specified, aborting."
		exit 2
	fi
	export SBOX_TARGET
	. ~/.scratchbox2/$SBOX_TARGET/sb2.config

	if [ -n "$SBOX_DIR" ]; then
		LD_LIBRARY_PATH=$SBOX_DIR/lib/libsb2:$SBOX_DIR/lib64/libsb2:$SBOX_DIR/lib32/libsb2
		SBOX_LIBSB2="libsb2.so.1"
		if [ -z "$SBOX_REDIR_SCRIPTS" ]; then
			SBOX_REDIR_SCRIPTS="$SBOX_DIR/share/scratchbox2/redir_scripts"
		fi
		SBOX_GCCWRAPPER="$SBOX_DIR/bin/sb_gcc_wrapper"
	else
		exit_error "Incorrect target config in ~/.scratchbox2/$SBOX_TARGET/sb2.config"
	fi

	export LD_LIBRARY_PATH SBOX_LIBSB2 SBOX_REDIR_SCRIPTS SBOX_TARGET_ROOT SBOX_GCCWRAPPER SBOX_TARGET_COMPILER

	# -Riku: I don't think we want to run this on every sb2 invocation..
	HOST_GCC_INC_DIR=$(echo "#include <stdio.h>" | gcc -M -E - | perl -e 'while(<STDIN>) { $foo{$1} = 1 if m/\/usr([^[:space:]]*\/include)/;}; foreach my $k (keys %foo) {print " -isystem $ENV{SBOX_DIR}/share/scratchbox2/host_usr$k"};')

	export PATH=$HOME/.scratchbox2/$SBOX_TARGET/bin:$SBOX_DIR/share/scratchbox2/scripts:$SBOX_DIR/bin:$PATH:/sbin:/usr/sbin:$SBOX_TARGET_ROOT/bin:$SBOX_TARGET_ROOT/usr/bin:$SBOX_TARGET_ROOT/usr/local/bin

	export LD_PRELOAD=$SBOX_LIBSB2
	export SBOX_EXTRA_HOST_COMPILER_ARGS="$HOST_GCC_INC_DIR"
	export SBOX_EXTRA_CROSS_COMPILER_ARGS="--sysroot=$SBOX_TARGET_ROOT"
	export SBOX_SCRATCHBOX_CONFIG=$HOME/.scratchbox2/$SBOX_TARGET/sb2.config
	export PS1="[SB2] \u@\h \w \$ "

	export SBOX_COMPILER_ROOT=$(readlink -f $SBOX_CROSS_GCC_DIR/..)

	if [ "$SBOX_MAPPING_DEBUG" == "1" ]; then
		export SBOX_MAPPING_LOGFILE=$HOME/sb2_mapping.log
		echo "Running scratchbox with these settings:"
		echo "SBOX_LIBSB2 = $SBOX_LIBSB2"
		echo "SBOX_REDIR_SCRIPTS = $SBOX_REDIR_SCRIPTS"
		echo "SBOX_GCCWRAPPER = $SBOX_GCCWRAPPER"
		echo "REDIR_LD_SO = $REDIR_LD_SO"
		echo "REDIR_LD_LIBRARY_PATH = $REDIR_LD_LIBRARY_PATH"
		echo "SBOX_TARGET_ROOT = $SBOX_TARGET_ROOT"
	fi
}

export SBOX_DIR=$(readlink -f $(dirname $0)/..)

while getopts vdht:em:s: foo
do
	case $foo in
	(v) version ;;
	(d) export SBOX_MAPPING_DEBUG=1 ;;
	(h) usage ;;
	(t) export SBOX_TARGET=$OPTARG ;;
	(e) export SBOX_MAPMODE=emulate ;;
	(m) export SBOX_MAPMODE=$OPTARG ;;
	(s) export SBOX_REDIR_SCRIPTS=$OPTARG;;
	(*) usage ;;
	esac
done
shift $(($OPTIND - 1))

# read commands to execute from stdin - not yet implemented
if [ "$1" = "-" ] ; then
	STDIN=true
fi

sboxify_environment

# set the initial binary name for the mapping engine
export __SB2_BINARYNAME="bash"

if [ $# -gt 0 -o "$STDIN" = true ] ; then
	binary="$1"
	shift 1
	args="$@"
	exec /bin/sh -c "$binary $args"
else
	exec /bin/bash --noprofile --norc
fi

