#!/usr/bin/env bash

# Copyright (C) 2015-2019 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2023 David Härdeman <david@hardeman.nu>
# Copyright (C) 2015-2023 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only

# Check if specified LDAP schema file is loaded in the local slapd cn=config
# database. If not, try loading it in the server.


set -o nounset -o pipefail -o errexit

schema_file="${1}"

if [ -z "${schema_file}" ] ; then
    printf "Error: You need to specify a schema file to load\\n" && exit 1
fi

if [ ! -e "${schema_file}" ] ; then
    printf "Error: %s does not exist\\n" "${schema_file}" && exit 1
fi

if [ ! -r "${schema_file}" ] ; then
    printf "Error: %s is unreadable\\n" "${schema_file}" && exit 1
fi

# The schema is not converted to ldif, defer to a helper script
if [[ "${schema_file}" == *.schema ]] ; then
    if type schema2ldif > /dev/null ; then
        ldif_schema_file="${schema_file%.*}.ldif"
        schema2ldif "${schema_file}" > "${ldif_schema_file}"
        schema_file="${ldif_schema_file}"
    else
        printf "Error: %s needs to be in the .ldif format\\n" "${schema_file}" && exit 1
    fi
fi

# From here on, only .ldif is supported
if [[ "${schema_file}" != *.ldif ]] ; then
    printf "Error: %s is in an unsupported format\\n" "${schema_file}" && exit 1
fi

# Get the DN of the schema
schema_dn="$(grep -E '^^dn:\s' "${schema_file}")"

# Get list of already installed schemas from local LDAP server
schema_list() {
    ldapsearch -Y EXTERNAL -H ldapi:/// -LLLQ -b 'cn=schema,cn=config' dn \
    | sed -e '/^$/d' -e 's/{[0-9]\+}//'
}

if schema_list | grep -q "${schema_dn}" ; then
    # The schema is already installed, do nothing
    exit 0
else
    # Try installing the schema in the database
    ldapadd -Y EXTERNAL -H ldapi:/// -f "${schema_file}"
fi
