#!/bin/sh
# This script is written by Martynas Bendorius and DirectAdmin
# It is used to install RoundCube webmail into DirectAdmin servers
# Official RoundCube webmail page: http://www.roundcube.net

VERSION=0.5

DA_SCRIPTS=/usr/local/directadmin/scripts
DA_MYSQL=/usr/local/directadmin/conf/mysql.conf
TARFILE=${DA_SCRIPTS}/packages/roundcubemail-${VERSION}.tar.gz
WWWPATH=/var/www/html
REALPATH=${WWWPATH}/roundcubemail-${VERSION}
ALIASPATH=${WWWPATH}/roundcube
if [ -e /etc/httpd/conf/exta/httpd-alias.conf ]; then
	HTTPDCONF=/etc/httpd/conf/exta/httpd-alias.conf
else
	HTTPDCONF=/etc/httpd/conf/httpd.conf
fi
CONFIG=${REALPATH}/config/main.inc.php
DA_HOSTNAME=`hostname`
HTTPPATH=http://files.directadmin.com/services/all/roundcube

OS=`uname`

# variables for the database:
ROUNDCUBE_DB=da_roundcube
ROUNDCUBE_DB_USER=da_roundcube
ROUNDCUBE_DB_PASS=`perl -le'print map+(A..Z,a..z,0..9)[rand 62],0..7'`;
DB_CONFIG=${REALPATH}/config/db.inc.php
MYSQLUSER=`grep "^user=" ${DA_MYSQL} | cut -d= -f2`
MYSQLPASSWORD=`grep "^passwd=" ${DA_MYSQL} | cut -d= -f2`

if [ "${OS}" = "FreeBSD" ]; then
	WGET=/usr/local/bin/wget
	TAR=/usr/bin/tar
	CHOWN=/usr/sbin/chown
	MYSQL_DATA=/home/mysql
else
	WGET=/usr/bin/wget
	TAR=/bin/tar
	CHOWN=/bin/chown
	if [ -e /etc/debian_version ]; then
		MYSQL_DATA=/home/mysql
	else
		MYSQL_DATA=/var/lib/mysql
	fi
fi 

if [ ! -e ${TARFILE} ]; then
	${WGET} -O ${TARFILE} ${HTTPPATH}/roundcubemail-${VERSION}.tar.gz
fi

if [ ! -e ${TARFILE} ]; then
	echo "Can not download roundcubemail-${VERSION}"
	exit 1
fi

MYSQLUP=0
if [ -d $MYSQL_DATA/${ROUNDCUBE_DB} ]; then
	if [ "`cat $ALIASPATH/index.php | grep RCMAIL_VERSION | awk '{ print $2}' | cut -d\' -f2`" != "${VERSION}" ]; then
		MYSQLUP=1
	fi
fi

#Extract the file
${TAR} xzf ${TARFILE} -C ${WWWPATH}

if [ ! -e ${REALPATH} ]; then
	echo "Directory ${REALPATH} does not exist"
	exit 1
fi

