#!/bin/bash

# binutils.SlackBuild
# by Stuart Winter <stuart@armedslack.org> 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 $PORTARCH in
   arm)     export SLKCFLAGS="-O2 -march=armv3 -mtune=xscale" ;;
   powerpc) export SLKCFLAGS="-O2" ;;
   sparc)   export SLKCFLAGS="-O2" ;;
   hppa)    export SLKCFLAGS="-O2" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
#tar xvvf $CWD/$PKGNAM-$VERSION.tar.bz2
tar xvvf $PORTCWD/sources/$PKGNAM-$VERSION.tar.bz2
cd $PKGNAM-$VERSION
slackhousekeeping

# Configure:
CFLAGS="$SLKCFLAGS" \
CPPFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure  \
   --prefix=/usr \
   --enable-shared \
   --enable-targets=$ARCH-slackware-linux \
   --build=$ARCH-slackware-linux || failconfig
# Not for ARM:
#   --enable-64-bit-bfd \

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

# Needed to link ksymoops:
make install
# We need to clear ldscripts/ because binutils doesn't implement DESTDIR everywhere:
rm -rf /usr/lib/ldscripts /usr/${ARCH}-slackware-linux/lib/ldscripts
# Repopulate it:
make install
# Install into the $PKG location:
make install DESTDIR=$PKG
# Add fresh ldscripts:
cp -favv /usr/${ARCH}-slackware-linux/lib/ldscripts $PKG/usr/lib

# Missing?
cp -favv gas/doc/as.1 $PKG/usr/man/man1

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

# Install docs:
mkdir -p $PKG/usr/doc/binutils-$VERSION
cp -favv $CWD/release.binutils-* $PKG/usr/doc/binutils-$VERSION
cp -favv COPYING* ChangeLog.linux MAI* README* $PKG/usr/doc/binutils-$VERSION

# These man pages are junk:
rm -f $PKG/usr/man/man1/{dlltool.1,windres.1}

# Install info pages:
rm -rf $PKG/usr/info
mkdir -p $PKG/usr/info
find . -name "as.info*" -exec cp -favv {} $PKG/usr/info \;
find . -name "bfd.info*" -exec cp -favv {} $PKG/usr/info \;
find . -name "ld.info*" -exec cp -favv {} $PKG/usr/info \;
find . -name "gasp.info*" -exec cp -favv {} $PKG/usr/info \;
find . -name "gprof.info*" -exec cp -favv {} $PKG/usr/info \;
find . -name "binutils.info*" -exec cp -favv {} $PKG/usr/info \;

# We use BSD strings in Slackware:
( cd $PKG/usr/bin ; mv strings strings-GNU )
( cd $PKG/usr/man/man1 ; mv strings.1 strings-GNU.1 )

# 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/${ARCH}-slackware-linux/lib
( cd $PKG/usr/${ARCH}-slackware-linux/lib ; rm -rf ldscripts ; ln -sf /usr/lib/ldscripts . )
mkdir -p $PKG/usr/${ARCH}-slackware-linux/bin
( cd $PKG/usr/${ARCH}-slackware-linux/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.
sh $PORTCWD/ksymoops.build

# 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
