#!/bin/bash
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-src/portage/bin/prepstrip,v 1.23 2004/10/19 04:58:42 carpaski Exp $

if [ "${FEATURES//*nostrip*/true}" == "true" ] || [ "${RESTRICT//*nostrip*/true}" == "true" ] ; then
	echo "nostrip"
	STRIP="/bin/false"
else
	if [ ! -z "${CBUILD}" ] && [ "${CBUILD}" != "${CHOST}" ]; then
		STRIP=${CHOST}-strip
	else
		STRIP=strip
	fi
fi

echo "strip: "
retval=0
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
						echo -e "\aText relocations require a lot of extra work to be preformed by the"
						echo -e "\adynamic linker which will cause serious performance impact on IA-32"
						echo -e "\aand might not function properly on other architectures hppa for example."
						echo -e "\aIf you are a programmer please take a closer look at this package and"
						echo -e "\aconsider writing a patch which addresses this problem."
						retval=1
					fi >&2
				fi
			fi
		fi
	fi
done
exit ${retval}
