#! /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-6.patch
	binutils-indirect-1.patch
	binutils-shr-85.patch
	ld-env-10.patch
	binutils-error-4.patch
	binutils-signed-3.patch
	binutils-weakdef-1.patch
	bfd-64k-2.patch
	binutils-sec64k-2.patch
	ld-test-m68k-got-1.patch
"

removed="
ld/testsuite/ld-m68k/got-12.s
ld/testsuite/ld-m68k/got-13.s
ld/testsuite/ld-m68k/got-14.s
ld/testsuite/ld-m68k/got-15.s
ld/testsuite/ld-m68k/got-34.s
ld/testsuite/ld-m68k/got-35.s
ld/testsuite/ld-m68k/xgot-15.s
"

if [ -n "$removed" ]
then
  rm $removed || exit 1
fi

mk68k_gots="
ld/testsuite/ld-m68k/got-multigot-12-13-14-34-35-ok.d
ld/testsuite/ld-m68k/got-multigot-14-ok.d
ld/testsuite/ld-m68k/got-negative-12-13-14-34-ok.d
ld/testsuite/ld-m68k/got-negative-14-ok.d
ld/testsuite/ld-m68k/got-single-12-ok.d
ld/testsuite/ld-m68k/got-xgot-12-13-14-15-34-35-ok.d
ld/testsuite/ld-m68k/got-xgot-15-ok.d
"

for f in $mk68k_gots
do
  grep -v "#source" $f | grep -v "^\^\[0-9a-f\]" > $f.got || exit 1
  mv $f.got $f
done

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
