#!/bin/bash 
# Setup & verify development structure
# should be run once when installed or when env vars change

# can be called in several conditions
# ./prj config  [no existing setenv]
#    use setenv.example to create setenv and tell user to edit to their desire
# ./prj config  [setenv file exists]
#    find/setup/verify external dependencies based on settings in setenv
# ./prj config <setenv file> 
#    use specified file as setenv and do find/setup/verify step

set -e
#set -x 

# TODO list for all ./prj config scripts
# add option to skip or fail if sudo is required
# add option to specify install target: project local, user global, or machine global
# add option to not install anything
# add option to not download anything
# add logic to download TI resources and add to download dir
# test on various Ubuntu and Debian versions
# add other distros to distro-resources
# ? allow download path to be a list
# ? allow non project local copies of uclibc source

MYDIR=$(cd $(dirname $0); pwd)
PRJ=$(dirname $MYDIR)
TOP=$(dirname $PRJ)

echo ${BASH_SOURCE[0]}
echo ${BASH_SOURCE[1]}

SETENV_FILE=$1

if [ ! -e $PRJ/scripts/setenv.example ] ; then
	echo "can't identify project directory"
	exit 1
fi

if [ -n "$SETENV_FILE" ]; then

	if [ ! -r $SETENV_FILE ] ; then
		echo "Can not read setenv file $SETENV_FILE"
		exit 2
	fi

	if [ -e $PRJ/setenv ] ; then
		mv $PRJ/setenv $PRJ/setenv.old
	fi

	cp $SETENV_FILE $PRJ/setenv
else
	if [ ! -e $PRJ/setenv ] ; then
		cp $PRJ/scripts/setenv.example $PRJ/setenv
		echo "Edit setenv for your configuration and rerun setup"
		exit 1
	fi
fi

add_boiler_plate() {
	cat <<EOF >>$PRJ/.setenv.local
SETENV_LOCAL_SOURCED=1
if [ -z "$$SETENV_SOURCED" ]; then
	source $$LINUX_C6X_TOP_DIR/linux-c6x-project/setenv
fi
EOF
}

do_find_setup() {
	rm $PRJ/.setenv.local  >/dev/null 2>&1 || true
	echo "# auto generated by $PRJ/prj config, don't edit; rerun that command" >$PRJ/.setenv.local
	echo "export LINUX_C6X_TOP_DIR=$TOP" >>$PRJ/.setenv.local
	source $PRJ/setenv
	scripts/get-distro-resources.sh
	scripts/get-gcc-resources.sh
	scripts/get-ti-resources.sh
	add_boiler_plate
}

(do_find_setup)

# now verify
source $PRJ/setenv

# is the path to the TI compiler correct?
if [ -n "$CGT_VERSION" ] && [ "$CGT_VERSION" != "none" ] ; then
	if [ -z "$TI_CG6X_DIR" -o ! -x $TI_CG6X_DIR/bin/cg6x ] ; then
		echo "TI_CG6X_DIR is \"$TI_CG6X_DIR\" and does not seem to be the TI c6x tools"
		exit 2
	fi
fi

# is the path to the GCC compiler correct?
if [ -z "$GNU_TOOLS_DIR" -o ! -x $GNU_TOOLS_DIR/bin/c6x-uclinux-gcc ] ; then
	echo "GNU_TOOLS_DIR is \"$GNU_TOOLS_DIR \" and does not seem to be the GCC tools"
	exit 2
fi

# is the top level project path correct?
if [ -z "$LINUX_C6X_TOP_DIR" -o ! -e $LINUX_C6X_TOP_DIR/linux-c6x-project/scripts/setenv.example ] ; then
	echo "LINUX_C6X_TOP_DIR is \"$LINUX_C6X_TOP_DIR\" and does not seem to be the project top"
	exit 2
fi

if [ -e $SDK0_DIR/linux-c6x-sdk0-prebuilt ] ; then
	# using prebuilt tool chain, however TI compiler may be in a different place; link it in
	if [ ! -e $SDK0_DIR/cgtools/bin -o -L $SDK0_DIR/cgtools/bin -o -L $SDK0_DIR/cgtools ] ; then
	    rm -rf $SDK0_DIR/cgtools 2>/dev/null
	    mkdir $SDK0_DIR/cgtools
	    ln -s $TI_CG6X_DIR/bin $SDK0_DIR/cgtools/bin
	else
	    echo $SDK0_DIR/cgtools appears to have a hard copy of the tools, skipping symlink
	fi
fi

echo "setup is complete"
