# --- T2-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# 
# T2 SDE: package/.../dietlibc/parse-config-9
# Copyright (C) 2004 - 2008 The T2 SDE Project
# Copyright (C) 1998 - 2004 ROCK Linux Project
# 
# More information can be found in the files COPYING and README.
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- T2-COPYRIGHT-NOTE-END ---

function set_wrapper_and_misc () {
	export DIETHOME=$root/usr/dietlibc
	if ! atstage native || [ "$SDECFG_LIBC" != "dietlibc" ] ; then
		var_append CC_WRAPPER_OTHERS ":" $dietbin
	fi

	if [ "$diet_dynamic_static" == "static"  ] ; then
	  var_append extraconfopt ' ' '--disable-shared --without-shared'
	  var_append GCC_WRAPPER_INSERT ' ' '-c?:-static'
	fi
	var_append GCC_WRAPPER_INSERT ' ' '-D__dietlibc__'
}

function add_lib () {
	var_append CC_WRAPPER_APPEND " " "-c?:$1"
}

function remove_lib () {
        var_append CC_WRAPPER_REMOVE " " "$1"
}

function pkg_dietlibc_rebuild () {
	patchfiles=
	. $base/package/*/dietlibc/parse-config-9
	apply_patchfiles

	# TODO: re-schedule a clean rebuild
	rm -f config.cache
	eval_config_command $confopt $extraconfopt
	eval $MAKE clean
	eval $MAKE $makeopt
	eval $MAKE $makeinstopt
}


# decide wether to compile the package with dietlibc or not
if [ "$SDECFG_LIBC" = "dietlibc" -o "$pkg" = "dietlibc" ]; then
	pkg_dietlibc_useit=1
else
	x="${pkg//-/_}" ; x="${x//+/_}"
	eval "pkg_dietlibc_useit=\$SDECFG_DIETLIBC_${x}"
fi

[ "$pkg_dietlibc_useit" != 1 ] && pkg_dietlibc_useit=0

[ "$pkg_dietlibc_useit" = 1 -a $pkg = gcc ] &&
	var_append extraconfopt " " "--disable-threads"

# never use dietlibc in stage 0
atstage toolchain && pkg_dietlibc_useit=0

# for some package only build with dielitbc, if this is the second run
if [ "$SDECFG_LIBC" != "dietlibc" -a "$pkg_dietlibc_useit" = 1 ]; then
  case $pkg in
	udev)
		if [ "$pkg_udev_this_is_the_second_run" != 1 ]; then
			echo_status "First pass, linking against system libc, ..."
			pkg_dietlibc_useit=0
			hook_add postmake 7 pkg_dietlibc_rebuild
		else
			# force static linking
			var_append extraconfopt ' ' '--enable-shared=no'
		fi
		;;
  esac
fi

if [ "$pkg_dietlibc_useit" = 1 ]; then
	# If we use something like 'i386-pc-linux-gnu' as architecture name,
	# some programs start using gnu extensions. So we use ...-gnudietlibc
	# instead.
	#
	pkg_dietlibc_orig_target="$arch_target"
	arch_target="${arch_target%-*}-gnudietlibc"
	if atstage native; then
		arch_build="${arch_target}"
	fi

	if [ -d $base/build/$SDECFG_ID/TOOLCHAIN/$toolsdir/diet-bin ] ; then
		PATH="$base/build/$SDECFG_ID/TOOLCHAIN/$toolsdir/diet-bin:$PATH"
	fi

	if [ "$SDECFG_DIETLIBC_DYN" = "1" ]
	then dietbin="diet-dyn" ; diet_dynamic_static="dynamic"
	else dietbin="diet" ; diet_dynamic_static="static" ; fi

	echo_status "Preparing configuration to build this package with dietlibc ("${diet_dynamic_static}"ally)."

	if atstage native; then
		var_remove CC_WRAPPER_INSERT " " "-pipe"
		var_append CC_WRAPPER_REMOVE " " "-pipe"

		var_remove GCC_WRAPPER_INSERT " " "-pipe"
		var_append GCC_WRAPPER_REMOVE " " "-pipe"

		var_remove KCC_WRAPPER_INSERT " " "-pipe"
		var_append KCC_WRAPPER_REMOVE " " "-pipe"
	fi

	if [ "$GCC_WRAPPER_INSERT" != "${GCC_WRAPPER_INSERT/-fstack-protector}" ]; then
		echo_warning 'Disabling GCC stack-protector for dietlibc-based package.'
		var_remove GCC_WRAPPER_INSERT ' ' '-fstack-protector'
	fi

	# patch has problems with Plan A patching so we switch with -x 16 to Plan B
	atstage rebuild && var_append patchopt ' ' '-x 16'

	case "$pkg" in
		dietlibc)
			;;

		linux*)
			echo_status "Dietlibc not utilized for this package."
			if atstage native && [ "$diet_dynamic_static" == "static" ]; then
			  var_append GCC_WRAPPER_INSERT " " "-static"
			fi
			;;

		iptables|mmv|grsecurity)
			echo_status "Enabling non-portable GNU features."
			var_append CC_WRAPPER_INSERT " " "-D_GNU_SOURCE"
			set_wrapper_and_misc
			;;

		coreutils|findutils|curl|procps|radiusclient-ng|libpcap|binutils|udhcp|file|libxml|lvm2|iputils|bsdiff)
			echo_status "Enabling non-portable BSD features."
			var_append CC_WRAPPER_INSERT " " "-D_BSD_SOURCE"
			set_wrapper_and_misc
			;;

		e2fsprogs|util-linux|ash|irda-utils|ppp|dropbear|nullmailer|traceroute)
			echo_status "Enabling non-portable GNU and BSD features."
			var_append CC_WRAPPER_INSERT " " "-D_GNU_SOURCE -D_BSD_SOURCE"
			set_wrapper_and_misc
			;;
		nvi)
			echo_status "Enabling non-portable GNU, BSD features and types."
			var_append CC_WRAPPER_INSERT " " "-D_GNU_SOURCE -D_BSD_SOURCE"
			var_append CC_WRAPPER_INSERT " " "-D__BIT_TYPES_DEFINED__"
			set_wrapper_and_misc
			;;
		iproute2)
			echo_status "Setting __GLIBC__=3 to build the package!"
			var_append CC_WRAPPER_INSERT " " "-D__GLIBC__=3"
			set_wrapper_and_misc
			;;
		net-tools)
			echo_status "Enabling non-portable GNU, BSD features and a lot of other tweaks."
			var_append CC_WRAPPER_INSERT " " "-D_GNU_SOURCE -D_BSD_SOURCE"
			var_append CC_WRAPPER_INSERT " " "-D__GLIBC__"
			var_append CC_WRAPPER_INSERT " " "-D_LINUX_IN6_H"
			set_wrapper_and_misc
			;;
		#squid)
			#var_append CC_WRAPPER_INSERT " " "-DHAVE_SYS_TIME_H"
			#set_wrapper_and_misc
		#	;;
		*)
			echo_status "Using default dietlibc options!"
			set_wrapper_and_misc
			;;
	esac

	case "$pkg" in
		sed|lilo|findutils|net-tools|dropbear|udev|rng-tools|udhcp|psmisc|bsdiff)
			add_lib "-lcompat"
			;;
		coreutils)
			add_lib "-lcompat"
			var_remove patchfiles " " "*acl-xattr.patch"
			;;
		ncurses)
			var_append extraconfopt " " \
			  "--without-cxx-binding --without-cxx"
			;;
		util-linux)
			add_lib "-lrpc -lcompat"
			;;
		dump)
			var_append extraconfopt " " "--disable-rmt"
			;;
		perl)
			add_lib "-lm -lcompat"
			;;
		openssl|gettext)
			remove_lib "-ldl"
			;;
		iptables)
			add_lib "-lrpc"
			remove_lib "-lnsl"
                        ;;
		gawk)
			[ "$diet_dynamic_static" == "static" ] || add_lib "-ldl"
			;;

		procps)
			if [ "$diet_dynamic_static" == "static"  ]; then
				var_append makeopt ' ' 'SHARED=0'
				var_append makeinstopt ' ' 'SHARED=0'
			fi
			;;
	esac

	var_append patchfiles " " \
	  `ls $base/package/base/dietlibc/patches/pkg_$pkg.patch 2>/dev/null`
	if ! atstage toolchain && [ "$SDECFG_LIBC" = "dietlibc" ]; then
	  var_append patchfiles " " \
	  `ls $base/package/base/dietlibc/patches/pkg_$pkg.chroot.patch 2>/dev/null`
	fi
fi

pkg_udev_this_is_the_second_run=1