#!/bin/sh

PHYLINUX_API_VERSION="2"

# Try to find ROOTDIR from arg0 of shell process.
DIR="`dirname $(readlink -f $0)`"

# Try to find ROOTDIR of the Yocto BSP. Walk up the directory tree until we
# find the sources/meta-phytec or .repo directory. Returns the empty string as
# an error code.
find_root_dir() {
	dir=$(readlink -f "$1")  # should return an absoulte path
	while [ ! "$dir" = "/" ]; do
		if [ -d "$dir/.repo" ]; then
		# or [ -d "$dir/sources/meta-phytec" ];
			echo $dir;
			return;
		fi
		dir=$(dirname "$dir")
	done
	# If anchor directory isn't found, function returns the empty strings
	# as an error code.
}

ROOTDIR=$(find_root_dir "$DIR")
if [ "$ROOTDIR" = "" ]; then
	echo >&2 "ERROR: Cannot find root directory of the Yocto BSP."
        echo >&2 "Is '$DIR' in a checkout of a BSP? Aborting..."
	exit 1;
fi


PHYTEC_DIR="${ROOTDIR}/sources/meta-phytec"

cp ${ROOTDIR}/sources/meta-imx/EULA.txt ${ROOTDIR}/sources/meta-freescale/EULA

${PHYTEC_DIR}/scripts/init