if [ -e ${ALIASPATH} ]; then
	if [ -d ${ALIASPATH}/logs ]; then
		cp -fR ${ALIASPATH}/logs/* ${REALPATH}/logs
	fi
	if [ -d ${ALIASPATH}/temp ]; then
		cp -fR ${ALIASPATH}/temp/* ${REALPATH}/temp
	fi
fi

#link it from a fake path:
/bin/rm -f ${ALIASPATH}
/bin/ln -sf roundcubemail-${VERSION} ${ALIASPATH}
cd ${REALPATH}

#insert data to mysql and create database/user for roundcube:
if [ ! -d $MYSQL_DATA/${ROUNDCUBE_DB} ]; then
	if [ -d SQL ]; then
		echo "Inserting data to mysql and creating database/user for roundcube..."
		mysql -e "CREATE DATABASE ${ROUNDCUBE_DB};" --user=${MYSQLUSER} --password=${MYSQLPASSWORD}
		mysql -e "GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER,INDEX ON ${ROUNDCUBE_DB}.* TO '${ROUNDCUBE_DB_USER}'@'localhost' IDENTIFIED BY '${ROUNDCUBE_DB_PASS}';" --user=${MYSQLUSER} --password=${MYSQLPASSWORD}
		mysql -e "use ${ROUNDCUBE_DB}; source SQL/mysql.initial.sql;" --user=${ROUNDCUBE_DB_USER} --password=${ROUNDCUBE_DB_PASS}
		echo "Database created, ${ROUNDCUBE_DB_USER} password is ${ROUNDCUBE_DB_PASS}"
	else
		echo "Can not find SQL directory in roundcubemail-${VERSION}"
		exit 1
	fi
else
	mysql -e "SET PASSWORD FOR '${ROUNDCUBE_DB_USER}'@'localhost' = PASSWORD('${ROUNDCUBE_DB_PASS}');" --user=${MYSQLUSER} --password=${MYSQLPASSWORD}
	if [ "${MYSQLUP}" = "1" ]; then
		mysql -e "use ${ROUNDCUBE_DB}; source SQL/mysql.update.sql;" --user=${ROUNDCUBE_DB_USER} --password=${ROUNDCUBE_DB_PASS}
	fi
fi

#install the proper config:
if [ -d ../roundcube ]; then
		echo "Editing roundcube configuration..."
		cd ${REALPATH}/config
		/bin/mv -f db.inc.php.dist db.inc.php
		/bin/mv -f main.inc.php.dist main.inc.php
		/usr/bin/perl -pi -e "s|mysql://roundcube:pass\@localhost/roundcubemail|mysql://${ROUNDCUBE_DB_USER}:${ROUNDCUBE_DB_PASS}\@localhost/${ROUNDCUBE_DB}|" db.inc.php > /dev/null
		/usr/bin/perl -pi -e "s|\['enable_caching'] = TRUE|\['enable_caching'] = FALSE|" main.inc.php > /dev/null


                #It's better not use use imap_root/delimiter
                #/usr/bin/perl -pi -e "s|\['imap_root'] = null|\['imap_root'] = 'INBOX.'|" main.inc.php > /dev/null
                #/usr/bin/perl -pi -e "s|\['imap_delimiter'] = null|\['imap_delimiter'] = '.'|" main.inc.php > /dev/null

                /usr/bin/perl -pi -e "s|\['default_host'] = ''|\['default_host'] = 'localhost'|" main.inc.php > /dev/null
                /usr/bin/perl -pi -e "s|\['drafts_mbox'] = 'Drafts'|\['drafts_mbox'] = 'INBOX.Drafts'|" main.inc.php > /dev/null
                /usr/bin/perl -pi -e "s|\['junk_mbox'] = 'Junk'|\['junk_mbox'] = 'INBOX.spam'|" main.inc.php > /dev/null
                /usr/bin/perl -pi -e "s|\['sent_mbox'] = 'Sent'|\['junk_mbox'] = 'INBOX.Sent'|" main.inc.php > /dev/null
                /usr/bin/perl -pi -e "s|\['trash_mbox'] = 'Trash'|\['junk_mbox'] = 'INBOX.Trash'|" main.inc.php > /dev/null
                /usr/bin/perl -pi -e "s|\['default_imap_folders'] = array\('INBOX', 'Drafts', 'Sent', 'Junk', 'Trash'\)|\['default_imap_folders'] = array\('INBOX', 'INBOX.Drafts', 'INBOX.Sent', 'INBOX.spam', 'INBOX.Trash'\)|" main.inc.php > /dev/null

                #smtp stuff
                /usr/bin/perl -pi -e "s|\['smtp_port'] = 25|\['smtp_port'] = 587|" main.inc.php > /dev/null
                /usr/bin/perl -pi -e "s|\['smtp_server'] = ''|\['smtp_server'] = 'localhost'|" main.inc.php > /dev/null
                /usr/bin/perl -pi -e "s|\['smtp_user'] = ''|\['smtp_user'] = '%u'|" main.inc.php > /dev/null
                /usr/bin/perl -pi -e "s|\['smtp_pass'] = ''|\['smtp_pass'] = '%p'|" main.inc.php > /dev/null
                /usr/bin/perl -pi -e "s|\['smtp_auth_type'] = ''|\['smtp_auth_type'] = 'LOGIN'|" main.inc.php > /dev/null

                /usr/bin/perl -pi -e "s|\['create_default_folders'] = FALSE;|\['create_default_folders'] = TRUE;|" main.inc.php > /dev/null
                /usr/bin/perl -pi -e "s/\'mdb2\'/\'db\'/" db.inc.php > /dev/null

                #password plugin
                if [ -e ${REALPATH}/plugins/password ]; then
                        /usr/bin/perl -pi -e "s|\['plugins'] = array\(\);|\['plugins'] = array\('password'\);|" main.inc.php > /dev/null

                        cd ${REALPATH}/plugins/password
                        if [ ! -e config.inc.php ]; then
                                cp config.inc.php.dist config.inc.php
                        fi

                        /usr/bin/perl -pi -e "s|\['password_driver'] = 'sql'|\['password_driver'] = 'directadmin'|" config.inc.php > /dev/null

                        if [ -e $DACONF_FILE ]; then
                                DAPORT=`grep -e '^port=' /usr/local/directadmin/conf/directadmin.conf | cut -d= -f2`
                                /usr/bin/perl -pi -e "s|\['password_directadmin_port'] = 2222|\['password_directadmin_port'] = $DAPORT|" config.inc.php > /dev/null
                        fi
                        cd ${REALPATH}/config
                fi

                echo "Roundcube ${ROUNDCUBE_VER} has been installed successfully."
fi

#set the permissions:
${CHOWN} -R webapps:webapps ${REALPATH}
${CHOWN} -R apache ${REALPATH}/temp ${REALPATH}/logs
/bin/chmod -R 770 ${REALPATH}/temp
/bin/chmod -R 770 ${REALPATH}/logs

#cleanup:
rm -rf ${ALIASPATH}/installer

#writing alias to httpd.conf
COUNTALIAS=`grep -c -e "Alias /roundcube" ${HTTPDCONF}`
if [ "${COUNTALIAS}" = "0" ]; then
   echo "Adding roundcube alias to ${HTTPDCONF}"
   echo "" >> ${HTTPDCONF}
   echo "# Alias for RoundCube webmail" >> ${HTTPDCONF}
   echo "Alias /roundcube ${ALIASPATH}/" >> ${HTTPDCONF}
   echo "" >> ${HTTPDCONF}
   echo "You need to restart the httpd service if you want alias to work."
fi

