#!/bin/sh
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../dietlibc/parse-config-9
# Copyright (C) 2006 - 2010 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# Copyright (C) 1998 - 2004 Clifford Wolf
#
# 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.
# --- SDE-COPYRIGHT-NOTE-END ---

# --- FUNCTIONS-BEGIN ---

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"
}

# --- FUNCTIONS-END --

# decide wether to compile the packet with dietlibc or not
if [ "$SDECFG_LIBC" = "dietlibc" -o "$pkg" = "dietlibc" ]; then
	pkg_dietlibc_useit=1
elif hasflag USE_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
if atstage toolchain; then
	pkg_dietlibc_useit=0
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 ..-dietlibc
	# instead.
	#
	pkg_dietlibc_orig_target="$arch_target"
	arch_target="${arch_target/-gnu/-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
	if atstage rebuild; then
		patchopt="-bfp1 -x 16 -z .orig"
	fi

	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|hotplug2|xmlrpc-c)
			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|libevent)
			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|busybox|e2fsprogs|nbd)
			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"
                        ;;
                grsecurity)
			var_append makeopt " " "nopam"
                        ;;
		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