# $FreeWRT$
#-
# This file is part of the FreeWRT project. FreeWRT is copyrighted
# material, please see the LICENCE file in the top-level directory
# or at http://www.freewrt.org/licence for details.

shopt -s extglob

SELF=${0##*/}

if [[ -z $prefix ]]; then
	echo >&2 "$SELF: strip command not defined ('prefix' variable not set)"
	exit 1
fi

TARGETS=$*

if [[ -z $TARGETS ]]; then
	echo >&2 "$SELF: no directories / files specified"
	echo >&2 "usage: $SELF [PATH...]"
	exit 1
fi

find $TARGETS -type f -a -exec file {} \; | \
    sed -n -e 's/^\(.*\):.*ELF.*\(executable\|relocatable\|shared object\).*, not stripped/\1:\2/p' |
    while IFS=: read F S; do
	T="${prefix}strip -R .comment"
	if [[ $F = */lib/modules/2.*.@(o|ko) ]]; then
		S="kernel module $S"
		# kernel module parametres must not be stripped off
		T="$T --strip-unneeded $(echo $(${prefix}nm $F | \
		    sed -n -e '/__param_/s/^.*__param_/-K /p' \
		    -e '/__module_parm_/s/^.*__module_parm_/-K /p'))"
	fi
	echo "$SELF: $F:$S"
	# The following line intentionally left in to prove
	# to n0-1@ that I do test my changes… yes it works.
	#echo "D: $T $F"
	eval "$T $F"
done
