#!/bin/sh
#mingw64_find-debuginfo.sh - automagically generate debug info and file list
#for inclusion in an rpm spec file for mingw64-* packages.

if [ -z "$1" ] ; then BUILDDIR="."
else BUILDDIR=$1
fi

for f in `find $RPM_BUILD_ROOT -type f -name "*.exe" -or -name "*.dll"`
do
	case $(x86_64-w64-mingw32-objdump -h $f 2>/dev/null | egrep -o '(debug[\.a-z_]*|gnu.version)') in
	    *debuglink*) continue ;;
	    *debug*) ;;
	    *gnu.version*)
		echo "WARNING: "`echo $f | sed -e "s,^$RPM_BUILD_ROOT/*,/,"`" is already stripped!"
		continue
		;;
	    *) continue ;;
	esac

	echo extracting debug info from $f
	x86_64-w64-mingw32-objcopy --only-keep-debug $f $f.debug || :
	pushd `dirname $f`
	x86_64-w64-mingw32-objcopy --add-gnu-debuglink=`basename $f.debug` --strip-unneeded `basename $f` || :
	popd
done

find $RPM_BUILD_ROOT -type f -name "*.exe.debug" -or -name "*.dll.debug" -or -name "*.exe.mdb" -or -name "*.dll.mdb" |
    sed -n -e "s#^$RPM_BUILD_ROOT##p" > $BUILDDIR/debugfiles.list


