#!/bin/sh
CWD=`pwd`
TMP=/tmp
PKG=$TMP/package-binutils
VERSION=2.15.94.0.1
ARCH=alpha
BUILD=2
TARGET=$ARCH-alphaslack-linux

mkdir -p $PKG/usr
( cd $TMP
rm -rf binutils-$VERSION
tar xjf $CWD/binutils-$VERSION.tar.bz2
cd binutils-$VERSION
chown -R root.root .
find . -perm 777 -exec chmod 755 {} \;
find . -perm 664 -exec chmod 644 {} \;

# Build for a standard glibc2-based Linux system:
CFLAGS="-O2" LDFLAGS=-s \
./configure \
    --prefix=/usr \
    --enable-shared \
    --enable-64-bit-bfd \
    --without-included-gettext \
    --enable-targets=$TARGET,$TARGET-gnulibc1 \
    $TARGET
make clean
make
# Needed to link ksymoops:
make install
# We need to clear ldscripts/ because binutils doesn't implement DESTDIR everywhere:
rm -rf /usr/lib/ldscripts /usr/$TARGET/lib/ldscripts
# Repopulate it:
make install
# Install into the $PKG location:
make install DESTDIR=$PKG
# Add fresh ldscripts:
cp -a /usr/$TARGET/lib/ldscripts $PKG/usr/lib
# Missing?
cp -a gas/doc/as.1 $PKG/usr/man/man1
# "make install" skips this, but binutils.spec doesn't.  Sneaky, huh?
cp -a include/libiberty.h $PKG/usr/include/libiberty.h
mkdir -p $PKG/usr/doc/binutils-$VERSION
cp -a COPYING* ChangeLog.linux MAI* README* $PKG/usr/doc/binutils-$VERSION
( cd $PKG/usr/bin ; mv strings strings-GNU )
# Add slack-desc:
mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
# Now we must remember to adjust ldscripts (real dir is /usr/lib/ldscripts/,
# links go in /usr/*-linux/lib/ ), and the bin links:
mkdir -p $PKG/usr/$TARGET/lib
( cd $PKG/usr/$TARGET/lib ; rm -rf ldscripts ; ln -sf /usr/lib/ldscripts . )
mkdir -p $PKG/usr/$TARGET/bin
( cd $PKG/usr/$TARGET/bin
  for file in * ; do
    if [ -r "/usr/bin/$file" ]; then
      rm $file
      ln -sf /usr/bin/$file .
    fi
  done
)
# By adding ksymoops to the binutils package, we can use the dynamic
# libbfd and make the binary about 500K smaller.
cd $CWD
sh ./ksymoops.build
gzip -9 $PKG/usr/man/man1/* $PKG/usr/info/*.info
rm -rf $PKG/usr/info/dir
cd $PKG
 find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded
 find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded
chown -R root.bin $PKG/usr/bin
makepkg -l y -c n $TMP/binutils-$VERSION-$ARCH-$BUILD.tgz
) 2>&1 | tee $TMP/binutils.build.log
