
xx() { echo ">> $*" ; eval "$*" ; }

mkdir -p src tars lib
rm -f src/temp.*

if [ ! -f lib/current-arch ] || [ "`cat lib/current-arch`" != $arch ] ; then
	rm -f lib/* ; echo "$arch" > lib/current-arch
fi

if [ ! -f lib/gcc-wrapper ] || \
  [ misc/lib-source/gcc-wrapper.c -nt lib/gcc-wrapper ] ; then
	echo "Creating gcc wrapper program (lib/gcc-wrapper)."
	x="$BUILD_CC -Wall -O2 -DENVPREFIX='\"CC\"' -o lib/gcc-wrapper \
	  misc/lib-source/gcc-wrapper.c"
	xx $x || exit 1
	
	if [ $crossnative = cross ] ; then
		rm -f lib/$CC  ; ln -s gcc-wrapper lib/$CC
		rm -f lib/$CXX ; ln -s gcc-wrapper lib/$CXX
	else
		for x in $CC $CXX c++ cc g++ gcc g77 $arch_target-gcc
		do rm -f lib/$x ; xx ln -s gcc-wrapper lib/$x ; done
	fi
fi

if [ ! -f lib/getdu ] || [ misc/lib-source/getdu.c -nt lib/getdu ] ; then
	echo "Creating getdu helper app (lib/getdu)."
	x="$BUILD_CC -Wall -O2 -o lib/getdu misc/lib-source/getdu.c"
	xx $x || exit 1
fi

if [ ! -f lib/getfiles ] || [ misc/lib-source/getfiles.c -nt lib/getfiles ] ; then
	echo "Creating getfiles helper app (lib/getfiles)."
	x="$BUILD_CC -Wall -O2 -o lib/getfiles misc/lib-source/getfiles.c"
	xx $x || exit 1
fi

if [ ! -f lib/fl_wrparse ] ||
   [ misc/lib-source/fl_wrparse.c -nt lib/fl_wrparse ] ; then
	echo "Creating output parser for the flist" \
	     "wrapper library (lib/fl_wrparse)."
	x="$BUILD_CC -Wall -O2"
	# x="$x -I $base/arch-conf/$arch/include"
	x="$x -o lib/fl_wrparse misc/lib-source/fl_wrparse.c"
	xx $x || exit 1
fi

if [ $useflwrapper = 1 ] ; then
	if [ ! -f lib/fl_wrapper.so ] ||
	   [ misc/lib-source/fl_wrapper.c -nt lib/fl_wrapper.so ] ; then
		echo "Creating flist wrapper library (lib/fl_wrapper.so)."
		x="$BUILD_CC -Wall -O2 -ldl --shared"
		#x="$x -I $base/arch-conf/$arch/include"
		x="$x -o lib/fl_wrapper.so misc/lib-source/fl_wrapper.c"
		x="$x -DNEW_FILES_LOG='\"$base/src/temp.fl_wrapper.out\"'"
		xx $x || exit 1
		echo -n "Testing the flist wrapper library ... "
		if ! sh -c 'LD_PRELOAD=lib/fl_wrapper.so date ; exit $?' >/dev/null 2>&1
		then
			echo ERROR
			echo
			echo Detected a problem with the flist wrapper lib \
			     on your system.
			echo Please append '"useflwrapper=0"' to your \
			     ./Config file ...
			echo
			rm -f lib/fl_wrapper.so ; exit 1
		else echo OK ; fi
	fi
	if [ -z "$LD_PRELOAD" ] ; then
		export LD_PRELOAD="$base/lib/fl_wrapper.so"
	else
		export LD_PRELOAD="$LD_PRELOAD:$base/lib/fl_wrapper.so"
	fi
fi

