#!/bin/bash

# bash.SlackBuild
# by Stuart Winter <stuart@armedslack.org> for the Slackware porting Project.
# Heavily based on the original Slackware build script.
# 12-Mar-2004

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

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

# Temporary build locations:
export TMPBUILD=$TMP/build-$PACKAGE
export PKG=$TMP/package-$PACKAGE
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 zxvvf $CWD/$PACKAGE-$VERSION.tar.gz
cd $PACKAGE-$VERSION
slackhousekeeping

# Apply patches (these reside in the real Slackware source tree):
for patch in $CWD/patches/bash* ; do
  zcat $patch | patch -p0 --verbose || failpatch
done

# Determine bash patchlevel:
PATCHLEVEL=`( cd $CWD/patches ; /bin/ls *gz | tail -1 | cut -f 2 -d - | cut -f 1 -d . 2> /dev/null )`
if [ "$PATCHLEVEL" = "" ]; then
  PATCHLEVEL=0
fi

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   $ARCH-slackware-linux || failconfig

# Build:
make || failmake

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

# Install docs & additional man pages:
mkdir -p $PKG/usr/doc/$PACKAGE-$VERSION
cp -a AUTHORS CHANGES COMPAT COPYING INSTALL MANIFEST NEWS NOTES \
      README Y2K doc/FAQ doc/INTRO $PKG/usr/doc/$PACKAGE-$VERSION
cd doc
install -pm644 builtins.1 rbash.1 $PKG/usr/man/man1/

# Enter the package's root:
cd $PKG
mkdir bin
# bash lives in /bin on all Linux systems.
# We rename it in Slackware so we can upgrade it.
# otherwise it'd be removepkg'd wilst being upgradepkg'd/installpkg'd
# which would be a bad thing.
mv usr/bin/bash bin/bash2.new

# We don't include the "bashbug" script.
rm -rf usr/bin usr/man/man1/bashbug.1

# 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

# Append the patch level to the package version number:
VERSION=$VERSION.$PATCHLEVEL

# Build the package:
if [ $PORTARCH = arm ]; then
   slackmp # run makepkg
  else
   makepkg -l y -c n $PKGSTORE/$PKGSERIES/$PACKAGE-$VERSION-$ARCH-$BUILD.tgz
fi

# Perform any final checks on the package:
cd $PKG
slackhlinks     # search for any hard links
