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


function usage()
{
	cat <<EOF
sb2-init - initialize a target root for scratchbox2
Usage:
	sb2-init [OPTION]... [TARGETNAME] [COMPILER]

sb2-init is expected to be run in the directory you want
to use as scratchbox 2 target root.

TARGETNAME is what you want to call this target
COMPILER is of the form $HOME/arm-2006q3/bin/arm-linux-gcc


Options:
    -c [qemu | sbrsh] select cpu transparency method, qemu is default
    -d                set target as default scratchbox2 target
    -h                print this help
    -n                don't build libtool for the target
    -s                skip checks for target root's /usr/include etc.
    -v                display version

Examples:
    sb2-init ARM arm-linux-gcc
    sb2-init -sn -c sbrsh armel-debian /path/to/arm-linux-gcc
EOF
	exit 2
}

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


function write_target_config()
{
	cat - > $HOME/.scratchbox2/$TARGET/sb2.config <<EOF
# Scratchbox2 configuration file generated by sb2-init.

export SBOX_TARGET_ROOT=$SBOX_TARGET_ROOT
export SBOX_CPU=$ARCH
export SBOX_GCC_TARGET=$GCC_TARGET
export SBOX_CPUTRANSPARENCY_METHOD=$QEMU
export SBOX_UNAME_MACHINE=$ARCH

export SBOX_DEFAULT_GCC_PREFIX=$GCC_PREFIX

export SBOX_CROSS_GCC_NAME=cross-gcc
export SBOX_CROSS_GCC_PREFIX_LIST=$GCC_TARGET-:$ARCH-linux-:$GCC_PREFIX
export SBOX_CROSS_GCC_SUBST_PREFIX=$GCC_PREFIX
export SBOX_CROSS_GCC_SPECS_FILE=
export SBOX_CROSS_GCC_DIR=$GCC_PATH
export SBOX_CROSS_GCC_LD_ARGS=

export DEB_BUILD_ARCH_CPU=$DEBIAN_CPU
export DEB_BUILD_ARCH=$DEBIAN_CPU
export DEB_BUILD_GNU_CPU=$ARCH
export DEB_BUILD_GNU_TYPE=$ARCH-linux-gnu

export DEB_HOST_ARCH_CPU=$DEBIAN_CPU
export DEB_HOST_ARCH=$DEBIAN_CPU
export DEB_HOST_GNU_CPU=$ARCH
export DEB_HOST_GNU_TYPE=$ARCH-linux-gnu

export SBOX_HOST_GCC_NAME=host-gcc
export SBOX_HOST_GCC_PREFIX_LIST=host-
export SBOX_HOST_GCC_SUBST_PREFIX=
export SBOX_HOST_GCC_SPECS_FILE=
export SBOX_HOST_GCC_DIR=/usr/bin
export SBOX_HOST_GCC_LD_ARGS=
EOF
	echo "Finished writing sb2.config"
}

function check_buildroot_sanity()
{
	a_ok=1

	if [ ! -e usr/include/stdio.h ]; then
		echo "no usr/include/stdio.h"
		a_ok=0
	fi

	if [ ! -e lib/libc.so.6 ] && [ ! -e lib/libc.so.0 ]; then
		echo "no lib/libc.so.6 or lib/libc.so.0"
		a_ok=0
	fi

	if [ ! -e usr/lib/libc.so ]; then
		echo "no usr/lib/libc.so"
		a_ok=0
	fi
	
	if [ $a_ok == 1 ]; then
		true
	else
		echo "Your buildroot seems to lack basic essentials like headers 
or c-library. You should probably get either a ready rootfs tarball or
copy the necessary files from your toolchain into place. After doing that
you can re-run this script."
		exit 1
	fi
}

SBOX_DIR=$(readlink -f $(dirname $_)/..)

set_as_default=0
cputransparency=qemu
with_libtool=1
skip_checks=false

if [ -z "$*" ]; then
	usage
fi

while getopts c:dhnsv foo
do
	case $foo in
	(c) cputransparency=$OPTARG ;;
	(d) set_as_default=1 ;;
	(h) usage ;;
	(n) with_libtool=0 ;;
	(s) skip_checks=true ;;
	(v) version ;;
	(*) usage ;;
	esac
done
shift $(($OPTIND - 1))

TARGET=$1
SBOX_TARGET_ROOT=$PWD
GCC=$2


GCC_FULLPATH=$(which $GCC)
# test that gcc exists and can be executed
if [ $? != 0 ]; then
	echo "$GCC doesn't exist"
	exit 1
fi
GCC_PATH=$(dirname $(which $GCC))
if [ $GCC -v > /dev/null 2>&1 != 0 ]; then
	echo "Invalid compiler specified: $GCC"
	exit 1
fi

GCC_PREFIX=$(basename $GCC | sed 's/-gcc$/-/')
ARCH=$($GCC -dumpmachine | awk -F- '{ print $1 }')
GCC_TARGET=$($GCC -dumpmachine)
DEBIAN_CPU=$ARCH

case "$ARCH" in
	arm*) 
		echo $GCC_TARGET | grep -q -i eabi
		if [ $? == 0 ]; then
			DEBIAN_CPU=armel
		fi
		;;
	ppc*) ;;
	mips*) ;;
	*)
		echo "Unsupported target architecture: $ARCH"
		echo "You must add support for it into preload/sb_exec.c"
		echo "and utils/sb2-init"
		exit 1
		;;

esac


QEMU=$(which qemu-$ARCH)
if [ ! $skip_checks -a $? != 0 ]; then
	echo "You don't have qemu-$ARCH installed."
	exit 1
fi

if [ -z "$GCC" -o -z "$TARGET" ]; then
	usage
	exit 1
fi

if [ ! $skip_checks ]; then
	check_buildroot_sanity
fi




mkdir -p $HOME/.scratchbox2

if [ -z "$(sb2-config -l)" ]; then
# force this as default anyway as there are no
# other existing targets
	set_as_default=1
fi

mkdir -p $HOME/.scratchbox2/$TARGET/bin
write_target_config

if [ $set_as_default == 1 ]; then
	sb2-config -d $TARGET
fi

if [ $with_libtool == 1 ]; then
	$SBOX_DIR/bin/sb2 -t $TARGET $SBOX_DIR/bin/sb2-build-libtool

	if [ $? == 0 ]; then
		echo "sb2-init completed successfully, have fun!"
	else
		echo "Running $SBOX_DIR/bin/sb2-build-libtool failed"
		echo "You can run this manually later, otherwise your"
		echo "sb2 environment is correctly setup and ready to use"
	fi
fi
