#!/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 *****
# 

#
# Exit values
#
# 0 - success
# 1 - ldap start failed
# 2 - ldapmodify failed
#

source `dirname $0`/../bin/zmshutil || exit 1
zmsetvars zimbra_home

source_config_dir=/opt/zimbra/openldap/etc/openldap
config_dir=/opt/zimbra/conf

ldap_pw=$1

if [ x$ldap_pw = "x" ]; then
	${zimbra_home}/bin/zmlocalconfig -f -e -r zimbra_ldap_password
	${zimbra_home}/bin/zmlocalconfig -f -e -r ldap_root_password
else
	${zimbra_home}/bin/zmlocalconfig -f -e zimbra_ldap_password=$ldap_pw
	${zimbra_home}/bin/zmlocalconfig -f -e ldap_root_password=$ldap_pw
fi

${zimbra_home}/bin/zmlocalconfig -e ldap_is_master="true"

zmsetvars -f zimbra_ldap_userdn zimbra_ldap_password ldap_root_password zimbra_server_hostname

root_sha=`/opt/zimbra/openldap/sbin/slappasswd -s ${ldap_root_password}`
zimbra_sha=`/opt/zimbra/openldap/sbin/slappasswd -s ${zimbra_ldap_password}`

# Update slapd.conf

sed -e "s|^rootpw.*|rootpw ${root_sha}|" \
	${source_config_dir}/slapd.conf > ${config_dir}/slapd.conf.in

if [ -f "/opt/zimbra/lib/conf/zimbra-ext.schema" ]; then
	plat=`/opt/zimbra/libexec/get_plat_tag.sh`
	if [ "x$plat" = "xMACOSX" -o "x$plat" = "xMACOSXx86" ]; then
		sed -e '\|/opt/zimbra/openldap/etc/openldap/schema/zimbra.schema| a\
		include "/opt/zimbra/lib/conf/zimbra-ext.schema"' ${config_dir}/slapd.conf.in > /tmp/slapd.conf
	else
		sed -e '\|/opt/zimbra/openldap/etc/openldap/schema/zimbra.schema| a include "/opt/zimbra/lib/conf/zimbra-ext.schema"' ${config_dir}/slapd.conf.in > /tmp/slapd.conf
	fi
	mv -f /tmp/slapd.conf ${config_dir}/slapd.conf.in
fi

# Update zimbra.ldif

sed -e "s|^userPassword.*|userPassword: ${zimbra_sha}|" \
	${source_config_dir}/zimbra.ldif > ${config_dir}/zimbra.ldif

# Start ldap

${zimbra_home}/bin/ldap start

if [ $? -ne 0 ]; then
	exit 1
fi

${zimbra_home}/openldap/bin/ldapmodify -a -c -H ${ldap_master_url} -w ${ldap_root_password} -D "${zimbra_ldap_userdn}" -x -f ${config_dir}/zimbra.ldif
${zimbra_home}/openldap/bin/ldapmodify -c -H ${ldap_master_url} -w ${ldap_root_password} -D "${zimbra_ldap_userdn}" -x -f ${source_config_dir}/zimbra_globalconfig.ldif 
${zimbra_home}/openldap/bin/ldapmodify -a -c -H ${ldap_master_url} -w ${ldap_root_password} -D "${zimbra_ldap_userdn}" -x -f ${source_config_dir}/zimbra_defaultcos.ldif

if [ $? -ne 0 ]; then
	exit 2
fi

${zimbra_home}/openldap/bin/ldapmodify -a -c -H ${ldap_master_url} -w ${ldap_root_password} -D "${zimbra_ldap_userdn}" -x -f ${source_config_dir}/zimbra_mimehandlers.ldif

if [ $? -ne 0 ]; then
	exit 2
fi

if [ -d "/opt/zimbra/lib/conf/" ]; then
	L=`ls /opt/zimbra/lib/conf/*ldif 2>/dev/null`
	if [ "x$L" != "x" ]; then
		for i in $L; do
			${zimbra_home}/openldap/bin/ldapmodify -a -c -H ${ldap_master_url} -w ${ldap_root_password} -D "${zimbra_ldap_userdn}" -x -f $i

			if [ $? -ne 0 ]; then
				exit 2
			fi
		done
	fi
fi

exit 0
