# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/sys-libs/glibc/files/eblits/src_unpack.eblit,v 1.8 2009/05/17 22:17:56 vapier Exp $

int_to_KV() {
	local version=$1 major minor micro
	major=$((version / 65536))
	minor=$(((version % 65536) / 256))
	micro=$((version % 256))
	echo ${major}.${minor}.${micro}
}

eend_KV() {
	[[ $(KV_to_int $1) -ge $(KV_to_int $2) ]]
	eend $?
}

get_kheader_version() {
	printf '#include <linux/version.h>\nLINUX_VERSION_CODE\n' | \
	$(tc-getCPP ${CTARGET}) -I "$(alt_build_headers)" | \
	tail -n 1
}

check_nptl_support() {
	# don't care about the compiler here as we arent using it
	just_headers && return

	local run_kv build_kv want_kv
	run_kv=$(int_to_KV $(get_KV))
	build_kv=$(int_to_KV $(get_kheader_version))
	want_kv=${NPTL_KERN_VER}

	ebegin "Checking gcc for __thread support"
	if ! eend $(want__thread ; echo $?) ; then
		echo
		eerror "Could not find a gcc that supports the __thread directive!"
		eerror "Please update your binutils/gcc and try again."
		die "No __thread support in gcc!"
	fi

	if ! is_crosscompile && ! tc-is-cross-compiler ; then
		# Building fails on an non-supporting kernel
		ebegin "Checking kernel version (${run_kv} >= ${want_kv})"
		if ! eend_KV ${run_kv} ${want_kv} ; then
			echo
			eerror "You need a kernel of at least ${want_kv} for NPTL support!"
			die "Kernel version too low!"
		fi
	fi

	ebegin "Checking linux-headers version (${build_kv} >= ${want_kv})"
	if ! eend_KV ${build_kv} ${want_kv} ; then
		echo
		eerror "You need linux-headers of at least ${want_kv} for NPTL support!"
		die "linux-headers version too low!"
	fi
}

unpack_pkg() {
	local a=${PN}
	[[ -n ${SNAP_VER} ]] && a="${a}-${RELEASE_VER}"
	[[ -n $1 ]] && a="${a}-$1"
	[[ -n ${SNAP_VER} ]] && a="${a}-${SNAP_VER}" || a="${a}-${RELEASE_VER}"
	unpack ${a}.tar.bz2
	[[ -n $1 ]] && mv ${a} $1
}

toolchain-glibc_src_unpack() {
	# Check NPTL support _before_ we unpack things to save some time
	want_nptl && check_nptl_support

	unpack_pkg

	cd "${S}"
	touch locale/C-translit.h #185476 #218003
	[[ -n ${LT_VER}     ]] && unpack_pkg linuxthreads ${LT_VER}
	[[ -n ${PORTS_VER}  ]] && unpack_pkg ports ${PORTS_VER}
	[[ -n ${LIBIDN_VER} ]] && unpack_pkg libidn

	if [[ -n ${PATCH_VER} ]] ; then
		cd "${WORKDIR}"
		unpack glibc-${RELEASE_VER}-patches-${PATCH_VER}.tar.bz2
		# pull out all the addons
		local d
		for d in extra/*/configure ; do
			d=${d%/configure}
			[[ -d ${S}/${d} ]] && die "${d} already exists in \${S}"
			mv "${d}" "${S}" || die "moving ${d} failed"
		done
	fi

	# XXX: We should do the branchupdate, before extracting the manpages and
	# infopages else it does not help much (mtimes change if there is a change
	# to them with branchupdate)
	if [[ -n ${BRANCH_UPDATE} ]] ; then
		cd "${S}"
		epatch "${DISTDIR}"/glibc-${RELEASE_VER}-branch-update-${BRANCH_UPDATE}.patch.bz2

		# Snapshot date patch
		einfo "Patching version to display snapshot date ..."
		sed -i -e "s:\(#define RELEASE\).*:\1 \"${BRANCH_UPDATE}\":" version.h
	fi

	if [[ -n ${MANPAGE_VER} ]] ; then
		cd "${WORKDIR}"
		unpack glibc-manpages-${MANPAGE_VER}.tar.bz2
	fi

	if [[ -n ${INFOPAGE_VER} ]] ; then
		cd "${S}"
		unpack glibc-infopages-${INFOPAGE_VER}.tar.bz2
	fi

	# tag, glibc is it
	cd "${S}"
	[[ -e csu/Banner ]] && die "need new banner location"
	[[ -n ${SNAP_VER} ]] && echo "Gentoo snapshot ${SNAP_VER}" >> csu/Banner
	[[ -n ${BRANCH_UPDATE} ]] && echo "Gentoo branch ${BRANCH_UPDATE}" >> csu/Banner
	if [[ -n ${PATCH_VER} ]] && ! use vanilla ; then
		cd "${S}"
		EPATCH_MULTI_MSG="Applying Gentoo Glibc Patchset ${RELEASE_VER}-${PATCH_VER} ..." \
		EPATCH_EXCLUDE=${GLIBC_PATCH_EXCLUDE} \
		EPATCH_SUFFIX="patch" \
		ARCH=$(tc-arch) \
		epatch "${WORKDIR}"/patches
		echo "Gentoo patchset ${PATCH_VER}" >> csu/Banner
	fi

	gnuconfig_update
}

eblit-glibc-src_unpack() {
	setup_env

	toolchain-glibc_src_unpack

	# Backwards SSP support
	cd "${S}"
# For now, we force everyone to have the extra symbols
#	einfon "Scanning system for __guard to see if we need SSP compat ... "
#	if [[ -n $(scanelf -qyls__guard -F'#s%F' | grep -v '^/lib.*/libc-2.*.so$') ]] ; then
		echo "yes" > "${T}"/.ssp.compat
#	else
#		# ok, a quick scan didnt find it, so lets do a deep scan ...
#		if [[ -n $(scanelf -qyRlps__guard -F'#s%F' | grep -v '^/lib.*/libc-2.*.so$') ]] ; then
#			echo "yes" > "${T}"/.ssp.compat
#		else
#			echo "no" > "${T}"/.ssp.compat
#		fi
#	fi
#	cat "${T}"/.ssp.compat

	# Glibc is stupid sometimes, and doesn't realize that with a
	# static C-Only gcc, -lgcc_eh doesn't exist.
	# http://sources.redhat.com/ml/libc-alpha/2003-09/msg00100.html
	# http://sourceware.org/ml/libc-alpha/2005-02/msg00042.html
	echo 'int main(){}' > "${T}"/gcc_eh_test.c
	if ! $(tc-getCC ${CTARGET}) "${T}"/gcc_eh_test.c -lgcc_eh 2>/dev/null ; then
		sed -i -e 's:-lgcc_eh::' Makeconfig || die "sed gcc_eh"
	fi

	cd "${WORKDIR}"
	find . -type f '(' -size 0 -o -name "*.orig" ')' -exec rm -f {} \;
	find . -name configure -exec touch {} \;

	# Fix permissions on some of the scripts
	chmod u+x "${S}"/scripts/*.sh
}