#! /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="
	binutils-indirect-5.patch
	binutils-shr-100.patch
	binutils-error-14.patch
	binutils-signed-8.patch
	binutils-weakdef-2.patch
	bfd-64k-2.patch
	binutils-lto-mixed-18.patch
	binutils-pr12639-2.patch
	binutils-x32-1.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
