#! /bin/sh

# If you don't use rpm, you can use this file to apply additional Linux
# patches. At the top level of the binutils source tree, do
#
# /bin/sh patches/README
#
# You may have to do
#
# cd bfd
# make headers
#
# if the build fails.

dir=`dirname $0`
clean=$1

patches="
	bfd-ref_addr-7.patch
	binutils-indirect-1.patch
	binutils-shr-85.patch
	ld-env-10.patch
	binutils-error-6.patch
	binutils-signed-3.patch
	binutils-weakdef-1.patch
	bfd-64k-2.patch
	binutils-sec64k-2.patch
"

if [ -n "$clean" ]
then
  rm -f ChangeLog.patches
  touch ChangeLog.patches
fi

for p in $patches
do
  if [ -n "$clean" ]
  then
    sed -e "/^---/,\$d" $dir/$p >> ChangeLog.patches
  else
    suffix=$(echo $p | sed -e "s/.*-\([^-]\+\)-[0-9]\+.patch/\1/")
    backup="-b --suffix .$suffix"
  fi
  case $p in
  binutils*)
    level=1
    ;;
  *)
    level=0
    ;;
  esac
  patch -E -p$level $backup < $dir/$p || exit 1
done
find -name "*.orig" | xargs rm -fv
find -name "*.gmo" | xargs rm -fv
