#!/bin/bash
# Copyright 1999-2003 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-src/portage/bin/prepstrip,v 1.20 2004/07/10 04:32:32 carpaski Exp $

if [ "${FEATURES//*nostrip*/true}" == "true" ] || [ "${RESTRICT//*nostrip*/true}" == "true" ] ; then
	echo "nostrip"
	exit 0
fi

if [ ! -z "${CBUILD}" ] && [ "${CBUILD}" != "${CHOST}" ]; then
	STRIP=${CHOST}-strip
else
	STRIP=strip
fi

echo "strip: "
for x in "$@"; do # "$@" quotes each element... Plays nice with spaces.
	if [ -d "${x}" ]; then
		# We only want files. So make a pass for each directory and call again.
		find "${x}" -type f \( -perm -0100 -or -perm -0010 -or -perm -0001 -or -name '*.so' -or -name '*.so.*' \) -print0 |
				$XARGS -0 -n500 prepstrip
	else
		f=$(file "${x}")
		if [ -z "${f/*SB executable*/}" ]; then
			echo "   ${x:${#D}:${#x}}"
			${STRIP} "${x}"
		fi
		if [ -z "${f/*SB shared object*/}" ]; then
			echo "   ${x:${#D}:${#x}}"
			${STRIP} --strip-unneeded "${x}"

			if [ -x /usr/bin/readelf ] ; then
				/usr/bin/readelf -d "${x}" | grep TEXTREL > /dev/null
				if [ $? = 0 ]; then
					echo "	${x:${#D}:${#x}} will contain runtime text relocations"
					if [ "${FEATURES//*strict*/true}" == "true" ] ; then
						ewarn "Text relocations require a lot of extra work to be preformed by the"
						ewarn "dynamic linker which will cause serious performance impact on IA-32"
						ewarn "and might not function properly on other architectures hppa for example."
						ewarn "If you are a programmer please take a closer look at this package and"
						ewarn "consider writing a patch which addresses this problem."
						exit 1
					fi
				fi
			fi
		fi
	fi
done
