#!/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-2026  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 xf $CWD/$PKGNAM-[0-9]*.tar.!(*sign|*asc|*sig)
cd $PKGNAM*/ || failextract
slackhousekeeping

# Need unsetting due to them being set conditionally within the upstream SlackBuild:
unset VERSION PATCHLEVEL

# Versions of bundled packages and names of source archives.
# To save on maintenance, and since we carry the same versions as x86 Slackware,
# let's pull details from the x86 trunk:
slack_setvarfromupstream VERSION PATCHLEVEL
[ -z "${PATCHLEVEL}" ] && PATCHLEVEL=000
echo "Version: ${VERSION}"
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 | grep -Eq 'ASCII|text' && auto_apply_patch $pf || failpatch
#  done
#fi
if [ -d $CWD/bash-${VERSION}-patches ]; then
  ( cd $CWD/bash-${VERSION}-patches ; cat bash5?-??? ) | patch -p0 --verbose || failpatch
fi

# Set default BASH_LOADABLES_PATH. Yes, there's -DDEFAULT_BASH_LOADABLES_PATH,
# but good luck getting it to behave.
sed -i "s|/usr/local/lib/bash:/usr/lib/bash:/opt/local/lib/bash:/usr/pkg/lib/bash:/opt/pkg/lib/bash:.|/usr/lib${LIBDIRSUFFIX}/bash|g" config-top.h

# Reconf due to configure.ac patch:
autoreconf -vif

# Configure:
CFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --mandir=/usr/man \
   --infodir=/usr/info \
   --docdir=/usr/doc/bash-$VERSION.$PATCHLEVEL \
   --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.$PATCHLEVEL
cp -a AUTHORS COMPAT COPYING* INSTALL MANIFEST NOTES \
  README* Y2K doc/FAQ doc/INTRO \
  $PKG/usr/doc/bash-$VERSION.$PATCHLEVEL
( cd doc ; groff -ms -Tascii article.ms > $PKG/usr/doc/bash-$VERSION.$PATCHLEVEL/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
