#!/bin/sh -efu

# please comment here about this library

if [ -z "${__included_alterator_openldap_functions}" ]; then
__included_alterator_openldap_functions=1

. shell-config
. shell-error
. shell-quote
. shell-signal

# delimeters
rdelim='[[:space:]]\+'
wdelim=' '

# config files
SLAPD_CONF="/etc/openldap/slapd.conf"
PAMLDAP_CONF="/etc/pam_ldap.conf"

# blank samba sid
SID="S-1-0-0"

# min max uid gid
uid_min="5000"
gid_min="5000"
uid_max="9000"
gid_max="9000"

# mail spool dir
ENABLE_HOMEDIR=
ENABLE_SPOOL=
SPOOL="/var/spool/mail"

##############################################################################
# read config functions
##############################################################################

#reading some config file $1 for $2 variable and removing " from its value 

read_config()
{
    [ -e "$1" ] || fatal "read_config: '$1' file not found" 
    shell_config_get "$1" "$2" "$rdelim"|tr -d '"'
}

read_dn_conf()
{
    read_config "$DN_CONF" "$1"
}

read_slapd_conf()
{
    read_config "$SLAPD_CONF" "$1"
}

read_pamldap_conf()
{
    read_config "$PAMLDAP_CONF" "$1"
}

##############################################################################



##############################################################################
# host_2_dn dn_2_host
##############################################################################

# host.domain.com => dc=host,dc=domain,dc=com
host_2_dn()
{
    echo "$1"|sed '
    s/^/dc=/
    s/\./,dc=/g'
}

# dc=host,dc=domain,dc=com => host.domain.com
dn_2_host()
{
    echo "$1"|sed '
    s/^dc=//
    s/,dc=/\./g'
}

##############################################################################



##############################################################################
# slapd functions
##############################################################################

# return 
# 0 if all right
# 1 if pidfile exists but daemon died
# 3 if daemon stoped 
slapd_daemon_status()
{
    [ -e "/etc/init.d/slapd" ] || fatal "slapd_daemon_status: /etc/init.d/slapd doesn't exist"
    /etc/init.d/slapd status >/dev/null 2>&1
    return $?
}

##############################################################################



##############################################################################
# base_rootdn_rootpw functions
##############################################################################

base_rootdn_rootpw() {

    [ -z "$DN_CONF" ] && fatal "base_rootdn_rootpw: DN_CONF not set"
    
	base="$(read_dn_conf suffix)"
	rootdn="$(read_dn_conf rootdn)"
	rootpw="$(read_dn_conf rootpw)"

    [ -z "$base" ] && fatal "base_rootdn_rootpw: basedn not set in conf '$DN_CONF'"
    [ -z "$rootdn" ] && fatal "base_rootdn_rootpw: rootdn not set in conf '$DN_CONF'"
    [ -z "$rootpw" ] && fatal "base_rootdn_rootpw: rootpw not set in conf '$DN_CONF'"
    
    rootpw="-w $rootpw" 

	export base rootdn rootpw 
}

##############################################################################



##############################################################################
# helper functions
##############################################################################

get_sid()
{
     local sid="$(net getlocalsid | cut -f2 -d':'| sed -e "s/[[:blank:]]//g" 2>/dev/null)"
     [ -n "$sid" ] && SID="$sid"
}

##############################################################################
fi #__included_alterator_openldap_functions
