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

source `dirname $0`/../bin/zmshutil || exit 1
zmsetvars \
    zimbra_home \
    zimbra_log_directory \
    zimbra_db_directory \
    logger_mysql_mycnf \
    logger_mysql_data_directory \
    logger_mysql_directory

sql_root_pw=$1

#
# Sanity checks
#
zmassert -x ${zimbra_home}/bin/logmysqladmin
zmassert -x ${zimbra_home}/bin/logmysql
zmassert -x ${zimbra_home}/bin/zmlocalconfig
zmassert -x ${zimbra_home}/bin/zmmylogpasswd
zmassert -x ${logger_mysql_directory}/bin/mysql_install_db
zmassert -r ${zimbra_db_directory}/loggerdb.sql
zmassert -r ${zimbra_db_directory}/versions-init.sql

#
# Create necessary directories
#
echo '*' Creating required directories
mkdir -p ${logger_mysql_data_directory}
mkdir -p ${zimbra_log_directory}

#
# Create database
#
echo '*' Creating database in ${logger_mysql_data_directory}
(cd ${logger_mysql_directory}; ./bin/mysql_install_db \
    --defaults-file=${logger_mysql_mycnf}) \
    >> ${zimbra_log_directory}/zmloggerinit.log 2>&1

#
# Start mysql server
#
echo '*' Starting logger_mysql server
${zimbra_home}/bin/logmysql.server start \
    >> ${zimbra_log_directory}/zmmyinit.log 2>&1
sleep 15

#
# Load zimbra sql files
#
echo '*' Loading schema ${zimbra_db_directory}/loggerdb.sql 
${zimbra_home}/bin/logmysql -u root --password= < \
    ${zimbra_db_directory}/loggerdb.sql

cat > /tmp/logger.sql.$$ <<EOF
use zimbra_logger;
INSERT INTO zimbra_logger.config(name, value, description) VALUES
	('db.version', '5', 'db schema version');
commit;
EOF

${zimbra_home}/bin/logmysql -u root --password= < \
	/tmp/logger.sql.$$

rm -f /tmp/logger.sql.$$

#echo '*' Loading version from ${zimbra_db_directory}/versions-init.sql
#${zimbra_home}/bin/logmysql -u root --password= < \
#    ${zimbra_db_directory}/versions-init.sql

#
# Generate passwords for mysql into local config
#
if [ x$sql_root_pw = "x" ]; then
	echo '*' Setting random password for mysql root user in zimbra local config
	${zimbra_home}/bin/zmlocalconfig -r -f -e mysql_logger_root_password

	echo '*' Setting random password for mysql zimbra user in zimbra local config
	${zimbra_home}/bin/zmlocalconfig -r -f -e zimbra_logger_mysql_password
else
	echo '*' Setting password for mysql root user in zimbra local config
	${zimbra_home}/bin/zmlocalconfig -f -e mysql_logger_root_password=$sql_root_pw
	echo '*' Setting password for mysql zimbra user in zimbra local config
	${zimbra_home}/bin/zmlocalconfig -f -e zimbra_logger_mysql_password=$sql_root_pw
fi

#
# Change mysql root user password, but first read back the passwords 
# zimbra local config - they was generated above.  Note that we can not
# use 'zmmypasswd --root' here because of bootstrapping problems - at
# this stage we know that the root password is empty.
#
zmsetvars -f mysql_logger_root_password zimbra_logger_mysql_password
echo '*' Changing mysql root user password
${zimbra_home}/bin/logmysqladmin -u root --password= password ${mysql_logger_root_password}

echo '*' Changing mysql zimbra user password
${zimbra_home}/bin/zmmylogpasswd ${zimbra_logger_mysql_password}
