#!/bin/bash
ulimit -s unlimited
shopt -s extglob

# bash.SlackBuild
# Modified by Stuart Winter <mozes@slackware.com> for Slackware ARM.
# 12-Mar-2004
#
# Copyright 2005-2010  Patrick J. Volkerding, Sebeka, Minnesota, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

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

# Paths to skeleton port's source & real Slackware source tree:
slackset_var_cwds

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

# Extract source:
tar xvvf $CWD/$PKGNAM-[0-9]*.tar.!(*sign|*asc|*sig)
cd $PKGNAM*/ || exit 1
slackhousekeeping

# Determine bash patchlevel:
PATCHLEVEL="$( cd $CWD/bash-${VERSION}-patches ; /bin/ls bash5?-??? 2> /dev/null | tail -1 | cut -f 2 -d -)"
if [ "$PATCHLEVEL" = "" ]; then
   PATCHLEVEL=000
fi
echo "Patch level: $PATCHLEVEL"

# Fix .so errors in manpages (needed for strict implementations such as mandoc):
zcat $CWD/bash_manpages.patch.gz | patch -p1 --verbose || exit 1

# Apply patches (these reside in the real Slackware source tree):
if [ -d $CWD/bash-${VERSION}-patches ]; then
  for pf in $CWD/bash-${VERSION}-patches/* ; do
   file $pf | egrep -q 'ASCII|text' && auto_apply_patch $pf
  done
fi

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --docdir=/usr/doc/$PKGNAM-$VERSION \
   --host=${SLK_ARCH_HOST} \
   --build=${SLK_ARCH_BUILD} || failconfig

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

# Install into package:
make install DESTDIR=$PKG || failinstall
mkdir -p $PKG/bin
mv -fv $PKG/usr/bin/bash $PKG/bin/bash5.new
# We don't include the "bashbug" script.
rm -rf $PKG/usr/bin $PKG/usr/man/man1/bashbug.1
# bash.1 is already installed by "make install"
install -vpm644 doc/{builtins.1,rbash.1} $PKG/usr/man/man1

# Copy documentation:
mkdir -p $PKG/usr/doc/bash-$VERSION
cp -a AUTHORS COMPAT COPYING* INSTALL MANIFEST NOTES \
  README* Y2K doc/FAQ doc/INTRO \
  $PKG/usr/doc/bash-$VERSION
( cd doc ; groff -ms -Tascii article.ms > $PKG/usr/doc/bash-$VERSION/article.txt )
changelogliposuction ChangeLog $PKGNAM $VERSION
changelogliposuction NEWS $PKGNAM $VERSION
changelogliposuction CHANGES $PKGNAM $VERSION

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall   # strip all .a archives and all ELFs
slack_delete_lafiles # delete usr/lib{,64}/*.la
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 SLACKPACKAGE=$PKGNAM-$VERSION.$PATCHLEVEL-$PKGARCH-$BUILD.txz
slackmp         # run makepkg -l y -c n

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