#!/bin/sh

# NOTE: This script will work after fresh install or update

/bin/echo -e "** initialize-default-databases **\n"
/etc/e-smith/events/actions/initialize-default-databases

/sbin/e-smith/expand-template /etc/my.cnf
/usr/bin/sv t /service/mysqld

/bin/echo -e "** signal-event remoteaccess-update **\n"
/sbin/e-smith/signal-event remoteaccess-update

/bin/echo -e "** webaccess config **\n"
/bin/chown -R www:www /var/lib/zarafa-webaccess/tmp
FILE=/var/lib/zarafa/client; if [ -d $FILE ]; then /bin/chown -R www:www $FILE; fi
FILE=/var/lib/zarafa-webaccess-mobile/cache; if [ -d $FILE ]; then /bin/chown -R www:www $FILE; fi
FILE=/var/lib/zarafa-webaccess-mobile/templates_c; if [ -d $FILE ]; then /bin/chown -R www:www $FILE; fi
FILE=/etc/httpd/conf.d/zarafa-webaccess.conf; if [ -a $FILE ]; then /bin/rm -f $FILE; fi
FILE=/etc/httpd/conf.d/zarafa-webaccess-mobile.conf; if [ -a $FILE ]; then /bin/rm -f $FILE; fi
/sbin/e-smith/expand-template /etc/httpd/conf/httpd.conf
/usr/bin/sv restart /service/httpd-e-smith

/bin/echo -e "** z-push config **\n"
FILE=/var/lib/z-push;
if [ -d $FILE ]; then /bin/echo -e "** $FILE already exists, skipping **\n"; else /bin/mkdir $FILE; fi
FILE=/var/log/z-push;
if [ -d $FILE ]; then /bin/echo -e "** $FILE already exists, skipping **\n"; else /bin/mkdir $FILE; fi
FILE=/var/lib/z-push; if [ -d $FILE ]; then /bin/chown -R www:www $FILE; fi
FILE=/var/log/z-push; if [ -d $FILE ]; then /bin/chown -R www:www $FILE; fi

db=$(/sbin/e-smith/config getprop zarafa-server DbName);
if [ "$db" = "" ];
then
FILE=/var/lib/mysql/zarafa
else
FILE=/var/lib/mysql/$db
fi
if [ -d $FILE ];
then
/bin/echo -e "** Zarafa database already exists, skipping creation **\n"
/bin/echo -e "************************************************************************"
/bin/echo -e "*               IMPORTANT NOTE ABOUT USER SAFE MODE:                   *"
/bin/echo -e "* Zarafa user and mail databese already exist.  your server is being   *"
/bin/echo -e "* set to \"User Safe Mode\" for safety reasons.  This mode will not      *"
/bin/echo -e "* allow the creation or deletion of users and allows you to verify     *"
/bin/echo -e "* the server is correctly configured.  After you are sure the server   *"
/bin/echo -e "* is configured correctly then issue the following to place the server *"
/bin/echo -e "* in normal operation: \"config setprop zarafa-server UserSafeMode no\"  *"
/bin/echo -e "* Then followed by \"signal-event zarafa update\"                        *"
/bin/echo -e "************************************************************************"
/bin/echo -e "** Placing Zarafa Server in \033[1;;31m\"SAFE MODE\"\033[0m **\n"
/sbin/e-smith/config setprop zarafa-server UserSafeMode yes
/bin/echo -e "** Reseting Zarafa database password for security reasons **"
database=$(/sbin/e-smith/config getprop zarafa-server DbName);
if [ "$database" = "" ];
then
db="zarafa"
else
db="$database"
fi
dbuser=$(/sbin/e-smith/config getprop zarafa-server DbUser);
if [ "$dbuser" = "" ];
then
user="zarafa"
else
user="$dbuser"
fi
pass=`dd if=/dev/urandom count=2000 bs=1 2>/dev/null| tr -dc "a-zA-Z0-9-_!@#$&*" | cut -c-64`
/sbin/e-smith/config setprop zarafa-server DbPassword "$pass";
/usr/bin/mysql << EOF
use $db;
use mysql;
GRANT ALL PRIVILEGES ON $db.* TO $user@localhost IDENTIFIED BY '$pass';
flush privileges;
\q;
EOF
/bin/echo -e "\n** The Zarafa database password has been reset to: $pass **\n"
else
/bin/echo -e "** Creating base Zarafa database **\n"
/sbin/e-smith/expand-template /etc/e-smith/sql/init/80zarafa
/etc/rc.d/init.d/mysql.init start
fi

plugin=$(/sbin/e-smith/config getprop zarafa-server Plugin);
if [ "$plugin" = "" ]
then
/bin/echo -e "************************************************************************"
/bin/echo -e "*               IMPORTANT NOTE ABOUT THE USER PLUGIN:                  *"
/bin/echo -e "* The Zarafa user plugin, it is being automatically set to unix by     *"
/bin/echo -e "* default.  Zarafa on SMEServer  is intended and designed to work best *"
/bin/echo -e "* with the \"unix\" plugin.  If you elect to use a different plugin      *"
/bin/echo -e "* via db commands, that is OK but \"Your Mileage May Vary!\" and you     *"
/bin/echo -e "* will be responsible for any consequences.                            *"
/bin/echo -e "************************************************************************"
/bin/echo -e "** Setting Plugin setting to \"unix\" **\n"
/sbin/e-smith/config setprop zarafa-server Plugin unix
else
if [ "$plugin" = "unix" ]
then
/bin/echo -e "** Zarafa plugin setting already set to \"unix\", skipping **\n"
else
/bin/echo -e "************************************************************************"
/bin/echo -e "*             IMPORTANT WARNING ABOUT THE USER PLUGIN:                 *"
/bin/echo -e "* Your installation is currently NOT set to use the \"unix\" user plugin *"
/bin/echo -e "* and Zarafa on SME Server is intended and designed to work best       *"
/bin/echo -e "* with the \"unix\" plugin.  If you elect to use a different plugin      *"
/bin/echo -e "* via db commands, that is OK but \"Your Mileage May Vary!\" and you     *"
/bin/echo -e "* will be responsible for any consequences.                            *"
/bin/echo -e "************************************************************************\n"
fi
fi

/bin/echo -e "************************************************************************"
/bin/echo -e "* Global Forward is used to make ALL SME users use the Zarafa webmail, *"
/bin/echo -e "* imap and pop3 services.  This is an all-or-none setting. If you want *"
/bin/echo -e "* to limit which users will use Zarafa, you will need to change this   *"
/bin/echo -e "* and you will be able to set each user individually via db settings.  *"
/bin/echo -e "* This will be set to \"enabled\" by default.                            *"
/bin/echo -e "************************************************************************"

global=$(/sbin/e-smith/config getprop zarafa-server GlobalForward);

if [ "$global" = "" ]
then
/bin/echo -e "** Setting Global Forward to \"enabled\" **\n"
/sbin/e-smith/config setprop zarafa-server GlobalForward enabled
else
if [ "$global" = "enabled" ]
then
/bin/echo -e "** Zarafa Global Forward setting already set to \"$global\", skipping **\n"
else
/bin/echo -e "** PLEASE TAKE NOTE: Your Global Forward is  \"disabled\"... **"
/bin/echo -e "**       Please make sure you want it set that way.        **\n"
fi
fi

/bin/echo -e "Enabling Zarafa, expanding templates and (re)starting Zarafa services\n"
/sbin/e-smith/signal-event zarafa-services enabled

sleep 10

/bin/echo -e "creating Public store\n"
/usr/bin/zarafa-admin -s
