#!/bin/sh
# -----------------
# einit configure script
# -----------------
# tiny configure script... i might consider adding more on this some other time...
# (i'm just trying to make sure I don't violate any licenses by specifically not using any non-LGPL GPL tools)
# -----------------
# Copyright (c) 2006, Magnus Deininger
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that
# the following conditions are met:
#
#    * Redistributions of source code must retain the above copyright notice, this list of conditions and the
#      following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
#      the following disclaimer in the documentation and/or other materials provided with the distribution.
#    * Neither the name of the project nor the names of its contributors may be used to endorse or promote
#      products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -----------------

alias source=.

if [ "$(readlink /bin/sh)" != "dash" ]; then
alias echo="echo -e"
fi

BUILDNUMBER=`cat buildnumber`
if test -d .svn; then
	ISSVN=1
	echo "configuring eINIT-svn-${BUILDNUMBER}..."
else
	ISSVN=0
	echo "configuring eINIT-${BUILDNUMBER}..."
fi

BOOTSTRAP_MODULE_PATH="/lib/einit/bootstrap/"

SOURCESUFFIXES="c s c++ S"
HEADERSUFFIXES="h"

COMPILER=gnu

GCCFLAGS="-Wall"

MAKEFILETASKS="all install"

LIBRARIES="-L/lib -L/usr/lib -L/usr/local/lib"

pwd=$(pwd)
nomodule=""

XCFLAGS="$XCFLAGS -DPOSIXREGEX";

DBUS=""

. ./configure.options

