#!/bin/bash

# binutils.SlackBuild
# by Stuart Winter <mozes@slackware.com> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 30-May-2004

# Record toolchain & other info for the build log:
slackbuildinfo

# Paths to skeleton port's source & real Slackware source tree:
export CWD=$SLACKSOURCE/$PKGSERIES/$PKGNAM
export PORTCWD=$PWD

# Temporary build locations:
export TMPBUILD=$TMP/build-$PKGNAM
export PKG=$TMP/package-$PKGNAM
mkpkgdirs # Delete & re-create temporary directories then cd into $TMPBUILD

# Determine the CFLAGS for the known architectures:
case $ARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv4t" 
            export LIBDIRSUFFIX="" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
# It's easier to stick with what Debian are doing.  I've been bitten too many
# times in the past by weirdness with binutils.
tar xvvf $PORTCWD/sources/$PKGNAM*$VERSION*tar*z*
#tar xvvf $CWD/$PKGNAM*$VERSION*?z*
cd $PKGNAM* || exit 1
slackhousekeeping

# Apply patches:
zcat $PORTCWD/sources/$PKGNAM*diff* | patch -p1 || exit 1
for i in debian/patches/*patch ; do
    auto_apply_patch $i || exit 1
done

# Configure:
CFLAGS="$SLKCFLAGS" \
CPPFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure  \
   --prefix=/usr \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --with-docdir=/usr/doc/binutils-$VERSION \
   --enable-targets=$ARCH-slackware-linux-gnueabi \
   --enable-shared \
   --build=$ARCH-slackware-linux-gnueabi || failconfig
# Not for ARM:
#   --enable-multilib \
#   --enable-64-bit-bfd \

# Build:
make clean
make $NUMJOBS || failmake
make info

# Install into package:
make install DESTDIR=$PKG || exit 1

# "make install" skips this, but binutils.spec doesn't.  Sneaky, huh?
cp -a include/libiberty.h $PKG/usr/include/libiberty.h

# Differentiate between BSD strings and GNU strings
( cd $PKG/usr/bin ; mv strings strings-GNU )
( cd $PKG/usr/man/man1 ; mv strings.1 strings-GNU.1 )

# Move ldscripts to /usr/lib${LIBDIRSUFFIX}, and then put symlinks in place
mv $PKG/usr/${ARCH}-slackware-linux-gnueabi/lib/ldscripts $PKG/usr/lib${LIBDIRSUFFIX}
( cd $PKG/usr/${ARCH}-slackware-linux-gnueabi
  ln -s /usr/lib${LIBDIRSUFFIX}/ldscripts lib/ldscripts
  for FILE in ar as ld nm objcopy objdump ranlib strip ; do
    if [ -r "/usr/bin/$FILE" ]; then
      rm -f bin/$FILE
      ln -s /usr/bin/$FILE bin/$FILE
    fi
  done
)

# Remove some unneeded man pages, and then compress the rest
rm -f $PKG/usr/man/man1/{dlltool,windres}.1
( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $(find . -type l) ; do ln -s $( readlink $i ).gz $i.gz ; rm $i ; done
)

# Copy docs:
mkdir -p $PKG/usr/doc/binutils-$VERSION
cp \
  $CWD/release.binutils-* \
  COPYING* ChangeLog.linux MAI* README* \
  $PKG/usr/doc/binutils-$VERSION
changelogliposuction ChangeLog $PKGNAM $VERSION # Trim down a "ChangeLog" file

# If necessary, start the fakeroot server so we can set file/dir ownerships:
start_fakeroot

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slackgzpages -i # compress man & info pages and delete usr/info/dir
slackslack      # chown -R root:root, chmod -R og-w, slackchown, slack644docs
slackdesc       # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n
 
# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links

cat << EOF

######################################################################
Now rebuild oprofile because it links to libbfd (part of this package)
######################################################################

EOF
