#!/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) 2004, 2005, 2006 Zimbra, Inc.
# All Rights Reserved.
# 
# Contributor(s):
# 
# ***** END LICENSE BLOCK *****
# 

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

#
# Sanity checks
#
zmassert -x ${zimbra_home}/libexec/zmmycnf
zmassert -x ${zimbra_home}/bin/mysqladmin
zmassert -x ${zimbra_home}/bin/mysql
zmassert -x ${zimbra_home}/bin/zmlocalconfig
zmassert -x ${zimbra_home}/bin/zmmypasswd
zmassert -x ${mysql_directory}/bin/mysql_install_db
zmassert -r ${zimbra_db_directory}/db.sql
zmassert -r ${zimbra_db_directory}/versions-init.sql

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

#
# Generate a mysql config file
#
echo '*' Generating mysql config ${mysql_mycnf}
rm -f ${mysql_mycnf}
${zimbra_home}/libexec/zmmycnf > ${mysql_mycnf}

#
# Create database
#
echo '*' Creating database in ${mysql_data_directory}
(cd ${mysql_directory}; ./bin/mysql_install_db \
    --defaults-file=${mysql_mycnf}) \
    >> ${zimbra_log_directory}/zmmyinit.log 2>&1

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

# make sure we can connect before continuing 
until `echo "show processlist" | ${zimbra_home}/bin/mysql -u root --password= > /dev/null 2>&1`; do
  let i++
  sleep 5
  if [ $i -gt 25 ]; then
    echo '*' Failed to connect to mysql...giving up!
    exit -1
  else 
    echo '*' Failed to connect to mysql...retrying
  fi
done

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

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

if [ -f "${zimbra_db_directory}/backup-version-init.sql" ]; then
  echo '*' Loading version from ${zimbra_db_directory}/backup-version-init.sql
  ${zimbra_home}/bin/mysql -u root --password= < \
    ${zimbra_db_directory}/backup-version-init.sql
fi


#
# Generate passwords for mysql into local config
#
if [ x$sql_root_pw = "x" ]; then
    echo '*' Setting random passwd for mysql root user in zimbra localconfig
    ${zimbra_home}/bin/zmlocalconfig -r -f -e mysql_root_password
    
    echo '*' Setting random passwd for mysql zimbra user in zimbra localconfig
    ${zimbra_home}/bin/zmlocalconfig -r -f -e zimbra_mysql_password
else
    echo '*' Setting passwd for mysql root user in zimbra localconfig
    ${zimbra_home}/bin/zmlocalconfig -f -e mysql_root_password=$sql_root_pw
    echo '*' Setting passwd for mysql zimbra user in zimbra localconfig
    ${zimbra_home}/bin/zmlocalconfig -f -e zimbra_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_root_password zimbra_mysql_password
echo '*' Changing mysql root user password
${zimbra_home}/bin/mysqladmin -u root --password= password ${mysql_root_password}

echo '*' Changing mysql zimbra user password
${zimbra_home}/bin/zmmypasswd ${zimbra_mysql_password}