echo -n "checking options [";
for param in $@; do
	short=${param%%=*};
	shortd=${param%%--no-*};
	shorte=${param%%--enable-*};
	if test "$short" = "--internal-modules"; then
		echo -n " INTERNAL";
		INTERNALMODULES=${param#*=};
	elif test "$short" = "--external-modules"; then
		echo -n " EXTERNAL";
		EXTERNALMODULES=${param#*=};
	elif test "$short" = "--extra-object"; then
		echo -n " XOBJECT";
		COREOBJECTS="${COREOBJECTS} ${param#*=}";
		UTILOBJECTS="${UTILOBJECTS} ${param#*=}";
	elif test "$short" = "--prefix"; then
		echo -n " PREFIX";
		PREFIX=${param#*=};
	elif test "$short" = "--asm-functions"; then
		echo -n " ASM";
		ASMFUNCTIONS=${param#*=};
	elif test "$short" = "--bootstrap-module-path"; then
		echo -n " BOOTSTRAPMODULEPATH";
		BOOTSTRAP_MODULE_PATH=${param#*=};
	elif test "$short" = "--with-include"; then
		echo -n " INCLUDE";
		INCLUDEPATH="${INCLUDEPATH} ${param#*=}";
	elif test "$short" = "--do-include"; then
		echo -n " UINCLUDE";
		INCLUDE="${INCLUDE} -I${param#*=}";
	elif test "$short" = "--host"; then
		echo -n " HOST";
		RCHOST="${param#*=}";
	elif test "$short" = "--target"; then
		echo -n " TARGET";
		RCTARGET="${param#*=}";
	elif test "$short" = "--core"; then
		echo -n " CORE";
		COREFILE="${param#*=}";
	elif test "$short" = "--version"; then
		echo -n " VERSION";
		RCVERSION="${param#*=}";
	elif test "$param" = "--debug"; then
		echo -n " DEBUG";
		XCFLAGS="$XCFLAGS -DDEBUG";
		GCCFLAGS="${GCCFLAGS} -g"
	elif test "$param" = "--nowtf"; then
		echo -n " NOWTF";
		XCFLAGS="$XCFLAGS -DNOWTF";
	elif test "$param" = "--externalise"; then
		echo -n " EXT";
		externalise="yes";
	elif test "$param" = "--enable-linux"; then
		echo -n " LINUX";
		EXTERNALMODULES="${EXTERNALMODULES} ${MODULES_LINUX}"
	elif test "$param" = "--enable-bsd"; then
		echo -n " BSD";
		PMODULES="$PMODULES bsd";
	elif test "$param" = "--enable-efl"; then
		echo -n " EFL";
		PMODULES="$PMODULES efl";
	elif test "$param" = "--help"; then
		echo -n " HELP";
		HELP="1";
	elif test "$param" = "--bitch-a-lot"; then
		echo -n " BITCHY";
		XCFLAGS="$XCFLAGS -DBITCHY";
	elif test "$param" = "--use-posix-regex"; then
		echo -n " REGEX";
		XCFLAGS='$(sed "s/-DPOSIXREGEX//" < echo ${XCFLAGS}) -DPOSIXREGEX';
	elif test "$param" = "--no-posix-regex"; then
		echo -n " NOREGEX";
		XCFLAGS=$(sed "s/-DPOSIXREGEX//" < echo ${XCFLAGS});
	elif test "$param" = "--pthread-wait-bug"; then
		echo -n " PTHREAD-WAIT-BUG";
		XCFLAGS="$XCFLAGS -DBUGGY_PTHREAD_CHILD_WAIT_HANDLING";
	elif test "$param" = "--no-nix-environ"; then
		echo -n " NO-NIX-ENVIRON";
		XCFLAGS="$XCFLAGS -DNONIXENVIRON";
	elif test "$param" = "--avoid-usr"; then
		echo -n " EXUSR";
		EXUSR="yes";
	elif test "$short" = "--with-expat"; then
		echo -n " EXPAT";
		EXPAT_CFGD_LIB="${param#*=}";
        elif test "$param" = "--svn"; then
                echo -n " SVN";
                ISSVN=1;
        elif test "$param" = "--static"; then
                echo -n " STATIC";
                STATIC=1;
        elif test "$param" = "--ebuild"; then
                echo -n " EBUILD";
                EBUILD="1";
                CONFIGINSTALLMETHOD="ebuild";
        elif test "$param" = "--enable-ipc-dbus"; then
		EXTERNALMODULES="${EXTERNALMODULES} ipc-dbus";
		DBUS="dbus";
		UTILOBJECTS="${UTILOBJECTS} einit-control-dbus libeinit libeinit++";
	elif test "$shorte" = ""; then
		EXTERNALMODULES="${EXTERNALMODULES} ${param#*--enable-}";
	fi;
done;
echo " ]";

echo -n "checking automatic options [";
if test -n "`uname -a|grep -i 'Linux'`"; then
	echo -n " LINUX";
	EXTERNALMODULES="${EXTERNALMODULES} ${MODULES_LINUX}"
fi
echo " ]";

echo "skipping modules: ${nomodule}"

if test -e configure.overrides; then source configure.overrides; fi

if test "${externalise}" = "yes"; then
	EXTERNALMODULES="${EXTERNALMODULES} ${INTERNALMODULES}";
	INTERNALMODULES="${NEED_INTERNAL}";
fi

newext="";
for i in ${EXTERNALMODULES}; do
	match="no"
	for j in ${INTERNALMODULES}; do
		if test "${i}" = "${j}"; then
			match="yes"
		fi
	done

	if test "${match}" = "no"; then
		newext="${newext} ${i}";
	fi
done
EXTERNALMODULES="${newext}";

deldupes() {
	local ret;
	local have;

	ret=""
	
	for i in $@; do
		have="no"
		for y in ${ret}; do
			if test "${i}" = "${y}"; then
				have="yes"
			fi
		done

		if test "${have}" = "no"; then
			ret="${ret} ${i}"
		fi
	done

	echo ${ret};
}

deldisabled() {
	local ret;
	local have;

	ret=""
	
	for i in $@; do
		have="no"
		for y in ${nomodule}; do
			if test "${i}" = "${y}"; then
				have="yes"
			fi
		done

		if test "${have}" = "no"; then
			ret="${ret} ${i}"
		fi
	done

	echo ${ret};
}

EXTERNALMODULES=$(deldupes ${EXTERNALMODULES})
EXTERNALMODULES=$(deldisabled ${EXTERNALMODULES})
INTERNALMODULES=$(deldupes ${INTERNALMODULES})
INTERNALMODULES=$(deldisabled ${INTERNALMODULES})

echo "selected modules:"
echo " internal: ${INTERNALMODULES}"
echo " external: ${EXTERNALMODULES}"

if test "$HELP" = "1"; then
	echo "Usage:
 ./configure [--prefix=path] [--enable-*] [--debug] [--help]

Options:
 --prefix=path          use this to set a path to install to (absolute!)
 --with-include=path    use this to set a path to install to (absolute!)
 --enable-linux         enable compilation of linux-specific modules
 --enable-bsd           enable compilation of bsd-specific modules
 --enable-efl           enable compilation of modules that utilise the efl
 --bitch-a-lot          tell all programs used to compile and link to be more
                        verbose
 --use-posix-regex      use posix.2 regular expressions
 --pthread-wait-bug     some pthreads implementations don't allow all threads
                        in one process to wait on other processes' children
 --internal-modules     the modules to compile-in
 --external-modules     the modules to compile as loadable .so-files
 --tree-type=           define the type of tree-structures einit should use.
 --debug                will #define DEBUG.
 --static               build with static libs
 --help                 display this text and exit.
 --svn                  indicate that this is a build off SVN.
 --ebuild               indicate that this script was called from portage/e.a.";
exit;
fi;

echo -n "/etc/make.conf ["
if test "$EBUILD" = "1"; then
	echo -n " skipping (ebuild)";
elif test -n ""${CFLAGS}; then
	echo -n " skipping (CFLAGS set)";
elif test -f /etc/make.conf; then
	echo -n " sourcing";
	source /etc/make.conf;
else
	echo -n " not found";
fi
echo " ]"

if test -n "$RCHOST"; then CHOST=$RCHOST; fi;
if test -n "$RCTARGET"; then CTARGET=$RCTARGET; fi;
#GCCVERSION=`gcc --version`;

echo -n "checking system type... "
if test -n "$CHOST"; then
	CHOST=$CHOST;
else
#	CHOST=${GCCVERSION%%gcc-* *};
#	CHOST="${CHOST}libc";
	CHOST="gcc";
fi
echo " $CHOST";

echo -n "checking target system type... "
if test -n "$CTARGET"; then
	CTARGET=$CTARGET;
else
	CTARGET=$CHOST;
fi
echo " $CTARGET";

echo -n "checking for a compiler... "
if test -n "$CC"; then
	echo "got one! ($CC)";
else
	if test -x "$(which ${CTARGET}-gcc-${RCVERSION} 2>/dev/null)"; then CC="${CTARGET}-gcc-${RCVERSION}";
	elif test -x "$(which ${CTARGET}-gcc 2>/dev/null)"; then CC="${CTARGET}-gcc";
	elif test -x "$(which ${CTARGET}-cc 2>/dev/null)"; then CC="${CTARGET}-cc";
	elif test -x "$(which ${CHOST}-gcc-${RCVERSION} 2>/dev/null)"; then CC="${CHOST}-gcc-${RCVERSION}";
	elif test -x "$(which ${CHOST}-gcc 2>/dev/null)"; then CC="${CHOST}-gcc";
	elif test -x "$(which ${CHOST}-cc 2>/dev/null)"; then CC="${CHOST}-cc";
	elif test -x "$(which gcc 2>/dev/null)"; then CC=cc;
	elif test -x "$(which icc 2>/dev/null)"; then CC=cc;
	elif test -x "$(which cc 2>/dev/null)"; then CC=cc; fi;
	if test -n "$CC"; then
		echo "$CC";
	else
		echo "no compiler found, bailing out!"; exit;
	fi;
fi

echo -n "checking for an assembler... "
if test "${COMPILER}" = "gnu"; then
	ASM=${CC};
fi
echo ${ASM}

echo -n "checking for a C++ compiler... "
if test -n "$CXX"; then
	echo "got one! ($CXX)";
else
	if test -x "$(which ${CTARGET}-g++-${RCVERSION} 2>/dev/null)"; then CXX="${CTARGET}-g++-${RCVERSION}";
	elif test -x "$(which ${CTARGET}-g++ 2>/dev/null)"; then CXX="${CTARGET}-g++";
	elif test -x "$(which ${CTARGET}-cc 2>/dev/null)"; then CXX="${CTARGET}-cc";
	elif test -x "$(which ${CHOST}-g++-${RCVERSION} 2>/dev/null)"; then CXX="${CHOST}-g++-${RCVERSION}";
	elif test -x "$(which ${CHOST}-g++ 2>/dev/null)"; then CXX="${CHOST}-g++";
	elif test -x "$(which ${CHOST}-cc 2>/dev/null)"; then CXX="${CHOST}-cc";
	elif test -x "$(which g++ 2>/dev/null)"; then CXX=g++;
	elif test -x "$(which cc 2>/dev/null)"; then CXX=cc; fi;
	if test -n "$CXX"; then
		echo "$CXX";
	else
		echo "no C++ compiler found, bailing out!"; exit;
	fi;
fi

echo "finding absolute include directory..."
INCLUDE="-I`pwd`/build/headers ${INCLUDE}";
echo -n "searching for expat.h..."
EXPAT=`find -d ${INCLUDEPATH} -iname expat.h 2>/dev/null`
if test -n {EXPAT}; then
	for x in ${EXPAT}; do
		x=${x%/*};
		echo -n " ${x}";
		INCLUDE="${INCLUDE} -I${x}";
	done;
	echo "";
else
	echo "not found";
	echo "expat is currectly a critical requirement, you need to install it for einit to compile and work. Check your OS-distribution's package repository, or go to http://expat.sf.net/.";
fi

# CFLAGS="${CXXFLAGS}"

echo -n "checking system type... "
CCL="\${CC} \${INCLUDE} \${CFLAGS} -fPIC"
XXL="\${CXX} \${INCLUDE} \${CXXFLAGS} -fPIC"
if test -n "`uname -a|grep -i 'darwin kernel'`"; then
	echo "Darwin";
	OS="darwin";
	XCFLAGS="$XCFLAGS -DDARWIN -DPOSIX -DBSD";
	XDYNAMIC="";
	XDYNAMIC_SIMPLE="-dynamiclib"
# -bundle -flat_namespace -undefined suppress
	if [ "${COREFILE}" != "" ]; then
		LLD="-bundle -Wl,-bundle_loader,${COREFILE}";
	else
		LLD="-bundle -Wl,-bundle_loader,`pwd`/build/core/einit";
	fi
	STATIC="";
	LIBRT=""
	SO="dylib"
elif test -n "`uname -a|grep -i 'Linux'`"; then
	echo "Linux";
	OS="linux";
	XCFLAGS="$XCFLAGS -DPOSIX -DLINUX";
	LLD="-shared";
	XDYNAMIC="-Wl,-export-dynamic";
	XDYNAMIC_SIMPLE="-shared"
	LPA="-lpthread";
	if test "$STATIC" = "1"; then
# static linking doesn't work like this, it messes up .so modules :/
		STATIC="-static"
#		STATIC=""
		if test -n "${EXTERNALMODULES}"; then
			INTERNALMODULES="${INTERNALMODULES} ${EXTERNALMODULES}";
			EXTERNALMODULES=""
		fi
	else
		STATIC=""
	fi
	LIBRT="-lrt"
	SO="so"
	NETLINK="netlink"
else
	echo "(guess) generic posix-ish unix";
	OS="unknown";
	XCFLAGS="$XCFLAGS -DPOSIX";
	LLD="-shared";
	XDYNAMIC="-Wl,-export-dynamic";
	XDYNAMIC_SIMPLE="-shared"
	LPA="-lpthread";
	STATIC="";
	LIBRT="-lrt"
	SO="so"
fi

if [ "${NETLINK}" = "netlink" ]; then
	echo "getting libnl-1 flags off pkg-config..."
	NETLINK_CFLAGS=$(pkg-config --cflags libnl-1)
	NETLINK_LIBS=$(pkg-config --libs libnl-1)
fi

if [ "${NOTIFY}" = "notify" ]; then
	echo "getting libnotify flags off pkg-config..."
	LIBNOTIFY_CFLAGS=$(pkg-config --cflags libnotify)
	LIBNOTIFY_LIBS=$(pkg-config --libs libnotify)
fi

if [ -e "${EXPAT_CFGD_LIB}" ]; then
 EXPAT_LIBS="${EXPAT_CFGD_LIB}"
elif [ "${EXUSR}" = "yes" ]; then
 if [ -e "/usr/lib/libexpat.a" ]; then
  EXPAT_LIBS="/usr/lib/libexpat.a"
 elif [ -e "/usr/local/lib/libexpat.a" ]; then
  EXPAT_LIBS="/usr/local/lib/libexpat.a"
 elif [ -e "/lib/libexpat.a" ]; then
  EXPAT_LIBS="/lib/libexpat.a"
 else
  EXPAT_LIBS="-lexpat"
 fi;
else
 EXPAT_LIBS="-lexpat"
fi;
echo "will use this expat library: ${EXPAT_LIBS}"

echo -n "figuring out what libs need to be linked into the core..."
if test -n "`echo ${INTERNALMODULES}|grep linux-netlink`"; then
 echo -n " -lnl"
 CORELIBS="${CORELIBS} ${NETLINK_LIBS}"
fi;
if test -n "`echo ${INTERNALMODULES}|grep module-logic-v3`"; then
 echo -n " -lrt"
 CORELIBS="${CORELIBS} ${LIBRT}"
fi;
if test -n "`echo ${INTERNALMODULES}|grep module-so`"; then
	if test -z "$(uname -a|grep BSD)"; then
		echo -n " -ldl";
		CORELIBS="${CORELIBS} -ldl"
	fi
fi;
if test -n "`echo ${INTERNALMODULES}|grep bootstrap-configuration-xml-expat`"; then
 echo -n " -lexpat"
 CORELIBS="${CORELIBS} ${EXPAT_LIBS}"
fi;
echo " done"

if test -n "$(echo ${EXTERNALMODULES}|grep bootstrap-)"; then DO_BOOTSTRAP=yes; else DO_BOOTSTRAP=; fi

echo -n "testing architecture... "
if test -x "$(which arch 2>/dev/null)"; then
	ARCH=`arch`
else
	ARCH="unknown"
fi
echo ${ARCH}

echo -n "see if we have some sources in assembly... "
ASMDIR="asm/${ARCH}-${OS}-${COMPILER}"
if ! test -d ${ASMDIR}; then ASMDIR=""; fi
echo "done.";

if test "${PREFIX}" = "/"; then UPREFIX="${PREFIX}/usr"; else UPREFIX="${PREFIX}"; fi
if test "${EBUILD}" = "1"; then UPREFIX="${PREFIX}/usr"; fi

if [ "${DBUS}" = "dbus" ]; then
	echo "getting dbus flags off pkg-config..."
	DBUS_CFLAGS=$(pkg-config --cflags dbus-1)
	DBUS_LIBS=$(pkg-config --libs dbus-1)
fi

if [ "${GTK}" = "gtk" ]; then
	echo "getting gtkmm flags off pkg-config..."
	GTK_CFLAGS=$(pkg-config --cflags gtkmm-2.4)
	GTK_LIBS=$(pkg-config --libs gtkmm-2.4)
fi

XCFLAGS="${XCFLAGS} '-DUSHAREDIR=\"${UPREFIX}/share/einit\"'"

echo "creating config.mk..."
echo "PREFIX = $PREFIX
ETCDIR = ${PREFIX}/etc/einit
ETCXDIR = ${PREFIX}/etc
LIBDIR = ${PREFIX}/lib/einit
MODDIR = ${PREFIX}/lib/einit/modules
ULIBDIR = ${UPREFIX}/lib
BINDIR = ${PREFIX}/bin
UBINDIR = ${UPREFIX}/bin
USHAREDIR = ${UPREFIX}/share/einit
SBINDIR = ${PREFIX}/sbin
INCLUDEDIR = ${UPREFIX}/include
BOOTSTRAPDIR = ${PREFIX}/${BOOTSTRAP_MODULE_PATH}

SO = "${SO}"

INSTALL = install
IPARAM =
SOIPARAM = \${IPARAM}
BINIPARAM = \${IPARAM}
CONFIGINSTALLMETHOD = ${CONFIGINSTALLMETHOD}

COMPILER = ${COMPILER}
OS = ${OS}
ARCH = ${ARCH}
ASMDIR = ${ASMDIR}

LIBRT = ${LIBRT}

PMODULES = $PMODULES
OPTIONS = $OPTIONS
INCLUDE = $INCLUDE

XDYNAMIC = $XDYNAMIC
XDYNAMIC_SIMPLE = $XDYNAMIC_SIMPLE
BUILDNUMBER = $BUILDNUMBER
ISSVN = $ISSVN

INTERNALMODULES = ${INTERNALMODULES}
EXTERNALMODULES = ${EXTERNALMODULES}

CORELIBS = ${CORELIBS}

BOOTSTRAP_MODULE_PATH = ${BOOTSTRAP_MODULE_PATH}

CC = $CC ${GCCFLAGS} -fPIC
ASM = $ASM ${GCCFLAGS}

CFLAGS = $CFLAGS$XCFLAGS
CXXFLAGS = $CXXFLAGS$XCFLAGS
LDFLAGS = $LDFLAGS
CCC = \${CC} \${INCLUDE} \${CFLAGS}
CCL = $CCL
CLD = \${CC} \${LDFLAGS} ${LIBRARIES}
LLD = \${CC} \${LDFLAGS} $LLD ${LIBRARIES}
XLLD = $LLD
STATIC = $STATIC
LPA = $LPA

CXX = $CXX ${GCCFLAGS}
CXXC = \${CXX} \${INCLUDE} \${CXXFLAGS} \${CFLAGS} -DISSVN=\${ISSVN}
XXL = $XXL -DISSVN=\${ISSVN}
CXXLD = \${CXX} \${LDFLAGS}
CXXLD = \${CXXC} \${LDFLAGS}
LXXLD = \${CXXC} \${LDFLAGS} $LLD

DBUS_CFLAGS = ${DBUS_CFLAGS}
DBUS_LIBS = ${DBUS_LIBS}
DBUS = ${DBUS}

GTK_CFLAGS = ${GTK_CFLAGS}
GTK_LIBS = ${GTK_LIBS}
GTK = ${GTK}

NETLINK_CFLAGS = ${NETLINK_CFLAGS}
NETLINK_LIBS = ${NETLINK_LIBS}

LIBNOTIFY_CFLAGS = ${LIBNOTIFY_CFLAGS}
LIBNOTIFY_LIBS = ${LIBNOTIFY_LIBS}

EXPAT_LIBS= ${EXPAT_LIBS}

GUI = ${GUI}
" > config.mk

echo "creating config.sh..."
echo "PREFIX=\"$PREFIX\"
ETCDIR=\"${PREFIX}/etc/einit\"
ETCXDIR=\"${PREFIX}/etc\"
LIBDIR=\"${PREFIX}/lib/einit\"
ULIBDIR=\"${UPREFIX}/lib\"
MODDIR=\"${PREFIX}/lib/einit/modules\"
BINDIR=\"${PREFIX}/bin\"
SBINDIR=\"${PREFIX}/sbin\"
INCLUDEDIR=\"${UPREFIX}/include\"
BOOTSTRAPDIR=\"${PREFIX}/${BOOTSTRAP_MODULE_PATH}\"

INSTALL=\"install\"
IPARAM=\"\"
SOIPARAM=\"\${IPARAM}\"
BINIPARAM=\"\${IPARAM}\"
CONFIGINSTALLMETHOD=\"${CONFIGINSTALLMETHOD}\"

COMPILER=\"${COMPILER}\"
OS=\"${OS}\"
ARCH=\"${ARCH}\"
ASMDIR=\"${ASMDIR}\"

LIBRT=\"${LIBRT}\"

PMODULES=\"$PMODULES\"
OPTIONS=\"$OPTIONS\"
INCLUDE=\"$INCLUDE\"

XDYNAMIC=\"$XDYNAMIC\"
BUILDNUMBER=\"$BUILDNUMBER\"
ISSVN=\"$ISSVN\"

INTERNALMODULES=\"${INTERNALMODULES}\"
EXTERNALMODULES=\"${EXTERNALMODULES}\"

CORELIBS=\"${CORELIBS}\"

BOOTSTRAP_MODULE_PATH=\"${BOOTSTRAP_MODULE_PATH}\"

CC=\"$CC ${GCCFLAGS}\"
ASM=\"$ASM ${GCCFLAGS}\"

CFLAGS=\"$CFLAGS$XCFLAGS\"
LDFLAGS=\"$LDFLAGS\"
CCC=\"\${CC} \${INCLUDE} \${CFLAGS}\"
CCL=\"$CCL\"
CLD=\"\${CC} \${LDFLAGS} ${LIBRARIES}\"
LLD=\"\${CC} \${LDFLAGS} $LLD ${LIBRARIES}\"
XLLD=\"$LLD\"
STATIC=\"$STATIC\"
LPA=\"$LPA\"

CXX=\"$CXX\"
CXXC=\"\${CXX} \${INCLUDE} \${CXXFLAGS} \${CFLAGS} -DISSVN=\${ISSVN}\"
XXL=\"$XXL -DISSVN=\${ISSVN}\"
CXXLD=\"\${CXX} \${LDFLAGS}\"
CXXLD=\"\${CXXC} \${LDFLAGS}\"
LXXLD=\"\${CXXC} \${LDFLAGS} $LLD\"

DBUS_CFLAGS=\"${DBUS_CFLAGS}\"
DBUS_LIBS=\"${DBUS_LIBS}\"
DBUS=\"${DBUS}\"
" > config.sh

collect_objects () {
	objects=""
	asmobjects=""
	for i in $@; do
		for suffix in ${SUFFIXES}; do
			res=$(find src ${ASMDIR} -name "${i}.${suffix}");
			if test -n "${res}"; then
				subobjects="";
				for y in ${res}; do
					subobjects="${subobjects} ${pwd}/${y}"
				done
				objects="${objects} ${i}.o";
				
				if [ -e "object.${i}.defines" ]; then LOCALDEF=$(cat "object.${i}.defines");
				else LOCALDEF=""; fi

				if test "${suffix}" = "s"; then
					asmobjects="${asmobjects} ${i}.o";
				fi
				if test "${suffix}" = "S"; then
					asmobjects="${asmobjects} ${i}.o";
				fi


				if test -n "$(echo ${INTERNALMODULES}|grep -w ${i})"; then
					echo "${i}.o: ${subobjects}\n	\$(CCC) ${DEFINES} ${LOCALDEF} -Dthismodule=$(echo ${i}|sed -e "s/[^A-Za-z0-9_]/_/g")_thismodule -Dself=$(echo ${i}|sed -e "s/[^A-Za-z0-9_]/_/g")_self -c ${subobjects} -o ${i}.o\n"
				else
					echo "${i}.o: ${subobjects}\n	\$(CCC) ${DEFINES} ${LOCALDEF} -c ${subobjects} -o ${i}.o\n"
				fi
			fi
		done
	done
}

collect_modules() {
	objects=""
	for i in $@; do
		for suffix in ${SUFFIXES}; do
			res=$(find src ${ASMDIR} -name "${i}.${suffix}");
			if test -n "${res}"; then
				subobjects="";
				for y in ${res}; do
					subobjects="${subobjects} ${pwd}/${y}"
				done
				objects="${objects} ${i}.so";

				if [ -e "object.${i}.defines" ]; then LOCALDEF=$(cat "object.${i}.defines");
				else LOCALDEF=""; fi

				if test "${suffix}" = "c++"; then
					echo "${i}.o: ${subobjects}\n	\${XXL} ${DEFINES} ${LOCALDEF} -I$(dirname ${subobjects}) -c ${subobjects} -o ${i}.o\n";
				else
					echo "${i}.o: ${subobjects}\n	\${CCL} ${DEFINES} ${LOCALDEF} -I$(dirname ${subobjects}) -c ${subobjects} -o ${i}.o\n";
				fi

				if test -z "$(grep ${i}.so\:\  ${template})"; then
					if test "${suffix}" = "c++"; then
						echo "${i}.so: ${i}.o\n	\${LXXLD} ${i}.o -o ${i}.so\n";
					else
						echo "${i}.so: ${i}.o\n	\${LLD} ${i}.o -o ${i}.so\n";
					fi
				fi
			fi
		done
	done
}

install_modules() {
	echo "install-auto: $@
	\${INSTALL} \${SOIPARAM} -d \${DESTDIR}/\${MODDIR}
	\${INSTALL} \${SOIPARAM} -d \${DESTDIR}/\${BOOTSTRAPDIR};";
	for i in $@; do
		if test -n "$(echo ${i}|grep bootstrap-)"; then
			echo "	\${INSTALL} \${SOIPARAM} ${i} \${DESTDIR}/\${BOOTSTRAPDIR}"
		else
			echo "	\${INSTALL} \${SOIPARAM} ${i} \${DESTDIR}/\${MODDIR}"
		fi
	done
}

collect_and_install_headers() {
	objects=""
	install=""
	main="no"

	for i in $@; do
		if [ "${i}" = "src/include/einit" ]; then
			main="yes"
		fi

		mkdir -p build/$z/$(basename ${i});
		install="${install}\n	\${INSTALL} \${IPARAM} -d \${DESTDIR}/\${INCLUDEDIR}/$(basename ${i})\n	\${INSTALL} \${IPARAM} -m 0644 $(basename ${i})/*.h \${DESTDIR}/\${INCLUDEDIR}/$(basename ${i})"
		
		for suffix in ${SUFFIXES}; do
			headers=$(find ${i} -name "*.${suffix}")
			for h in ${headers}; do
				newname=$(echo `basename $h`|${HEADERPREPARE})
				ln -sf $(pwd)/${h} build/$z/$(basename ${i})/${newname};
			
				objects="${objects} ${newname}"
			done
		done
	done
	echo "install-auto:${install}"

	if [ "${main}" = "no" ]; then return; fi
	
	echo "/* created by the configure-script, you shouldn't modify this */

#ifndef EINIT_MODULES_CONFIGURATION_H
#define EINIT_MODULES_CONFIGURATION_H

#define BOOTSTRAP_MODULE_PATH \"${BOOTSTRAP_MODULE_PATH}\"

" > build/headers/einit-modules/configuration.h

	if test "${DO_BOOTSTRAP}" = "yes"; then
 		echo "#define DO_BOOTSTRAP" >> build/headers/einit-modules/configuration.h;
	fi

	for i in ${INTERNALMODULES}; do
 		echo "#define einit_modules_$(echo ${i}|sed -e "s/[^A-Za-z0-9_]/_/g") 'y'" >> build/headers/einit-modules/configuration.h;
	done
	for i in ${EXTERNALMODULES}; do
 		echo "#define einit_modules_$(echo ${i}|sed -e "s/[^A-Za-z0-9_]/_/g") 'm'" >> build/headers/einit-modules/configuration.h;
	done

	echo "#endif
" >> build/headers/einit-modules/configuration.h

	echo "/* created by the configure-script, you shouldn't modify this */

#ifndef EINIT_CONFIGURATION_H
#define EINIT_CONFIGURATION_H

#define BUILDNUMBER ${BUILDNUMBER}
#define ISSVN ${ISSVN}

#define BUFFERSIZE 1024

#define EINIT_LIB_BASE \"${PREFIX}/lib/einit\"
#define EINIT_SBIN_BASE \"${PREFIX}/sbin\"
#define EINIT_ETC_BASE \"${PREFIX}/etc/einit\"

" > build/headers/einit/configuration.h

	if test "${ISSVN}" = "1"; then
		echo "#define EINIT_VERSION_LITERAL_SUFFIX \"-svn-${BUILDNUMBER}\" " >> build/headers/einit/configuration.h;
	else
		echo "#define EINIT_VERSION_LITERAL_SUFFIX \"#${BUILDNUMBER}\" " >> build/headers/einit/configuration.h;
	fi;

	for i in ${ASMFUNCTIONS}; do
		if test -e "${ASMDIR}/${i}.s"; then
			echo "#define _have_asm_${i}" >> build/headers/einit/configuration.h;
		fi
		if test -e "${ASMDIR}/${i}.S"; then
			echo "#define _have_asm_${i}" >> build/headers/einit/configuration.h;
		fi
	done

	echo "#endif
" >> build/headers/einit/configuration.h

}

echo -n "creating build environment... "
if test -d build; then
	echo -n "clean ";
	rm -Rf build;
fi;
echo -n "build ";
mkdir -p build;

components=""

for z in ${BUILDCOMPONENTS}; do
	echo -n "${z} ";
	mkdir -p build/${z};
	cat config.mk > build/${z}/Makefile
	echo "\nall: ${z}\n" >> build/${z}/Makefile

	inobjects=""
	inmodules=""
	inheaders=""
	objects=""

	if test "${z}" = "core"; then inobjects="${COREOBJECTS} ${INTERNALMODULES}"; SUFFIXES=${SOURCESUFFIXES}; DEFINES="-DEINIT_CORE"; fi
	if test "${z}" = "util"; then inobjects=${UTILOBJECTS}; SUFFIXES=${SOURCESUFFIXES}; DEFINES="-DEINIT_UTIL"; fi
	if test "${z}" = "modules"; then inmodules=${EXTERNALMODULES}; SUFFIXES=${SOURCESUFFIXES}; DEFINES="-DEINIT_MODULE"; fi
	if test "${z}" = "headers"; then inheaders=${HEADERSOURCES}; SUFFIXES=${HEADERSUFFIXES}; fi

	output="build/${z}/Makefile";
	template="makefile.${z}.template";

	if test -n "${inobjects}"; then collect_objects ${inobjects} >> build/${z}/Makefile; fi
	if test -n "${inmodules}"; then
		if test -n "${MODULE_PREPARE_OBJECTS}"; then
			collect_modules ${MODULE_PREPARE_OBJECTS} >> build/${z}/Makefile;
		fi
		collect_modules ${inmodules} >> build/${z}/Makefile;
		install_modules ${objects} >> build/${z}/Makefile;
	fi
	if test -n "${inheaders}"; then collect_and_install_headers ${inheaders} >> build/${z}/Makefile; fi

	if test -n "${objects}"; then components="${components} ${z}"; fi

	if test "${z}" = "core"; then 
		started="";
		for i in ${INTERNALMODULES}; do
			if test -z "${started}"; then
				started="yes";
				echo "// this file was automatically created by the build process" > build/core/module-configuration.c
				echo "#include <einit/module.h>" >> build/core/module-configuration.c
			fi;
			echo "extern const struct smodule * $(echo ${i}|sed -e "s/[^A-Za-z0-9_]/_/g")_self;" >> build/core/module-configuration.c
		done
		if test -n "${started}"; then
			echo "const struct smodule **coremodules[] = {" >> build/core/module-configuration.c
			
			for i in ${INTERNALMODULES}; do
				echo " &$(echo ${i}|sed -e "s/[^A-Za-z0-9_]/_/g")_self," >> build/core/module-configuration.c
			done
			echo  " NULL };" >> build/core/module-configuration.c
			echo  "" >> build/core/module-configuration.c
		else
			echo "// this file was automatically created by the build process" > build/core/module-configuration.c
			echo "#include <einit/module.h>" >> build/core/module-configuration.c
			echo "const struct smodule **coremodules[] = { NULL };" >> build/core/module-configuration.c
			echo  "" >> build/core/module-configuration.c
		fi;

		echo "\nmodule-configuration.o: module-configuration.c\n	\$(CCC) -DEINIT_CORE -c  module-configuration.c -o module-configuration.o\n" >> build/${z}/Makefile
	fi

	echo "\nobjects = ${objects}\n" >> build/${z}/Makefile
	echo "\nasmobjects = ${asmobjects}\n" >> build/${z}/Makefile
	cat makefile.${z}.template >> build/${z}/Makefile
done

echo "# makefiles below here are auto-created, it's not gonna help much if you modify any of them." > build/Makefile;

for y in ${MAKEFILETASKS}; do
	echo "\n${y}:" >> build/Makefile
	for i in ${components}; do
		echo "	cd ${i} && \${MAKE} ${y}" >> build/Makefile
	done
done

echo "done."
