# Description: Lightweight Directory Access Protocol (LDAP) Toolkit
# URL: https://www.openldap.org/
# Maintainer: CRUX System Team, core-ports at crux dot nu
# Depends on: libtool perl util-linux
# Optional: cyrus-sasl libsodium mariadb unixodbc

name=openldap
version=2.6.13
release=1
source=(https://www.openldap.org/software/download/OpenLDAP/openldap-release/$name-$version.tgz
    slapd)

build() {
    cd $name-$version

    sed -i 's|-m 644 $(LIBRARY)|-m 755 $(LIBRARY)|' libraries/{liblber,libldap}/Makefile.in

    sed -i 's|#define LDAPI_SOCK LDAP_RUNDIR LDAP_DIRSEP "run" LDAP_DIRSEP "ldapi"|#define LDAPI_SOCK LDAP_DIRSEP "run" LDAP_DIRSEP "openldap" LDAP_DIRSEP "ldapi"|' include/ldap_defaults.h
    sed -i 's|%LOCALSTATEDIR%/run|/run/openldap|' servers/slapd/slapd.{conf,ldif}
    sed -i 's|-$(MKDIR) $(DESTDIR)$(localstatedir)/run|-$(MKDIR) $(DESTDIR)/run/openldap|' servers/slapd/Makefile.in

    prt-get isinst cyrus-sasl && PKGMK_LDAP+=' --with-cyrus-sasl --enable-spasswd' || PKGMK_LDAP+=' --without-cyrus-sasl --disable-spasswd'
    prt-get isinst libsodium && PKGMK_LDAP+=' --enable-argon2 --with-argon2=libsodium'
    prt-get isinst mariadb unixodbc && PKGMK_LDAP+=' --enable-sql --with-odbc=unixodbc' || PKGMK_LDAP+=' --disable-sql'

    ./configure $PKGMK_LDAP \
        --prefix=/usr \
        --sysconfdir=/etc \
        --libexecdir=/usr/sbin \
        --localstatedir=/var/openldap \
        --with-pic \
        --with-threads \
        --with-tls \
        --enable-aci \
        --enable-backends \
        --enable-crypt \
        --enable-dnssrv=mod \
        --enable-dynacl \
        --enable-dynamic \
        --enable-ipv6 \
        --enable-ldap \
        --enable-local \
        --enable-meta=mod \
        --enable-modules \
        --enable-null=mod \
        --enable-overlays=mod \
        --enable-perl \
        --enable-shared \
        --enable-syslog \
        --enable-versioning \
        --disable-wt

    make depend
    make
    make DESTDIR=$PKG install

    ln -sf liblber.so $PKG/usr/lib/liblber.so.2
    ln -sf libldap.so $PKG/usr/lib/libldap.so.2

    install -D -m 0755 $SRC/slapd $PKG/etc/rc.d/slapd
    rm -f $PKG/etc/openldap/schema/README
}
