#!/bin/bash -e

SUPPORTED_EXPORTS="PATH LD_LIBRARY_PATH CFLAGS CXXFLAGS LDFLAGS ACLOCAL"
SUPPORTED_SHELLS="posix"

PATH="${PATH}:$(dirname $0):/usr/local/share/ui-auto:/usr/share/ui-auto"
. ui-libopt.sh

ui_opt_init "Produce shell code to taint the environment to use
ui-auto enabled projects."
ui_opt_add "d"  "Also run for the project's dependencies."
ui_opt_add "p"  "Also set the path of the project as cwd."
ui_opt_add "D"  "Only run for the project's dependencies."
ui_opt_add "I"  "Information on a project's configuration in '~/.ui-auto.conf'."
ui_opt_add "E:" "Exports to produce, given as space-separated list." "${SUPPORTED_EXPORTS}" "" "\
Supported='${SUPPORTED_EXPORTS}'."
ui_opt_add "S:" "Shell syntax to use. Supported: ${SUPPORTED_SHELLS}." "posix"
ui_opt_addPos "ID" "A project id as configured in '~/.ui-auto.conf'."
ui_opt_parse "$@"

checkCall()
{
	for e in ${EXPORTS}; do
		local ok=false
		for s in ${SUPPORTED_EXPORTS}; do
			if test "${e}" = "${s}"; then
				ok=true
				break;
			fi
		done
		if ! ${ok}; then
			ui_opt_error "Unsupported export: ${e}" HELP
		fi
	done

	if test ! -e "${PROJECT_CONF}"; then
		ui_opt_error "${PROJECT_CONF} not found"
	fi
}

iset()
{
	eval $1="\${2}"
}

addItem()
{
	local var="${1}"
	local item="${2}"
	local sep=$(test -z "${!var}" -o -z "${item}" || echo -n "${3}")
	local append="${4}"
	local eregex="(^|[${sep}])${item}($|[${sep}])"
	if echo -n "${!var}" | egrep --quiet -e "${eregex}"; then
		echo "NOTE: \"${item}\" already included in $var, skipping." >&2
	else
		if test "${append}" = "append"; then
			iset $var "${!var}${sep}${item}"
		else
			iset $var "${item}${sep}${!var}"
		fi
	fi
}

calcEnv()
{
	for f in ${ui_env_library_paths}; do
		addItem LD_LIBRARY_PATH "${PROJECT_PATH}/${f}" ":"
		addItem LDFLAGS "-L${PROJECT_PATH}/${f}" " "
	done

	for f in ${ui_env_include_paths}; do
		addItem CFLAGS   "-I${PROJECT_PATH}/${f}" " "
		addItem CXXFLAGS "-I${PROJECT_PATH}/${f}" " "
	done

	for f in ${ui_env_program_paths}; do
		addItem PATH "${PROJECT_PATH}/${f}" ":"
	done

	if test "${ui_env_m4_macro_paths}" -a "x$ACLOCAL" = "x"; then
		iset ACLOCAL "aclocal"
	fi
	for f in ${ui_env_m4_macro_paths}; do
		addItem ACLOCAL "-I${PROJECT_PATH}/${f}" " " append
	done
}

serialCall()
{
	local list="${1}"
	tmp=$(mktemp /tmp/ui-auto-env.XXXXXX)
	trap "rm -f ${tmp}" EXIT
	for p in ${list}; do
		${0} "${p}" >"${tmp}"
		eval $(cat ${tmp})
	done
	cat "${tmp}"
}

# Processing starts here
if ! . ~/.ui-auto.conf; then
	ui_opt_error " Syntax wrong (or no) ~/.ui-auto.conf file; please configure first"
fi
ID=$(ui_opt_getPos 0 | tr "-" "_")
ID_LOC="${ID}_loc"
if [ -z "${!ID_LOC}" ]; then
	echo -e "ID $ID not configured in ~/.ui-auto.conf.\n" >&2
	exit 1
fi

ID_DEPS="${ID}_deps"

if ui_opt_given p; then
	echo "cd \"${!ID_LOC}\";"
fi

if ui_opt_given C; then
	# Check project only
	echo "Project     : ${ID}"
	echo "Location    : ${!ID_LOC}"
	echo "Dependencies: ${!ID_DEPS}"
	exit 0
elif ui_opt_given d; then
	serialCall "${!ID_DEPS} ${ID}"
	exit 0
elif ui_opt_given D; then
	serialCall "${!ID_DEPS}"
	exit 0
fi

# Single run
cd "${!ID_LOC}"
PROJECT_PATH="$(pwd)"

PROJECT_CONF="${PROJECT_PATH}/.ui-auto.conf"
EXPORTS=$(ui_opt_get E)
SHELL=$(ui_opt_get S)

checkCall
. "${PROJECT_CONF}"
calcEnv
for e in ${EXPORTS}; do
	case ${SHELL} in
		posix)
			if test "${!e}"; then
				echo "${e}=\"${!e}\"; export ${e};"
			fi
			;;
		*)
			ui_opt_error "Unsupported shell: ${SHELL}" HELP
	esac
done

exit 0
