#!/bin/bash

# SlackBuild for dieharder, 5 January 2017
# awg <email removed>
# Modified and now maintained by B. Watson <urchlay@slackware.uk>.
# 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.

## dieharder itself remains a copyrighted work under the terms of the
## GNU GPL v2 (with beverage provision).  Please see the file COPYING
## in the dieharder source distribution for more on this.

# 20260703 bkw:
# - take over maintenance.
# - get rid of MANPDF option; it's broken.
# - stop writing to /usr/share/aclocal during the build!

# 20220211 bkw: Modified by SlackBuilds.org: add -j1 to the make commands
# because parallel builds are broken.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=dieharder
VERSION=${VERSION:-3.31.1}
BUILD=${BUILD:-4}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
# 20260703 bkw: the --exclude= here is because the tarball contains symlinks
# to the files in /usr/share/aclocal, and extracting the tarball modifies
# the timestamps of the files in /usr! turns out, they're not even needed,
# excluding them here just means they'll get copied by autoreconf.
tar xvf $CWD/$PRGNAM-$VERSION.tgz --exclude='*/m4/*'
cd $PRGNAM-$VERSION
chown -R root:root .
find . ! -type l    -a \
  \(     -perm /111 -a ! -perm 755 -a -exec chmod -f 755 {} + \) -o \
  \(   ! -perm /111 -a ! -perm 644 -a -exec chmod -f 644 {} + \)

# apply patches, mostly to appease crusty autoconf.
patch -p1 < $CWD/$PRGNAM.patch
mkdir -p manual/m4  # a stub.

# 20260703 bkw: don't use autogen.sh, it runs ./configure with no args.
autoreconf -i

CFLAGS="$SLKCFLAGS -D_GNU_SOURCE -fcommon" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
  --prefix=/usr \
  --libdir=/usr/lib${LIBDIRSUFFIX} \
  --sysconfdir=/etc \
  --localstatedir=/var \
  --mandir=/usr/man \
  --disable-static \
  --enable-shared \
  --with-gnu-ld \
  --build=$ARCH-slackware-linux

make -j1
make -j1 install-strip DESTDIR=$PKG
rm -f $PKG/usr/lib*/*.la
gzip -9 $PKG/usr/man/man*/*

PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
mkdir -p $PKGDOC
cp -a COPYING NOTES AUTHORS $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

# 20260703 bkw: this stuff is broken, just dike it out.
## if [ "${MANPDF:-no}" = "yes" ]; then
##   ( cd manual; make -j1 pdf )
##   cp -a manual/dieharder.pdf $PKGDOC
## fi

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
