#! /bin/sh

# --disable-nls  for saving memory and disk space

s=../../local/binutils-2.11
rm $s/bfd/libbfd.h

emx-config-cross $s --build="`cat ../../SYSTEM`" \
  --host=arm-epoc-pe --target=arm-epoc-pe --prefix=/usr \
  --disable-nls

mv Makefile Makefile.orig
sed '/^SUBDIRS =/s/ gprof / /' Makefile.orig |
  sed -e '/^	all-gprof /d' \
      -e 's/^LDFLAGS *= *$/LDFLAGS = -heap 32M/' >Makefile
touch -r Makefile.orig Makefile

for x in `find . -type f -name config.h`
do
  if fgrep -q USE_BINARY_FOPEN $x
  then
    mv $x $x.orig
    sed 's!^.*#undef USE_BINARY_FOPEN.*$!#define USE_BINARY_FOPEN 1!' $x.orig >$x
    touch -r $x.orig $x
  fi
done

for x in `find . -type f -name Makefile`
do
  if fgrep -q fopen-same.h $x
  then
    mv $x $x.orig
    sed 's!fopen-same.h!fopen-bin.h!' $x.orig >$x
    touch -r $x.orig $x
  fi
done

patch -p0 <../../patches/libiberty-Makefile.patch
patch -p0 <../../patches/bfd-Makefile.patch

(cd bfd;make headers)
