#!/bin/bash

AWK=awk
GREP=grep
SED=sed
MAKE=make
DEFCONFIG=defconfig
CONFIG=.config
CONFIGURE_STATUS=.pathcache
out=$CONFIGURE_STATUS

srcdir=$(dirname $(readlink -f $0))/
builddir=$(pwd)/

prefix=/usr/local
function argument {
	echo ${1} | ${AWK} 'BEGIN { FS = "="} {print $2}'
}

function help {
	echo "${0} [options]"
	printf "\nConfiguration:\n"
	printf "\t--help\t\t\tdisplay this help and exit\n"
	printf "\nInstallation directories:\n"
	printf "\t--prefix=PREFIX\t\tinstall architecture-independent files in PREFIX [$prefix]\n"
	printf "\t--exec-prefix=PREFIX\tinstall architecture-dependent files in PREFIX [\$prefix]\n"
	printf "\t--bindir=DIR\t\tuser executables in DIR [\$exec-prefix/bin]\n"
	printf "\t--sbindir=DIR\t\tsystem admin executables in DIR [\$exec-prefix/sbin]\n"
	printf "\t--sysconfdir=DIR\tread-only single-machine data in DIR [\$prefix/etc]\n"
	printf "\t--libdir=DIR\t\tobject code libraries in DIR [\$exec-prefix/lib]\n"
	printf "\t--includedir=DIR\tC header files in DIR [\$prefix/include]\n"
	printf "\t--datadir=DIR\t\tread-only architecture-independent data files in DIR[\$prefix/share]\n"
	printf "\t--localstatedir=DIR\trunning data files in DIR[\$prefix/var]\n"
	printf "\nSystem types:\n"
	printf "\t--build=BUILD\t\tconfigure for building on BUILD [guessed]\n"
	printf "\t--host=HOST\t\tcross-compile to build programs to run on HOST [BUILD]\n"
	printf "\t--target=<host>\t\tcross-compile to build programs to run on HOST [BUILD]\n"
	printf "\t--sysroot=DIR\t\tcross-compiler root directory [none]\n"
	printf "\t--toolchain=DIR\t\tcross-compiler tools path [none]\n"
	printf "\nFeatures enabling:\n"
	for option in $OPTIONS
	do
		default=$(echo $option | ${SED} 's/_/-/g' | ${SED} 's/=/ /g' | ${AWK} '{print $2}')
		if [ "$default" == "y" -o  "$default" == "n" ]; then
			enable=$(echo $option | ${SED} 's/_/-/g' | ${SED} 's/=/ /g' | ${AWK} '{print "--enable-"tolower($1)}')
			if [ $(echo $enable | wc -c) -ge 17 ]; then
				printf "\t$enable\tdefault:$default\n"
			else
				printf "\t$enable\t\tdefault:$default\n"
			fi
		fi
	done
	printf "\nFeatures:\n"
	for option in $OPTIONS
	do
		default=$(echo $option | ${SED} -e 's/_/-/g' -e 's/=/ /g' -e 's/:.*)/)/' | ${AWK} '{print $2}')
		if [ "$default" != "y" -a  "$default" != "n" ]; then
			with=$(echo $option | ${SED} -e 's/_/-/g' -e 's/=/ /g' | ${AWK} '{print "--with-"tolower($1)}')
			printf "\t$with=<value>\tdefault:$default\n"
		fi
	done
	exit
}

if [ -e ${srcdir}${DEFCONFIG} ]; then
  OPTIONS=$(cat ${srcdir}${DEFCONFIG} | ${SED} 's/\#.*//g')
fi

error=n

