#!/usr/bin/env bash

# Author:   Zhang Huangbin (zhb _at_ iredmail.org)

#---------------------------------------------------------------------
# This file is part of iRedMail, which is an open source mail server
# solution for Red Hat(R) Enterprise Linux, CentOS, Debian and Ubuntu.
#
# iRedMail is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# iRedMail is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with iRedMail.  If not, see <http://www.gnu.org/licenses/>.
#---------------------------------------------------------------------

# Define SOGo yum/apt mirror sites.
#   - CentOS 6: ${SOGO_PKG_MIRROR}/RHEL6/$basearch
#   - CentOS 7: ${SOGO_PKG_MIRROR}/RHEL7/$basearch
#   - Debian: ${SOGO_PKG_MIRROR}/debian
#   - Ubuntu: ${SOGO_PKG_MIRROR}/ubuntu
export SOGO_PKG_MIRROR="${SOGO_PKG_MIRROR:=http://inverse.ca/downloads/SOGo}"

export SOGO_DAEMON_USER='sogo'
export SOGO_DAEMON_GROUP='sogo'

export SOGO_PID_FILE='/var/run/sogo/sogo.pid'
export SOGO_LOG_FILE='/var/log/sogo/sogo.log'

export SOGO_CONF_DIR='/etc/sogo'
export SOGO_SIEVE_MASTER_USER="sogo_sieve_master"
export SOGO_RC_SCRIPT_NAME='sogod'
export MEMCACHED_RC_SCRIPT_NAME='memcached'

# SOGo deamon listens on port 127.0.0.1:20000 by default.
export SOGO_BIND_ADDRESS='127.0.0.1'
export SOGO_BIND_PORT='20000'

export SOGO_DB_NAME='sogo'
export SOGO_DB_USER='sogo'
export SOGO_DB_AUTH_VIEW='users'
export SOGO_DB_TABLE_USER_PROFILE='sogo_user_profile'
export SOGO_DB_TABLE_FOLDER_INFO='sogo_folder_info'
export SOGO_DB_TABLE_SESSIONS_FOLDER='sogo_sessions_folder'
export SOGO_DB_TABLE_ALARMS='sogo_alarms_folder'

export SOGO_HTTPD_CONF="${HTTPD_CONF_DIR}/SOGo.conf"

export SOGO_CMD_TOOL='/usr/sbin/sogo-tool'
export SOGO_CMD_EALARMS_NOTIFY='/usr/sbin/sogo-ealarms-notify'
export SOGO_GNUSTEP_DIR="/usr/lib/GNUstep/SOGo"

if [ X"${DISTRO}" == X'RHEL' ]; then
    if [ X"${OS_ARCH}" == X'x86_64' ]; then
        export SOGO_GNUSTEP_DIR="/usr/lib64/GNUstep/SOGo"
    fi

elif [ X"${DISTRO}" == X'DEBIAN' -o X"${DISTRO}" == X'UBUNTU' ]; then
    export SOGO_RC_SCRIPT_NAME='sogo'

elif [ X"${DISTRO}" == X'FREEBSD' ]; then
    export SOGO_DAEMON_USER='sogod'
    export SOGO_DAEMON_GROUP='sogod'

    export SOGO_GNUSTEP_DIR='/usr/local/GNUstep/Local/Library/SOGo'
    export SOGO_RC_SCRIPT_NAME='sogod'
    export SOGO_CONF_DIR='/usr/local/etc/sogo'

    export SOGO_CMD_TOOL='/usr/local/GNUstep/Local/Tools/Admin/sogo-tool'
    export SOGO_CMD_EALARMS_NOTIFY='/usr/local/GNUstep/Local/Tools/Admin/sogo-ealarms-notify'
elif [ X"${DISTRO}" == X'OPENBSD' ]; then
    export SOGO_DAEMON_USER='_sogo'
    export SOGO_DAEMON_GROUP='_sogo'

    export SOGO_GNUSTEP_DIR='/usr/local/lib/GNUstep/SOGo'
    export SOGO_CMD_TOOL='/usr/local/sbin/sogo-tool'
    export SOGO_CMD_EALARMS_NOTIFY='/usr/local/sbin/sogo-ealarms-notify'
fi

export SOGO_CONF="${SOGO_CONF_DIR}/sogo.conf"
export SOGO_SIEVE_CREDENTIAL_FILE="${SOGO_CONF_DIR}/sieve.cred"
export SOGO_CRON_FILE="${CRON_SPOOL_DIR}/${SOGO_DAEMON_USER}"

if [ X"${BACKEND}" == X'OPENLDAP' -o X"${BACKEND}" == X'MYSQL' ]; then
    export SOGO_DB_TYPE='mysql'
elif [ X"${BACKEND}" == X'PGSQL' ]; then
    export SOGO_DB_TYPE='postgresql'
fi

# Backup database.
if [ X"${USE_SOGO}" == X'YES' ]; then
    export MYSQL_BACKUP_DATABASES="${MYSQL_BACKUP_DATABASES} ${SOGO_DB_NAME}"
    export PGSQL_BACKUP_DATABASES="${PGSQL_BACKUP_DATABASES} ${SOGO_DB_NAME}"
fi
