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

# krb5.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2009  Tom Canich, State College, Pennsylvania, USA
# Copyright 2015-2017  Willy Sudiarto Raharjo <willysr@slackbuilds.org>
# Copyright 2017, 2018, 2019, 2020  Patrick J. Volkerding, Sebeka, MN, 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-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || exit 1
slackhousekeeping

# Apply patches:
sed -i "/KRB5ROOT=/s/\/local//" src/util/ac_check_krb5.m4
cat $CWD/d775c95af7606a51bf79547a94fa52ddd1cb7f49.patch | patch -p1 --verbose || exit 1

# Bootstrap:
if [ -f /var/lib/pkgtools/packages/openldap-[0-9]* ]; then
   echo "Building with LDAP support"
   WITH_LDAP="--with-ldap"
 else
   echo "Bootstrap mode: Building without LDAP support"
   WITH_LDAP=""
fi

cd src
# Configure:
[ ! -r configure ] && ./autogen.sh
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
CPPFLAGS+=" -I/usr/include/et" \
./configure \
   --prefix=/usr \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   --sysconfdir=/etc \
   --localstatedir=/var/kerberos \
   --runstatedir=/var/run \
   --mandir=/usr/man \
   --enable-dns-for-realm \
   --with-system-et \
   --with-system-ss \
   --enable-pkinit \
   --with-tls-impl=openssl \
   --with-system-verto=no \
   --with-prng-alg=os \
   \
   $WITH_LDAP \
   \
   --host=${SLK_ARCH_HOST} \
   --build=${SLK_ARCH_BUILD} || failconfig

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

# Double check for proper runstatedir setting:
if ! grep -q /var/run/krb5kdc include/osconf.h ; then
  echo "FATAL: runstatedir not set properly: $(grep KDC_RUN_DIR include/osconf.h)"
  exit 1
fi

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

# Fix perms on shared objects:
find $PKG/usr/lib${LIBDIRSUFFIX} -name "*.so*" -exec chmod 755 "{}" \+

# Install init scripts:
mkdir -p $PKG/etc/rc.d
install -vpm644 $CWD/conf/rc.kadmind $PKG/etc/rc.d/rc.kadmind.new
install -vpm644 $CWD/conf/rc.kpropd $PKG/etc/rc.d/rc.kpropd.new
install -vpm644 $CWD/conf/rc.krb5kdc $PKG/etc/rc.d/rc.krb5kdc.new

# Install default options:
mkdir -p $PKG/etc/default
install -vpm644 $CWD/conf/kadmind $PKG/etc/default/kadmind.new
install -vpm644 $CWD/conf/kpropd $PKG/etc/default/kpropd.new
install -vpm644 $CWD/conf/krb5kdc $PKG/etc/default/krb5kdc.new

# Install example config files:
mkdir -p $PKG/etc
install -vpm644 $CWD/conf/krb5.conf.example $PKG/etc/krb5.conf.example
mkdir -p /var/kerberos/krb5kdc
install -vpm644 $CWD/conf/kdc.conf.example $PKG/var/kerberos/krb5kdc/kdc.conf.example

# Move examples to the documentation directory:
mkdir -p $PKG/usr/doc/${PKGNAM}-${VERSION}/examples
mv -fv $PKG/usr/share/examples/krb5/* $PKG/usr/doc/${PKGNAM}-${VERSION}/examples
rmdir $PKG/usr/share/examples/krb5 $PKG/usr/share/examples 2> /dev/null

# Move some libraries to $PKG/lib${LIBDIRSUFFIX}:
mkdir -p $PKG/lib${LIBDIRSUFFIX}
( cd $PKG/usr/lib${LIBDIRSUFFIX}
  for lib in libgssapi_krb5 libkrb5 libk5crypto libkrb5support ; do
    mv -fv ${lib}.so.?.* ../../lib${LIBDIRSUFFIX}
    ln -vsf ../../lib${LIBDIRSUFFIX}/${lib}.so.?.* .
    cp -fav  ${lib}.so.? ../../lib${LIBDIRSUFFIX}
  done )

# Add documentation:
# krb5 ships with a ton of docs, but for now we'll just include these:
mkdir -p $PKG/usr/doc/$PKGNAM-$VERSION
  cp -a \
  ../NOTICE* ../README* \
  $PKG/usr/doc/$PKGNAM-$VERSION

# Don't need pre-compiled man pages:
rm -rf $PKG/usr/man/cat{1,5,8}

# Apply generic Slackware packaging policies:
cd $PKG
slackstripall        # strip all .a archives and all ELFs
#slackstriprpaths     # strip rpaths
slack_delete_lafiles # delete usr/lib{,64}/*.la
slackgzpages -i      # compress man & info pages and delete usr/info/dir
slackslack           # set standard Slackware file/dir permissions and ownerships
slackdesc            # install slack-desc and doinst.sh
slackmp         # run makepkg -l y -c n

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