#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1
# 
# The contents of this file are subject to the Mozilla Public License
# Version 1.1 ("License"); you may not use this file except in
# compliance with the License. You may obtain a copy of the License at
# http://www.zimbra.com/license
# 
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
# 
# The Original Code is: Zimbra Collaboration Suite Server.
# 
# The Initial Developer of the Original Code is Zimbra, Inc.
# Portions created by Zimbra are Copyright (C) 2005, 2006 Zimbra, Inc.
# All Rights Reserved.
# 
# Contributor(s):
# 
# ***** END LICENSE BLOCK *****
# 

NAME=$1

if [ "x$NAME" = "x" ]; then
	echo "New server name required!"
	exit 1
fi

echo $NAME | egrep '^-' > /dev/null 2>&1

if [ $? -eq 0 ]; then
	echo "Usage: $0 <servername>"
	echo "Change the name of the Zimbra server"
	exit 1
fi

source /opt/zimbra/bin/zmshutil || exit 1
zmsetvars -f zimbra_home

SERVICES=`/opt/zimbra/bin/zmprov -l gs ${zimbra_server_hostname} | sed -ne '/^zimbraServiceEnabled/ s/zimbraServiceEnabled: //p'`

/opt/zimbra/bin/zmcontrol stop
sleep 10

if [ "x${ldap_is_master}" = "xtrue" ]; then
	ldap start
fi

/opt/zimbra/openldap/bin/ldapsearch -H ${ldap_master_url} -w ${ldap_root_password} -D "${zimbra_ldap_userdn}" -x -LLL "(cn=${zimbra_server_hostname})" | sed -e "s/${zimbra_server_hostname}/${NAME}/g" > /tmp/server.ldif

/opt/zimbra/openldap/bin/ldapsearch -H ${ldap_master_url} -w ${ldap_root_password} -D "${zimbra_ldap_userdn}" -x -LLL "(zimbraMailHost=${zimbra_server_hostname})" dn > /tmp/users.ldif

cat /dev/null > /tmp/newusers.ldif
while read i; do
if [ "x$i" != "x" ]; then
cat >> /tmp/newusers.ldif <<EOF
$i
changetype: modify
replace: zimbraMailHost
zimbraMailHost: ${NAME}
-
replace: zimbraMailTransport
zimbraMailTransport: lmtp:${NAME}:7025
-

EOF
fi
done < /tmp/users.ldif

/opt/zimbra/openldap/bin/ldapmodify -c -H ${ldap_master_url} -w ${ldap_root_password} -D "${zimbra_ldap_userdn}" -x -f /tmp/newusers.ldif -v

/opt/zimbra/openldap/bin/ldapdelete -H ${ldap_master_url} -w ${ldap_root_password} -D "${zimbra_ldap_userdn}" -x "cn=${zimbra_server_hostname},cn=servers,cn=zimbra"

/opt/zimbra/openldap/bin/ldapadd -a -H ${ldap_master_url} -w ${ldap_root_password} -D "${zimbra_ldap_userdn}" -x -f /tmp/server.ldif

LHN=`/opt/zimbra/bin/zmprov -l gcf zimbraLogHostName | sed -e 's/zimbraLogHostName: //'`
if [ x$LHN = x${zimbra_server_hostname} ]; then
	/opt/zimbra/bin/zmprov -l mcf zimbraLogHostName ${NAME}
fi

zmlocalconfig -e zimbra_server_hostname=${NAME}

foo=`echo ${ldap_master_url} | sed -e "s|${zimbra_server_hostname}|${NAME}|g"`
if [ "x{$foo}" != "x${ldap_master_url}" ]; then
	zmlocalconfig -e ldap_master_url=$foo
	ldap_master_url=$foo
fi

foo=`echo ${ldap_url} | sed -e "s|${zimbra_server_hostname}|${NAME}|g"`
if [ "x{$foo}" != "x${ldap_url}" ]; then
	zmlocalconfig -e ldap_url=$foo
	ldap_url=$foo
fi

foo=`echo ${ldap_host} | sed -e "s|${zimbra_server_hostname}|${NAME}|g"`
if [ "x{$foo}" != "x${ldap_host}" ]; then
	zmlocalconfig -e ldap_host=$foo
	ldap_host=$foo
fi
foo=`echo ${smtp_destination} | sed -e "s|${zimbra_server_hostname}|${NAME}|g"`
if [ "x{$foo}" != "x${smtp_destination}" ]; then
	zmlocalconfig -e smtp_destination=$foo
	smtp_destination=$foo
fi
foo=`echo ${av_notify_user} | sed -e "s|${zimbra_server_hostname}|${NAME}|g"`
if [ "x{$foo}" != "x${av_notify_user}" ]; then
	zmlocalconfig -e av_notify_user=$foo
	av_notify_user=$foo
fi
foo=`echo ${snmp_trap_host} | sed -e "s|${zimbra_server_hostname}|${NAME}|g"`
if [ "x{$foo}" != "x${snmp_trap_host}" ]; then
	zmlocalconfig -e snmp_trap_host=$foo
	snmp_trap_host=$foo
fi
foo=`echo ${smtp_source} | sed -e "s|${zimbra_server_hostname}|${NAME}|g"`
if [ "x{$foo}" != "x${smtp_source}" ]; then
	zmlocalconfig -e smtp_source=$foo
	smtp_source=$foo
fi

# resource the vars
zmsetvars -f

# rewrite the ldap-*.cf file for postfix
echo $SERVICES | grep -q mta
if [ $? == 0 ]; then
  echo -n "Reinitializing the mta config..."
  /opt/zimbra/libexec/zmmtainit ${ldap_host} ${ldap_port}
  echo "done."
fi

# rewrite swatchrc
echo $SERVICES | grep -q snmp
if [ $? == 0 ]; then
  echo -n "Reinitializing snmp config..."
  /opt/zimbra/libexec/zmsnmpinit
fi
