#!/bin/sh -f

# if we are not server 
[ -f /etc/sysconfig/system ] || exit 0

. /etc/sysconfig/system

[ "$SERVER_ROLE" = "master" ] || exit 0

. shell-quote

olddomain="$1" ; shift
newdomain="$1" ; shift

host_2_dn()
{
    local host="$1" ; shift
    host="$(echo $host|sed -e "s/^/dc=/"|sed -e "s/\./,dc=/g")"
    echo "$host"
}

create()
{
    local dn="$1" ; shift

    ldap-dn create "$dn"
}

rename()
{
    local old="${1#*.}" ; shift
    local new="${1#*.}" ; shift

    old="$(host_2_dn "$old")"
    new="$(host_2_dn "$new")"

    if [ -z "$(ldap-dn find "$old")" ] ;then
        create "$new"
    else
        ldap-dn rename "$old" "$new"
    fi  
}
    rename "$olddomain" "$newdomain"
