# building with bash

my_source_d="$(pwd)/"

if test ! -d ${my_project_d}c_src/ ; then
	echo "The current directory isn't right," >&2
	echo "the 'c_src' folder doesn't exist." >&2
	exit 1
fi

#----------------------
# find output directory

to_relative=yes
my_output_d="output"
if test $# -ge 2; then
	if test $1 = "-to"; then
		to_relative=no
		my_output_d="$2";
		shift 2
	elif test $1 = "-torel"; then
		my_output_d="$2";
		shift 2
	fi
fi

if test ! -d "${my_output_d}" ; then
	mkdir "${my_output_d}"
	fresh_output=yes
else
	fresh_output=no
fi
cd "${my_output_d}"

my_output_d="$(pwd)/"

#--------------------
# get setup directory

if test ! -d "setup" ; then
	mkdir "setup"
fi
cd "setup"

my_setup_d="$(pwd)/"

echo -n > "${my_setup_d}setupopt.h"

cd "${my_source_d}"

#-------------------------
# parse rest of parameters

do_compile=yes
keep_setup=no
keep_make=no
use_IDE=auto
use_Targ=auto
use_Script=auto
be_quiet=no

while test $# -gt 0; do
	cur_arg="$1"
	shift
	case "$cur_arg" in
		-nocompile)
			do_compile=no
			;;
		-keepsetup)
			keep_setup=yes
			;;
		-keepmake)
			keep_make=yes
			;;
		-quiet)
			be_quiet=yes
			;;
		-ds)
			echo "#define $1 \"$2\"" >> "${my_setup_d}setupopt.h"
			shift 2
			;;
		-d)
			echo "#define $1 $2" >> "${my_setup_d}setupopt.h"
			shift 2
			;;
		-ide)
			if test "${use_IDE}" = auto; then
				use_IDE="UseIde$1"
				shift
			else
				echo "only one UseIde parameter allowed" >&2
				exit 1
			fi
			;;
		-targ)
			if test "${use_Targ}" = auto; then
				use_Targ="UseTarg$1"
				shift
			else
				echo "only one UseTarg parameter allowed" >&2
				exit 1
			fi
			;;
		-script)
			if test "${use_Script}" = auto; then
				use_Script="UseScript$1"
				shift
			else
				echo "only one UseScript parameter allowed" >&2
				exit 1
			fi
			;;
		-*)
			echo "unknown option = $cur_arg" >&2
			exit 1
			;;
		*)
			echo "#define $cur_arg 1" >> "${my_setup_d}setupopt.h"
			;;
  	esac
done

if test "${be_quiet}" = no; then
	echo "building with bash"
	echo "my_source_d = ${my_source_d}"
	echo "my_output_d = ${my_output_d}"
fi

my_cc=gcc
if test -z "`whereis ${my_cc}`"; then
	my_cc=cc
	if test -z "`whereis ${my_cc}`"; then
		echo "c compiler not found"
	fi
fi

if test "${use_IDE}" = auto; then
	use_IDE="UseIdeBashGcc"
fi

if test "${use_Targ}" = auto; then
	if test -d "/System/Library/Frameworks/Carbon.framework" ; then
		if test "${be_quiet}" = no; then
			echo "This looks like Mac OS X"
		fi

		use_Targ="UseTargMacho"
	else
		if test "${be_quiet}" = no; then
			echo "Use Generic X11"
		fi

		use_Targ="UseTargLinuxx86"
	fi
fi

if test "${use_Script}" = auto; then
	use_Script="UseScriptBash"
fi

if test "${to_relative}" = yes; then
	echo "#define TheUseAbsolute 0" >> "${my_setup_d}setupopt.h"
fi
echo "#define ${use_Script} 1" >> "${my_setup_d}setupopt.h"
echo "#define ${use_IDE} 1" >> "${my_setup_d}setupopt.h"
echo "#define ${use_Targ} 1" >> "${my_setup_d}setupopt.h"

#-----------

${my_cc} "${my_source_d}build/setup.c" -o "${my_setup_d}setup" -Wall -Wno-unused-function -I"${my_setup_d}" -I"${my_source_d}"
if test $? -ne 0 ; then
	exit 1
fi

"${my_setup_d}setup" > "${my_setup_d}setup.sh"

if test "${use_Script}" = UseScriptBash; then

	if test "${to_relative}" = yes; then
		bash "${my_setup_d}setup.sh" "${my_output_d}"
	else
		bash "${my_setup_d}setup.sh" "${my_output_d}" "${my_source_d}"
	fi
	if test $? -ne 0 ; then
		exit 1
	fi

	if test "${keep_setup}" = no; then
		rm -rf "${my_setup_d}"
	fi

	if test "${use_IDE}" = UseIdeBashGcc; then
		cd "${my_output_d}"

		my_make=make
		if test "${be_quiet}" = yes; then
			my_make="${my_make} -s"
		fi

		#-----------------------------------
		# get rid of previous compiles which
		# may have used different options

		if test "${fresh_output}" = no; then
			${my_make} clean
		fi

		#----------------
		# make if desired

		if test "${do_compile}" = yes; then
			if test "${be_quiet}" = no; then
				echo "Make"
			fi

			${my_make}

			if test "${keep_make}" = no; then
				rm -f "${my_output_d}Makefile"
				rm -rf "${my_output_d}config"
				rm -rf "${my_output_d}obj"
			fi
		fi

		cd "${my_source_d}"
	fi
fi

#---------

if test "${be_quiet}" = no; then
	echo "Done"
fi
