#!/bin/sh -f

. alterator-service-functions
. shell-config

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

. /etc/sysconfig/system

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

CONFIG_FILES='  /etc/postfix/ldap-aliases.cf
		/etc/postfix/ldap-users.cf
		/etc/postfix/mydestination'

if [ $# -eq 0 ]; then
    olddomain=""
    newdomain="$(shell_config_get /etc/sysconfig/network HOSTNAME)"
    [ -n "$newdomain" ] &&
        newdomain="${newdomain#*.}" ||
        newdomain="localdomain"
else
    olddomain="${1#*.}" ; shift
    newdomain="${1#*.}" ; shift
fi

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

olddn="$(host_2_dn "$olddomain")"
newdn="$(host_2_dn "$newdomain")"

[ -n "$newdomain" -a -n "$newdn" ] || exit

dn_delim='\(^\|[^[:alnum:]=-]\|$\)'
dns_delim='\(^\|[^[:alnum:].-]\|$\)'

[ -n "$olddn" ] && sed -i \
	-e "s/${dn_delim}${olddn}${dn_delim}/\1@basedn@\2/g" \
	$CONFIG_FILES
[ -n "$olddomain" ] && sed -i \
	-e "s/${dns_delim}${olddomain}${dns_delim}/\1@domain@\2/g" \
	$CONFIG_FILES

sed -i	-e "s/@basedn@/${newdn}/g" \
	-e "s/@domain@/${newdomain}/g" \
	$CONFIG_FILES

service_control postfix condreload
