#!/bin/bash
# =============================================================================
# start_slapd.sh
# Wrapper invoked by supervisord. Always reads the desired listener set
# (plain LDAP vs LDAPS) from /etc/default/slapd so that setup_ldaps.sh
# never has to patch supervisord.conf itself.
# =============================================================================
set -euo pipefail

SLAPD_DEFAULT="/etc/default/slapd"

# Defaults if the env file doesn't exist yet (first boot, before setup_ldaps.sh
# has ever run).
SLAPD_SERVICES="ldap:/// ldapi:///"
SLAPD_DEBUG="${SLAPD_DEBUG:-0}"

# shellcheck disable=SC1090
if [[ -f "$SLAPD_DEFAULT" ]]; then
  source "$SLAPD_DEFAULT"
fi

exec /usr/sbin/slapd \
  -h "${SLAPD_SERVICES}" \
  -g openldap \
  -u openldap \
  -F /etc/ldap/slapd.d \
  -d "${SLAPD_DEBUG}"