while [ "${1}" != "" ]; do
	case ${1} in
		--prefix*)
			prefix=$(argument ${1})
			;;
		--exec-prefix*)
			exec_prefix=$(argument ${1})
			;;
		--program-prefix*)
			program_prefix=$(argument ${1})
			;;
		--bindir*)
			bindir=$(argument ${1})
			;;
		--sbindir*)
			sbindir=$(argument ${1})
			;;
		--includedir*)
			includedir=$(argument ${1})
			;;
		--libdir*)
			libdir=$(argument ${1})
			;;
		--pkglibdir*)
			pkglibdir=$(argument ${1})
			;;
		--datadir*)
			datadir=$(argument ${1})
			;;
		--pkgdatadir*)
			pkgdatadir=$(argument ${1})
			;;
		--sysconfdir*)
			sysconfdir=$(argument ${1})
			;;
		--localstatedir*)
			localstatedir=$(argument ${1})
			;;
		--host*)
			CROSS_COMPILE=$(argument ${1})
			;;
		--target*)
			CROSS_COMPILE=$(argument ${1})
			;;
		--build*)
			HOST_COMPILE=$(argument ${1})
			;;
		--sysroot*)
			SYSROOT=$(argument ${1})
			;;
		--toolchain*)
			TOOLCHAIN=$(argument ${1})
			;;
		--disable-error)
			error=disable
			;;
		--disable-check)
			check=disable
			;;
		--help*)
			help
			;;
		--enable-*)
			name=$(echo ${1} | ${SED} 's/--enable-//g' | ${SED} 's/-/_/g' | ${AWK} '{print toupper($1)}')
			ENABLEOPTIONS="${ENABLEOPTIONS} $name"
			;;
		--disable-*)
			name=$(echo ${1} | ${SED} 's/--disable-//g' | ${SED} 's/-/_/g' | ${AWK} '{print toupper($1)}')
			DISABLEOPTIONS="${DISABLEOPTIONS} $name"
			;;
		--with-libtool-*)
			name=$(echo ${1} | ${SED} 's/--with-//g' | ${SED} 's/-/_/g' | ${AWK} -F= '{print toupper($1)}')
			value=$(echo ${1} | ${SED} 's/--with-//g' | ${SED} 's/-/_/g' | ${AWK} -F= '{print $2}')
			WITHOPTIONS="${WITHOPTIONS} $name"
			eval $name="$value"
			;;
		--with-*)
			name=$(echo ${1} | ${SED} 's/--with-//g' | ${SED} 's/-/_/g' | ${AWK} -F= '{print toupper($1)}')
			value=$(echo ${1} | ${SED} 's/--with-//g' | ${SED} 's/-/_/g' | ${AWK} -F= '{print $2}')
			WITHOPTIONS="${WITHOPTIONS} $name"
			eval $name="$value"
			;;
		*)
			error=${1}
			;;
	esac
	shift
done

if [ $error != n  -a $error != disable ]; then
	printf "\x1B[31m\n\tbad argument: ${error}\n\n\x1B[0m"
	help
fi

echo "#Makemore 1.0" > ${builddir}$out
printf "\nInstallation:\n"
if [ -n "$prefix" ]; then
	echo "prefix="$prefix"" >> ${builddir}$out
	printf " prefix:\t$prefix\n"
fi
if [ -n "${exec_prefix}" ]; then
	echo "exec-prefix="${exec_prefix}"" >> ${builddir}$out
	printf " exec-prefix:\t${exec_prefix}\n"
fi
if [ -n "$bindir" ]; then
	echo "bindir="$bindir"" >> ${builddir}$out
	printf " bindir:\t$bindir\n"
fi
if [ -n "$sbindir" ]; then
	echo "sbindir="$sbindir"" >> ${builddir}$out
	printf " sbindir:\t$sbindir\n"
fi
if [ -n "$includedir" ]; then
	echo "includedir="$includedir"" >> ${builddir}$out
	printf " includedir:\t$includedir\n"
fi
if [ -n "$libdir" ]; then
	echo "libdir="$libdir"" >> ${builddir}$out
	printf " libdir:\t$libdir\n"
fi
if [ -n "$sysconfdir" ]; then
	echo "sysconfdir="$sysconfdir"" >> ${builddir}$out
	printf " sysconfdir:\t$sysconfdir\n"
fi
if [ -n "$datadir" ]; then
	echo "datadir="$datadir"" >> ${builddir}$out
	printf " datadir:\t$datadir\n"
fi
if [ -n "$pkgdatadir" ]; then
	echo "pkgdatadir="$pkgdatadir"" >> ${builddir}$out
	printf " pkgdatadir:\t$pkgdatadir\n"
fi
if [ -n "$pkglibdir" ]; then
	echo "pkglibdir="$pkglibdir"" >> ${builddir}$out
	printf " pkglibdir:\t$pkglibdir\n"
fi
if [ -n "$SYSROOT" ]; then
	echo "SYSROOT=$SYSROOT" >> ${builddir}$out
	printf " sysroot:\t$SYSROOT\n"
fi
if [ -n "$CROSS_COMPILE" ]; then
	echo "CROSS_COMPILE=$CROSS_COMPILE" >> ${builddir}$out
	printf " host:\t$CROSS_COMPILE\n"
