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

# openldap.SlackBuild
# Heavily based on the original Slackware build scripts,
# Modified by Stuart Winter for Slackware ARM.
#
# Copyright 2008-2025  Patrick J. Volkerding, Sebeka, Minnesota, USA
# Copyright 2015-2017 Giuseppe Di Terlizzi <giuseppe.diterlizzi@gmail.com>
# 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

# Ensure user:group exists before building:
if ! grep -q "^ldap:" /etc/passwd ; then
   echo "ldap:x:330:330:OpenLDAP server:/var/lib/openldap:/bin/false" >> /etc/passwd
fi
if ! grep -q "^ldap:" /etc/group ; then
   echo "ldap:x:330:" >> /etc/group
fi
if ! grep -q "^ldap:" /etc/shadow ; then
   echo "ldap:*:9797:0:::::" >> /etc/shadow
fi

# Extract source:
tar xvvf $CWD/$PKGNAM-$VERSION.tar.!(*sign|*asc|*sig)
cd $PKGNAM-$VERSION || failextract
slackhousekeeping

# Change the location of run directory into /var/run/openldap:
sed -i -e 's|%LOCALSTATEDIR%/run/|/var/run/openldap/|' \
  servers/slapd/slapd.*

# Change the location of ldapi socket into /var/run/openldap:
sed -i -e 's|\(#define LDAPI_SOCK\).*|\1 "/var/run/openldap/ldapi"|' \
  include/ldap_defaults.h

# Change the default OpenLDAP database directory:
sed -i -e 's|openldap-data|lib/openldap|' \
  servers/slapd/slapd.* include/ldap_defaults.h servers/slapd/Makefile.in

# Fix man pages:
sed -i "s/openldap\\\-data/lib\/openldap/g" doc/man/man5/slapd-config.5 doc/man/man5/slapd-bdb.5 doc/man/man5/slapd-mdb.5 doc/man/man5/slapd.conf.5

# GCC 14 "fix":
SLKCFLAGS="$SLKCFLAGS -Wno-error=incompatible-pointer-types"

# Configure:
[ ! -r configure ] && ./autogen.sh
CFLAGS="$SLKCFLAGS" \
CXXFLAGS="$SLKCFLAGS" \
./configure \
   --prefix=/usr \
   --exec-prefix=/usr \
   --sysconfdir=/etc \
   --mandir=/usr/man \
   --localstatedir=/var \
   --libdir=/usr/lib${LIBDIRSUFFIX} \
   \
   --enable-debug \
   --enable-dynamic \
   --enable-syslog \
   --enable-proctitle \
   --enable-ipv6 \
   --enable-local \
   \
   --enable-slapd \
   --enable-dynacl \
   --enable-aci \
   --enable-cleartext \
   --enable-crypt \
   --enable-lmpasswd \
   --enable-spasswd \
   --enable-modules \
   --enable-rewrite \
   --enable-rlookups \
   --enable-slapi \
   --disable-slp \
   --enable-wrappers \
   \
   --enable-backends=mod \
   --enable-bdb=yes \
   --enable-hdb=yes \
   --enable-mdb=yes \
   --enable-monitor=yes \
   --enable-perl=yes \
   --enable-argon2 \
   --with-argon2=libsodium \
   --disable-wt \
   \
   --enable-overlays=mod \
   \
   --disable-static \
   --enable-shared \
   \
   --with-cyrus-sasl \
   --without-fetch \
   --with-threads \
   --with-pic \
   --with-tls \
   \
   --host=${SLK_ARCH_HOST} \
   --build=${SLK_ARCH_BUILD} || failconfig

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

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

# Don't package this directory:
rmdir $PKG/var/run

# Fix permissions on shared libraries:
chmod 755 $PKG/usr/lib${LIBDIRSUFFIX}/*.so.*

# Get rid of .default config files:
rm -f $PKG/etc/openldap/*.default

# Move ldap.conf to ldap.conf.new and add an additional option:
mv $PKG/etc/openldap/ldap.conf $PKG/etc/openldap/ldap.conf.new
cat << EOF >> $PKG/etc/openldap/ldap.conf.new

# In order to avoid problems with self-signed certificates using TLS:
# "TLS certificate verification: Error, self signed certificate"
# See also 'man ldap.conf' or http://www.openldap.org/doc/admin/tls.html
TLS_REQCERT allow

EOF

# Move other config files to .new:
mv -fv $PKG/etc/openldap/slapd.conf $PKG/etc/openldap/slapd.conf.new
mv -fv $PKG/etc/openldap/slapd.ldif $PKG/etc/openldap/slapd.ldif.new

# Create a symlink for slapd in /usr/sbin:
if [ ! -x $PKG/usr/sbin/slapd ]; then
   ( cd $PKG/usr/sbin ; ln -sf ../libexec/slapd slapd )
fi

# Create OpenLDAP certificates directory:
mkdir -p $PKG/etc/openldap/certs

# Copy rc.openldap:
mkdir -p $PKG/etc/rc.d
cat $CWD/rc.openldap > $PKG/etc/rc.d/rc.openldap.new

# Copy slapd default file:
mkdir -p $PKG/etc/default
cat $CWD/slapd > $PKG/etc/default/slapd.new

# Add documentation:
mkdir -vpm755 $PKG/usr/doc/$PKGNAM-$VERSION
cp -fav \
  ANNOUNCEMENT* CHANGES COPYRIGHT* INSTALL* LICENSE* README* \
  $PKG/usr/doc/$PKGNAM-$VERSION
changelogliposuction CHANGES $PKGNAM $VERSION # Trim down a "ChangeLog" file

# Include monitor backend README
cp -fav \
  servers/slapd/back-monitor/README \
  $PKG/usr/doc/$PKGNAM-$VERSION/README.back-monitor

# Include Perl backend README
cp -fav \
  servers/slapd/back-perl/README \
  $PKG/usr/doc/$PKGNAM-$VERSION/README.back-perl

# Include Perl backend sample file
cp -fav \
  servers/slapd/back-perl/SampleLDAP.pm \
  $PKG/usr/doc/$PKGNAM-$VERSION

# Include OpenLDAP documentation
cp -fav \
  doc/guide/admin/*.png \
  doc/guide/admin/*.html \
  $PKG/usr/doc/$PKGNAM-$VERSION

# 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

# Special permissions:
#
# Fix ownership:
chown -R ldap:ldap $PKG/var/lib/openldap
chown -R ldap:ldap $PKG/etc/openldap

# Restrict access to database:
mkdir -p $PKG/var/lib/openldap
chmod 700 $PKG/var/lib/openldap

slackmp         # run makepkg -l y -c n

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