#!/bin/bash

# (C) Copyright 2011  DENX Software Engineering GmbH
#
# Licensed under the GPLv2 or later.

ELDK_VERSION="5.0"
default_path="/opt/eldk-${ELDK_VERSION}"

# Available image types (first in list is default)
IMAGES="gmae qte"

tmp=""
DEF_IMAGE=""
for i in ${IMAGES} ; do
	if [ -z "$tmp" ] ; then
		tmp="\"$i\""
		DEF_IMAGE="$i"
	else
		tmp="$tmp, \"$i\""
	fi
done

usage() {
	my_name=$(basename $0)
	cat >&2 <<E_O_F
Usage: ${my_name} [-d <destination directory>] [-i <image type>] [-s] [-r] [<target>] [<sdk_arch>]
-d: Destination defaults to "${default_path}"; you can override it
    but you will need to make software 'think' that it is installed
    into the default location (use symlink or namespaces or ...)
-i: Image type; select one of $tmp; defaults to "${DEF_IMAGE}"
-s: Install (only) the SDK
-r: Install (only) the target root file system
    If neither '-s' nor '-r' are given, the default is to install both.
target defaults to "powerpc"
sdk_arch defaults to "i686"
E_O_F
}

src_dir=$(dirname $0)
dest_path=""
image_type=""
inst_rootfs=""
inst_sdk=""

while getopts ":d:i:rsh" option; do
	case "$option" in
	d) dest_path="$OPTARG" ;;
	i) for i in ${IMAGES} ; do
		if [ "$i" = "$OPTARG" ] ; then
			image_type="$OPTARG"
			break
		fi
	   done
	   if [ -z "${image_type}" ] ; then
		echo "Error: unknown image type '$OPTARG'" >&2
		usage
		exit 1
	   fi
	   ;;
	r) inst_rootfs=yes ;;
	s) inst_sdk=yes ;;
	h) usage ; exit 0 ;;
	*) echo "Error: unknown option '$OPTARG'" >&2 ; usage ; exit 1 ;;
	esac
done
shift $((OPTIND - 1))

if [ $# -gt 2 ] ; then
	echo "Error: too many argumentes ($#, max is 2)" >&2
	usage
	exit 1
fi

: ${dest_path:="${default_path}"}
: ${image_type:="${DEF_IMAGE}"}

if [ \( -z "$inst_rootfs" \) -a \( -z "$inst_sdk" \) ] ; then
	inst_rootfs=yes
	inst_sdk=yes
fi

if [ "$inst_rootfs" ] ; then
	cat >&2 <<E_O_F
--------------------------------------------------------------------
NOTICE: superuser priviledges will be needed to install the
root file system; make sure you have sufficient permissions.
--------------------------------------------------------------------
E_O_F
fi

# First argument is the target name (defaults to powerpc)
target=${1:-"powerpc"}

# try to include target configuration
target_conf="${src_dir}/targets/${target}/target.conf"

if [ ! -r ${target_conf} ] ; then
	echo "Error: can't read target config file \"${target_conf}\"" >&2
	exit 1
fi

. ${target_conf}

# Currently we support only i686 and x86_64 hosts
cur_arch=$(uname -m)

case ${cur_arch} in
i686) ;;
x86_64) ;;
*)
	echo "You are running neither on i686 nor x86_64 machine" >&2
	echo "Your machine is not supported" >&2
	exit 1
	;;
esac

# Second argument is the SDK type (i686/x86_64) (defaults to i686)
SDK_ARCH=${2:-i686}
SDK_OS="linux"
SDK_VENDOR="-oesdk"

case ${SDK_ARCH} in
i686) ;;
x86_64) ;;
*)
	echo "Error: SDK architecture \"${SDK_ARCH}\" is not supported" >&2
	exit 1
	;;
esac

case ${image_type} in
gmae)	sdk_type="gmae"
	rootfs_type="sato"
	;;
qte)	sdk_type="qte"
	rootfs_type="qte"
	;;
*)	echo "INTERNAL ERROR: unknown image type \"${image_type}\"" >&2
	exit 1
	;;
esac

if [ "${dest_path}" != "${default_path}" ] ; then
	cat >&2 <<E_O_F
--------------------------------------------------------------------
WARNING! You are installing to "${dest_path}" which is non-standard.
Unfortunately, the host path of the ELDK is NOT relocatable so for
ELDK to work correctly you will need to make the default path point
to the place you are installing to (by means of symlink or
name spaces).
--------------------------------------------------------------------
E_O_F
fi

# tarballs to install
sdk_tarball=${src_dir}/targets/${target}/eldk-eglibc-${SDK_ARCH}-${TARGET_ARCH}-toolchain-${sdk_type}-${ELDK_VERSION}.tar.bz2
rootfs_tarball=${src_dir}/targets/${target}/poky-image-${rootfs_type}-sdk-${MACHINE}.tar.gz

inst_path="${dest_path}/${MACHINE}"

if [ "$inst_rootfs" ] ; then
	if [ ! -r "${rootfs_tarball}" ] ; then
		echo "Error: Can't read rootfs tarball \"${rootfs_tarball}\"" >&2
		exit 1
	fi

	rootfs_path="${inst_path}/rootfs"

	if [ -e ${rootfs_path} ] ; then
		echo "Error: installation directory \"${rootfs_path}\" already exists" >&2
		exit 1
	fi

	mkdir -p ${rootfs_path} || exit 1
	echo "*** Installing ${rootfs_tarball}" >&2
	sudo tar xzf ${rootfs_tarball} -C ${rootfs_path}
fi

if [ "$inst_sdk" ] ; then
	if [ ! -r "${sdk_tarball}" ] ; then
		echo "Error: Can't read SDK tarball \"${sdk_tarball}\"" >&2
		exit 1
	fi

	sdk_path="${inst_path}/sysroots"

	if [ -e ${sdk_path} ] ; then
		echo "Error: installation directory \"${sdk_path}\" already exists" >&2
		exit 1
	fi

	mkdir -p ${inst_path} || exit 1
	tmp_dir=$(mktemp -d ${dest_path}/eldk_install.XXXXX)
	trap 'rm -fr $tmp_dir' 0 1 2 3 15

	echo "*** Installing ${sdk_tarball}" >&2
	tar xjf ${sdk_tarball} -C ${tmp_dir} || exit 1
	mv ${tmp_dir}/${default_path}/${MACHINE}/* ${inst_path}/
fi

exit 0
