#!/bin/sh -e
#
# brp-strip - strip ELF binaries.
# Inspired by brp-strip script from RPM source code.
#
# Copyright (C) 2000,2003  Dmitry V. Levin <ldv@altlinux.org>
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#

. /usr/lib/rpm/functions
ValidateBuildRoot

Usage()
{
	[ "$1" = 0 ] || exec >&2
	cat <<EOF
brp-strip - strip ELF binaries.

brp-strip is free software, covered by the GNU General Public License.
brp-strip comes with ABSOLUTELY NO WARRANTY, see license for details.

Usage: $PROG [options] [files]

Valid options are:
-h, --help
	Show a summary of the options to brp-strip and exit.
-p, --preserve-dates
	Preserve modified/access timestamps of stripped files.
-R <name>, --remove-section=<name>
	Remove the named section from files. This option may be given more than once.
-s, --strip-all
	Remove all symbols.
-g, -S, --strip-debug
	Remove all debugging symbols.
--skip-files=<pattern>
	Skip files matched by specified pattern.
--strip-unneeded
	Remove all symbols not needed by relocations.
-N, --strip-symbol=<name>
	Do not copy named symbol.
-K, --keep-symbol=<name>
	Only copy named symbol.
-x, --discard-all
	Remove all non-global symbols.
-X, --discard-locals
	Remove any compiler-generated symbols.
-v, --verbose
	List all object files modified.
-T <name>, --topdir=<name>
	Start file lookup at named directory, \$RPM_BUILD_ROOT by default.

files is list of files or directory trees where files to be stripped.
By default, all files in TOPDIR, specified by \$RPM_STRIP_METHOD, will be stripped.
EOF

	[ -n "$1" ] && exit "$1" || exit
}

TEMP=`getopt -n "$PROG" -o hpR:sgSN:K:xXvT: -l help,preserve-dates,remove-section:,skip-files:,strip-all,strip-unneeded,strip-symbol:,keep-symbol:,discard-all,discard-locals,verbose,topdir: -- "$@"` || Usage
eval set -- "$TEMP"

: ${RPM_STRIP_SKIPLIST:=}
: ${RPM_STRIP_TOPDIR:=}
export RPM_STRIP_SKIPLIST

export STRIP_FORCED=
export STRIP_FORCED_OPTS=

AddForcedOpts()
{
	if [ -z "$STRIP_FORCED_OPTS" ]; then
		STRIP_FORCED_OPTS="$*"
	else
		STRIP_FORCED_OPTS="$STRIP_FORCED_OPTS $*"
	fi
}

while :; do
	case "$1" in
		-h|--help)
			Usage 0
			;;
		-p|--preserve-dates)
			AddForcedOpts -p
			shift
			;;
		-R|--remove-section)
			shift
			AddForcedOpts -R "$1"
			shift
			STRIP_FORCED=1
			;;
		--skip-files)
			shift
			RPM_STRIP_SKIPLIST="$RPM_STRIP_SKIPLIST $1"
			shift
			;;
		-s|--strip-all)
			AddForcedOpts -s
			shift
			STRIP_FORCED=1
			;;
		-g|-S|--strip-debug)
			AddForcedOpts -g
			shift
			STRIP_FORCED=1
			;;
		--strip-unneeded)
			AddForcedOpts --strip-unneeded
			shift
			STRIP_FORCED=1
			;;
		-N|--strip-symbol)
			shift
			AddForcedOpts -N "$1"
			shift
			STRIP_FORCED=1
			;;
		-K|--keep-symbol)
			shift
			AddForcedOpts -K "$1"
			shift
			STRIP_FORCED=1
			;;
		-x|--discard-all)
			AddForcedOpts -x
			shift
			STRIP_FORCED=1
			;;
		-X|--discard-locals)
			AddForcedOpts -X
			shift
			STRIP_FORCED=1
			;;
		-v|--verbose)
			AddForcedOpts -v
			shift
			;;
		-T|--topdir)
			shift
			TOPDIR="$1"
			shift
			;;
		--)
			shift
			break
			;;
		*)
			Fatal "unrecognized option: $1"
			;;
	esac
done

if [ -z "$TOPDIR" ]; then
	TOPDIR="$RPM_BUILD_ROOT"
	cd "$TOPDIR"
	cd "$OLDPWD"
	[ -d "$TOPDIR$RPM_STRIP_TOPDIR" ] || exit 0
	TOPDIR="$TOPDIR$RPM_STRIP_TOPDIR"
else
	cd "$TOPDIR"
	cd "$OLDPWD"
fi

TOPDIR="$(printf %s "$TOPDIR" |sed '
s:/\(\./\)\+:/:g
s:/\+:/:g
s:/$::
')"

SHOW_METHODS=
AddShowMethods()
{
	if [ -z "$SHOW_METHODS" ]; then
		SHOW_METHODS="$*"
	else
		SHOW_METHODS="$SHOW_METHODS,$*"
	fi
}

export STRIP_EXECUTABLE=
export STRIP_RELOCATABLE=
export STRIP_SHARED=
export STRIP_STATIC=
for t in `printf %s "$RPM_STRIP_METHOD" |tr , ' '`; do
	case "$t" in
		false|no|none|off|skip)
			exit 0
			;;
		exec*)
			STRIP_EXECUTABLE=executable
			AddShowMethods executable
			;;
		reloc*)
			STRIP_RELOCATABLE=relocatable
			AddShowMethods relocatable
			;;
		share*)
			STRIP_SHARED=shared
			AddShowMethods shared
			;;
		static*)
			STRIP_STATIC=static
			AddShowMethods static
			;;
		*)
			Fatal "Unrecognized strip method: $t"
			;;
	esac
done

if [ -z "$STRIP_EXECUTABLE" -a -z "$STRIP_RELOCATABLE" -a -z "$STRIP_SHARED" -a -z "$STRIP_STATIC" ]; then
	# Nothing to do
	exit 0
fi

StripTree()
{
	echo "Stripping binaries in $1 ($SHOW_METHODS)"
	find "$1" -type f -print0 |xargs -r0 /usr/lib/rpm/strip_files || return 1
}

if [ -n "$*" ]; then
	for d in "$@"; do
		if [ -d "$d" ]; then
			StripTree "$d"
		else
			/usr/lib/rpm/strip_files "$d"
		fi
	done
else
	[ -n "$TOPDIR" ] || Fatal "non-/ TOPDIR expected"

	StripTree "$TOPDIR"
fi
