#!/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
    -L level     enable logging (levels=one of error,warning,notice,info,debug,noise)
    -d           debug mode: log all redirections (logging level=debug)
    -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
    -Q BUGLIST   emulate bugs of the old scratchbox 1 (BUGLIST consists of
                 letters: 'x' enables exec permission checking bug emulation)

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

	LD_LIBRARY_PATH=$SBOX_DIR/lib/libsb2:$SBOX_DIR/lib64/libsb2:$SBOX_DIR/lib32/libsb2:/emul/lib64/libsb2:/emul/lib32/libsb2
	SBOX_LIBSB2="libsb2.so.1"
	
	. ~/.scratchbox2/$SBOX_TARGET/sb2.config

	if [ "$SBOX_CONFIG_VERSION" != "5" ]; then
		echo "Please run sb2-init for your target:"
		echo "name: $SBOX_TARGET"
		echo "dir:  $SBOX_TARGET_ROOT"
		echo "gcc:  $SBOX_CROSS_GCC_DIR/${SBOX_CROSS_GCC_SUBST_PREFIX}gcc"
		echo "to update its sb2.config to work with current version of sb2"
		echo "for example: "
		echo sb2-init $SBOX_TARGET $SBOX_CROSS_GCC_DIR/${SBOX_CROSS_GCC_SUBST_PREFIX}gcc""
		exit 1
	fi

	if [ -n "$SBOX_DIR" ]; then
		if [ -z "$SBOX_LUA_SCRIPTS" ]; then
			SBOX_LUA_SCRIPTS="$SBOX_DIR/share/scratchbox2/lua_scripts"
		fi
	else
		exit_error "Incorrect target config in ~/.scratchbox2/$SBOX_TARGET/sb2.config"
	fi

	export LD_LIBRARY_PATH SBOX_LIBSB2 SBOX_LUA_SCRIPTS SBOX_TARGET_ROOT SBOX_TARGET_COMPILER

	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

	# LD_PRELOAD will be set to SBOX_LIBSB2 by sb2-monitor.

	export PS1="[SB2 $SBOX_TARGET] \u@\h \w \$ "
}


function initialize_sb_logging()
{
	cmd_param=$1
	args_param=$2
	if [ "$SBOX_MAPPING_LOGLEVEL" != "" ]; then
		tstamp=`SBOX_DISABLE_MAPPING=1 /bin/date +%Y%m%d-%H%M`
		if [ ! -d $HOME/sb2_logs ]
		then
			SBOX_DISABLE_MAPPING=1 mkdir $HOME/sb2_logs 
			echo "Created directory $HOME/sb2_logs for log files"
		fi
		MAPPING_LOGFILE=$HOME/sb2_logs/$cmd_param.$tstamp.$$.log
		export SBOX_MAPPING_LOGFILE=$MAPPING_LOGFILE

		if [ "$SBOX_MAPPING_DEBUG" == "1" ]; then
			# log command:
			echo "##Log from $cmd_param $args_param" >$MAPPING_LOGFILE

			# log initial environment if logging is enabled
			env | sed -e 's/^/#/' >>$MAPPING_LOGFILE

			echo "Running scratchbox with these settings:"
			echo "SBOX_LIBSB2 = $SBOX_LIBSB2"
			echo "SBOX_LUA_SCRIPTS = $SBOX_LUA_SCRIPTS"
			echo "SBOX_TOOLS_ROOT = $SBOX_TOOLS_ROOT"
			echo "SBOX_REDIR_LD_SO = $SBOX_REDIR_LD_SO"
			echo "SBOX_REDIR_LD_LIBRARY_PATH = $SBOX_REDIR_LD_LIBRARY_PATH"
			echo "SBOX_TARGET_ROOT = $SBOX_TARGET_ROOT"
			echo "SBOX_MAPPING_LOGFILE = $SBOX_MAPPING_LOGFILE"
			echo "SBOX_MAPPING_LOGLEVEL = $SBOX_MAPPING_LOGLEVEL"
			echo
		fi
	fi
}

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

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

if [ "$SBOX_MAPPING_DEBUG" == "1" ]; then
	# check that loglevel is valid
	case $SBOX_MAPPING_LOGLEVEL in
	(error|warning|notice|info|debug|noise)	;; # OK
	(*) usage ;;
	esac
else
	# default logging level
	export SBOX_MAPPING_LOGLEVEL=warning
fi

# 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 [ -n "$SBOX_TOOLS_ROOT" ]; then
	SHELL=$SBOX_TOOLS_ROOT/bin/bash
else
	SHELL=/bin/bash
fi

if [ $# -gt 0 -o "$STDIN" = true ] ; then
	binary="$1"
	shift 1
	args="$@"
	initialize_sb_logging $(echo $binary | sed 's/\//_/g') "$args"
	exec sb2-monitor -x $SBOX_DIR/share/scratchbox2/scripts/sb2-exitreport \
		-- $SHELL -c "$binary $args"
else
	initialize_sb_logging sb2
	exec sb2-monitor -x $SBOX_DIR/share/scratchbox2/scripts/sb2-exitreport \
		-- $SHELL --noprofile --norc
fi