fi
if [ -n "$TOOLCHAIN" ]; then
	echo "TOOLCHAIN=$TOOLCHAIN" >> ${builddir}$out
	printf " toolchain:\t$TOOLCHAIN\n"
fi
if [ -n "$CC" ]; then
	echo "CC=$CC" >> ${builddir}$out
fi
if [ -n "$CFLAGS" ]; then
	echo "CFLAGS=$CFLAGS" >> ${builddir}$out
fi
if [ -n "$CXX" ]; then
	echo "CXX=$CXX" >> ${builddir}$out
fi
if [ -n "$CXXFLAGS" ]; then
	echo "CXXFLAGS=$CXXFLAGS" >> ${builddir}$out
fi
if [ -n "$LD" ]; then
	echo "LD=$LD" >> ${builddir}$out
fi
if [ -n "$LDFLAGS" ]; then
	echo "LDFLAGS=$LDFLAGS" >> ${builddir}$out
fi
if [ -n "$AS" ]; then
	echo "AS=$AS" >> ${builddir}$out
fi
if [ -n "$AR" ]; then
	echo "AR=$AR" >> ${builddir}$out
fi
if [ -n "$RANLIB" ]; then
	echo "RANLIB=$RANLIB" >> ${builddir}$out
fi
if [ -n "$CC_FOR_BUILD" ]; then
	echo "HOSTCC=$CC_FOR_BUILD" >> ${builddir}$out
fi
if [ -n "$CFLAGS_FOR_BUILD" ]; then
	echo "HOSTCFLAGS=$CFLAGS_FOR_BUILD" >> ${builddir}$out
fi
if [ -n "$CXX_FOR_BUILD" ]; then
	echo "HOSTCXX=$CXX_FOR_BUILD" >> ${builddir}$out
fi
if [ -n "$CXXFLAGS_FOR_BUILD" ]; then
	echo "HOSTCXXFLAGS=$CXXFLAGS_FOR_BUILD" >> ${builddir}$out
fi
if [ -n "$LD_FOR_BUILD" ]; then
	echo "HOSTLD=$LD_FOR_BUILD" >> ${builddir}$out
fi

echo "#Makemore 1.0" > ${builddir}${CONFIG}
printf "\nConfiguration:\n"
if [ -n "$OPTIONS" ]; then
	for option in $OPTIONS
	do
		name=$(echo ${option} | ${AWK} -F= '{print $1}')
		value=$(echo ${option} | ${SED} "s/$name*=//")
		found=$(echo $ENABLEOPTIONS | grep -w $name)
		if [ x"$found" != x"" ]; then
			value=y
		fi
		found=$(echo $DISABLEOPTIONS | grep -w $name)
		if [ -n "$found" ]; then
			value=n
		fi
		found=$(echo $WITHOPTIONS | grep -w $name)
		if [ x"$found" != x"" ]; then
			value=${!name}
		fi
		printf "  $name:\t$value\n" 2> /dev/null
		echo $name=$value >> ${builddir}${CONFIG}
	done
else
	for name in $ENABLEOPTIONS
	do
		printf "  $name:\ty\n"
		echo $name=y >> ${builddir}${CONFIG}
	done
	for name in $DISABLEOPTIONS
	do
		printf "  $name:\tn\n"
		echo $name=n >> ${builddir}${CONFIG}
	done
	for name in $DISABLEOPTIONS
	do
		value=${!name}
		printf "  $name:\t$value\n"
		echo $name=$value >> ${builddir}${CONFIG}
	done
fi

printf "\nDependencies checking:\n"
if [ "$check" != "disable" ]; then
	${MAKE} --no-print-directory BUILDDIR=${builddir} -C${srcdir} check
	if [ $? != 0 ]; then
		echo "binary dependencies failed"
		exit -1
	fi
fi

if [ ! -e ${builddir}Makefile ]; then
	printf '#Generated Makefile\n' > ${builddir}Makefile
	printf 'goal:=$(filter-out Makefile,$(MAKECMDGOALS))\n' >> ${builddir}Makefile
	printf 'goals:=$(if $(goal),$(goal),all)\n' >> ${builddir}Makefile
	printf '$(goals):\n' >> ${builddir}Makefile
	printf '\t@make BUILDDIR='${builddir}' -C'${srcdir}' $@\n' >> ${builddir}Makefile
fi
