#!/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/$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=armv4 -mtune=xscale" ;;
   *)       export SLKCFLAGS="-O2" ;;
esac

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.gz
cd $PKGNAM*
slackhousekeeping

# Determine bash patchlevel:
#PATCHLEVEL="$( cd $CWD/bash-${VERSION}-patches ; /bin/ls bash32-??? | tail -1 | cut -f 2 -d - 2> /dev/null )"
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

# Apply patches (these reside in the real Slackware source tree):
#if [ -d $CWD/bash-${VERSION}-patches ]; then
#  ( cd $CWD/bash-${VERSION}-patches ; cat bash3?-??? ) | patch -p0 --verbose || exit 1
#fi
for patch in $CWD/patches/bash* ; do
  zcat $patch | patch -p0 --verbose || exit 1
done

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

# Build:
make $NUMJOBS || failmake

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

# Install docs & additional man pages:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
cp -a AUTHORS CHANGES COMPAT COPYING INSTALL MANIFEST NEWS NOTES \
      README Y2K doc/FAQ doc/INTRO $PKG/usr/doc/$PKGNAM-$VERSION
cd doc
install -vpm644 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:
export VERSION=$VERSION.$PATCHLEVEL
slackmp         # run makepkg -l y -c n

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