#! /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-4.patch
	binutils-shr-97.patch
	binutils-error-12.patch
	binutils-signed-8.patch
	binutils-weakdef-2.patch
	bfd-64k-2.patch
	binutils-lto-mixed-13.patch
	binutils-pr12639-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
