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

export APACHE_RC_SCRIPT_NAME='apache2'

if [ X"${DISTRO}" == X'RHEL' ]; then
    export APACHE_RC_SCRIPT_NAME='httpd'
    # Apache configuration files.
    export HTTPD_CONF_ROOT="/etc/httpd"
    export HTTPD_CONF="${HTTPD_CONF_ROOT}/conf/httpd.conf"
    export HTTPD_CONF_DIR="${HTTPD_CONF_ROOT}/conf.d"
    export HTTPD_SSL_CONF="${HTTPD_CONF_DIR}/ssl.conf"
    if [ X"${DISTRO_VERSION}" == X'6' ]; then
        export HTTPD_WSGI_CONF="${HTTPD_CONF_DIR}/wsgi.conf"
    else
        export APACHE_VERSION='2.4'
        export HTTPD_MODULES_DIR="${HTTPD_CONF_ROOT}/conf.modules.d"
        export HTTPD_WSGI_CONF="${HTTPD_MODULES_DIR}/10-wsgi.conf"
    fi

    # Daemon user.
    export HTTPD_USER='apache'
    export HTTPD_GROUP='apache'

    # Log file location.
    export HTTPD_LOG_ACCESSLOG='/var/log/httpd/access_log'
    export HTTPD_LOG_ERRORLOG='/var/log/httpd/error_log'

elif [ X"${DISTRO}" == X'DEBIAN' -o X"${DISTRO}" == X'UBUNTU' ]; then
    # Apache configuration files.
    export HTTPD_CONF_ROOT="/etc/apache2"
    export HTTPD_CONF="${HTTPD_CONF_ROOT}/apache2.conf"

    if [ X"${DISTRO_CODENAME}" == X'wheezy' ]; then
        export HTTPD_CONF_DIR="${HTTPD_CONF_ROOT}/conf.d"
        export HTTPD_SSL_CONF="${HTTPD_CONF_ROOT}/sites-available/default-ssl"
        export HTTPD_MOD_CONF_SECURITY="${HTTPD_CONF_DIR}/security"
    else
        export APACHE_VERSION='2.4'
        export HTTPD_CONF_DIR="${HTTPD_CONF_ROOT}/conf-available"
        export HTTPD_SSL_CONF="${HTTPD_CONF_ROOT}/sites-available/default-ssl.conf"
        export HTTPD_MOD_CONF_SECURITY="${HTTPD_CONF_DIR}/security.conf"
        export HTTPD_DOCUMENTROOT="/var/www/html"
    fi

    # Log file location.
    export HTTPD_LOG_ACCESSLOG='/var/log/apache2/access.log'
    export HTTPD_LOG_ERRORLOG='/var/log/apache2/error.log'

elif [ X"${DISTRO}" == X'FREEBSD' ]; then
    export APACHE_VERSION='2.4'
    export APACHE_RC_SCRIPT_NAME='apache24'

    # Apache configuration files.
    export HTTPD_CONF_ROOT="/usr/local/etc/apache24"
    export HTTPD_CONF="${HTTPD_CONF_ROOT}/httpd.conf"
    export HTTPD_CONF_DIR="${HTTPD_CONF_ROOT}/Includes"
    export HTTPD_MODULES_DIR="${HTTPD_CONF_ROOT}/modules.d"

    # Copy from example: /usr/local/share/examples/apache24/extra/httpd-ssl.conf
    export HTTPD_SSL_CONF="${HTTPD_CONF_ROOT}/extra/httpd-ssl.conf"

    # Web data.
    export HTTPD_SERVERROOT='/usr/local/www'
    export HTTPD_DOCUMENTROOT="${HTTPD_SERVERROOT}/apache24/data"

    # Log file location.
    export HTTPD_LOG_ACCESSLOG='/var/log/httpd-access.log'
    export HTTPD_LOG_ERRORLOG='/var/log/httpd-error.log'

elif [ X"${DISTRO}" == X'OPENBSD' ]; then
    export APACHE_RC_SCRIPT_NAME='httpd'

    # Apache configuration files.
    export HTTPD_CONF_ROOT="/var/www/conf"
    export HTTPD_CONF="${HTTPD_CONF_ROOT}/httpd.conf"
    export HTTPD_CONF_DIR="${HTTPD_CONF_ROOT}/modules"
    export HTTPD_SSL_CONF="${HTTPD_CONF}"

    # Log file location.
    export HTTPD_LOG_ACCESSLOG="${HTTPD_SERVERROOT}/logs/access_log"
    export HTTPD_LOG_ERRORLOG="${HTTPD_SERVERROOT}/logs/error_log"

fi

export HTTPD_CGIBIN_DIR="${HTTPD_SERVERROOT}/cgi-bin"

if [[ X"${APACHE_VERSION}" == X'2.4' ]]; then
    export HTACCESS_ALLOW_ALL='Require all granted'
else
    export HTACCESS_ALLOW_ALL='Order allow,deny
Allow from all'
fi
