#!/bin/sh -f

po_domain="alterator-openldap"
alterator_api_version=1

. alterator-sh-functions
. avahi-sh-functions
. shell-config
. shell-error


SLAPD_CONF_DIR="/etc/openldap"
MAIN_SLAPD_CONF="$SLAPD_CONF_DIR/slapd.conf"
LDAP_SYSCONFIG="/etc/sysconfig/ldap"
LDAP_CONF="$SLAPD_CONF_DIR/ldap.conf"
CERT_FILE="/etc/openldap/ssl/slapd.pem"


ldap_read_sysconfig()
{	local url
    url="$(shell_config_get "$LDAP_SYSCONFIG" SLAPDURLLIST)"
    [ "${url/ldap:\/\//}" != "$url" ] && write_bool_param 'port_ldap' 'yes'
    [ "${url/ldaps:\/\//}" != "$url" ] && write_bool_param 'enable_ldaps'  'yes'
    [ "${url/\/\/127\.0\.0\.1\/}" != "$url" ] && write_bool_param 'localhost' 'yes'
}

# Enables or disables the specified schema in the slapd configuration.
# args: schema-name flag
toggle_schema()
{
    sed -i -e "s,^[[:space:]]*#include[[:space:]]\+/etc/openldap/schema/$1.schema,include\t/etc/openldap/schema/$1.schema," "$MAIN_SLAPD_CONF"
}


#args
write_tls()
{
    if [ "$in_enable_ldaps" = "#t" ] ;then
        for i in TLSCACertificateFile TLSCertificateFile TLSCertificateKeyFile;do
            sed -i -e "s|^#$i $CERT_FILE$|$i $CERT_FILE|" $MAIN_SLAPD_CONF
        done
    else
        for i in TLSCACertificateFile TLSCertificateFile TLSCertificateKeyFile;do
            sed -i -e "s|^$i $CERT_FILE$|#$i $CERT_FILE|" $MAIN_SLAPD_CONF
        done
    fi
}

get_default_dn()
{
   local host="$(hostname -d)"
    
   if [ -n "$host" -a "$host" != "localdomain" ] ;then
       host="$(echo $host|sed -e "s/^/dc=/"|sed -e "s/\./dc=/g")"
   else
       host="dc=myoffice,dc=ru"
   fi

   echo "$host"
}

check_status()
{
    local bdn="$1"; shift
    local avahi_dir="/etc/avahi/services/alterator-openldap"
    local status="Unpublished"

    [ -f "$avahi_dir-$bdn.service" ] && status="Published"
    echo "$status"
}

list_dn()
{
    local bdn=
    local source=
    local status=

    ldap-dn getlocal|
    while read bdn source;do

        status=$(check_status "$bdn")
        write_table_item  \
        name "$bdn" \
        bdn "$bdn" \
        source "$source" \
        status "$status" 
    done
}

list_actions()
{
    write_enum_item "publish" "`_ "Publish"`"
    write_enum_item "unpublish" "`_ "Unpublish"`"
    write_enum_item "delete" "`_ "Delete"`"
}

publish_dn()
{
    local dn="$1"
    publish_service "alterator-openldap-$dn" "Ldap dn at %h" "_ldap._tcp" 0 \
    "dn=\"$dn\""
}

unpublish_dn()
{

    local dn="$1"
    unpublish_service "alterator-openldap-$dn" 
}

delete_dn()
{

    local dn="$1"
    local ans="$(ldap-dn delete "$dn" 2>&1)"
    [ -n "$ans" ] && write_error "${ans#ldap-dn*:}"
}
on_message()
{
    case "$in_action" in
        list)
        case "$in__objects" in
            avail_dn)
            list_dn	
            ;;
            avail_actions)
            list_actions
            ;;
        esac
        ;;
        read)
        case "$in__objects" in
            /)
            ldap_read_sysconfig
            ;;
            selected_dn)
                write_string_param  bdn "$in_name"
            ;;
            default_dn)
                write_sting_param ldap_basedn "$(get_default_dn)"
            ;;
        esac
        ;;
        write)
        case "$in__objects" in
            /)
            local host=""
            local url=""
            if [ "$in_localhost" = "#t" ]; then
                host="127.0.0.1"
            fi
            if [ "$in_port_ldap" = "#t" ]; then
                url="$url ldap://$host/"
            fi
            if [ "$in_enable_ldaps" = "#t" ]; then
                url="$url ldaps://$host/"
            fi
            write_tls
            toggle_schema "samba3" 

            # create new dn
            [ -z "$in_dn_name" -a "$in_create" = "dn" ] && write_error "DN not set"
            if [ -n "$in_dn_name" -a "$in_create" = "dn" ] ;then
                ans="$(ldap-dn create "$in_dn_name" 2>&1)"
                [ -n "$ans" ] && write_error "${ans}"
            else
                shell_config_set "$LDAP_SYSCONFIG" SLAPDURLLIST "\"'$url'\""
                shell_config_set "$LDAP_CONF" URI "$url" ' ' ' '
            fi
            ;;
            selected_dn)
            case "$in_actions" in
                publish)
                #[ -n "$in_name" ] && publish_dn "$in_name"
                echo "publish" >&2
                ;;
                unpublish)
                #[ -n "$in_name" ] && unpublish_dn "$in_name"
                echo "unpublish" >&2
                ;;
                delete)
                [ -n "$in_name" ] &&  delete_dn "$in_name" 
                ;;
            esac
            ;;
        esac
        service slapd condrestart 1>&2
        ;;
    esac
}

message_loop
