
#
# Copyright (C) 2019 OX Software GmbH
# 
# This file is part of OX Automation.
#
# OX Automation 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.
#
# OX Automation 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 OX Automation. If not, see <http://www.gnu.org/licenses/>.
#
#
# Authors:
# Benedikt Kroening <benedikt.kroening@open-xchange.com>
#

# This restarts the provided service name with the configured service managers
function restartService {
    echo "Restarting service $1"
    if [[ "${INSTALL_TYPE}" != "docker" ]]; then
        if [[ "${DIST}" == "SLES12" ]] || [[ "${DIST}" == "RHEL7" ]]; then
            systemctl restart $1.service
        else
            service $1 restart
        fi
    else
        #echo "ERROR: Not possible to determine installation type, could not restart service $1"
        supvervisorctl restart $1
    fi


}


function detectDistribution {
    if [[ -f /etc/debian_version ]]; then
        REV=$(cat /etc/debian_version)
        if [[ "${REV}" == "9."* ]]; then
            DIST="DebianStretch"
            ANY_DEB=true
            MARIADB=true
            ADDITIONAL_PACKAGES="mariadb-server"
            HTTPD="apache2"
            HTTPD_CONF_PATH=/etc/${HTTPD}/conf-enabled/
            HTTPD_SITES_PATH=/etc/${HTTPD}/sites-enabled/
            HTTPD_WWWROOT_PATH=/var/www/html
            HTTPD_LOG_PATH=\$\{APACHE_LOG_DIR\}
            HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            HTTPD_TLS_CRT=/etc/ssl/certs/ssl-cert-snakeoil.pem
            HTTPD_TLS_KEY=/etc/ssl/private/ssl-cert-snakeoil.key
            DISTRO="DebianStretch"
            COMPATIBLE=true
        fi
    elif [[ -f /etc/SuSE-release ]]; then
        # Installing wget to get buildkeys
        zypper -n up
        zypper -n in wget

        DIST=$(tr "\n" ' ' < /etc/SuSE-release | sed s/VERSION.*//)
        SLES_PATCHLEVEL=$(cat /etc/SuSE-release | grep PATCHLEVEL)
        if [[ "${DIST}" == "SUSE Linux Enterprise Server 12"* ]] && [[ "${SLES_PATCHLEVEL}" != "PATCHLEVEL = 0" ]]; then
            DIST="SLES12"
            ANY_RPM=true
            ANY_SLES=true
            ADDITIONAL_PACKAGES="mariadb"
            HTTPD="apache2"
            HTTPD_CONF_PATH=/etc/${HTTPD}/conf.d/
            HTTPD_SITES_PATH=/etc/${HTTPD}/conf.d/
            HTTPD_WWWROOT_PATH=/srv/www/htdocs
            HTTPD_LOG_PATH=/var/log/apache2
            HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            HTTPD_TLS_CRT=/etc/apache2/ssl.crt/server.crt
            HTTPD_TLS_KEY=/etc/apache2/ssl.key/server.key
            DISTRO="SLE_12"
            COMPATIBLE=true
        fi
    elif [[ -f /etc/redhat-release ]]; then
        REV=$(sed s/.*Red\ Hat\ Enterprise\ Linux\ Server\ release\ // < /etc/redhat-release | sed s/\ .*//)
        if [[ "${REV}" == "6."* ]]; then
            DIST="RHEL6"
            ANY_RPM=true
            ANY_RHEL=true
            MARIADB=true
            ADDITIONAL_PACKAGES="MariaDB-server"
            HTTPD="httpd"
            HTTPD_CONF_PATH=/etc/${HTTPD}/conf.d/
            HTTPD_SITES_PATH=/etc/${HTTPD}/conf.d/
            HTTPD_WWWROOT_PATH=/var/www/html
            HTTPD_LOG_PATH=logs
            HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            HTTPD_TLS_CRT=/etc/pki/tls/certs/localhost.crt
            HTTPD_TLS_KEY=/etc/pki/tls/private/localhost.key
            DISTRO="RHEL6"
            COMPATIBLE=true
        elif [[ "${REV}" == "7."* ]]; then
            DIST="RHEL7"
            ANY_RPM=true
            ANY_RHEL=true
            ADDITIONAL_PACKAGES="mariadb-server"
            HTTPD="httpd"
            HTTPD_CONF_PATH=/etc/${HTTPD}/conf.d/
            HTTPD_SITES_PATH=/etc/${HTTPD}/conf.d/
            HTTPD_WWWROOT_PATH=/var/www/html
            HTTPD_LOG_PATH=logs
            HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            HTTPD_TLS_CRT=/etc/pki/tls/certs/localhost.crt
            HTTPD_TLS_KEY=/etc/pki/tls/private/localhost.key
            DISTRO="RHEL7"
            COMPATIBLE=true
        fi
        REV=$(sed s/.*CentOS\ release\ // < /etc/redhat-release | sed s/\ .*//)
        if [[ "${REV}" == "6."* ]]; then
            DIST="CENTOS6"
            ANY_RPM=true
            ANY_CENTOS=true
            MARIADB=true
            ADDITIONAL_PACKAGES="MariaDB-server"
            HTTPD="httpd"
            HTTPD_CONF_PATH=/etc/${HTTPD}/conf.d/
            HTTPD_SITES_PATH=/etc/${HTTPD}/conf.d/
            HTTPD_WWWROOT_PATH=/var/www/html
            HTTPD_LOG_PATH=logs
            HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            HTTPD_TLS_CRT=/etc/pki/tls/certs/localhost.crt
            HTTPD_TLS_KEY=/etc/pki/tls/private/localhost.key
            DISTRO="RHEL6"
            COMPATIBLE=true
        fi
        REV=$(sed s/.*CentOS\ Linux\ release\ // < /etc/redhat-release | sed s/\ .*//)
        if [[ "${REV}" == "7."* ]]; then
            DIST="CENTOS7"
            ANY_RPM=true
            ANY_CENTOS=true
            MARIADB=true
            ADDITIONAL_PACKAGES="mariadb-server"
            HTTPD="httpd"
            HTTPD_CONF_PATH=/etc/${HTTPD}/conf.d/
            HTTPD_SITES_PATH=/etc/${HTTPD}/conf.d/
            HTTPD_WWWROOT_PATH=/var/www/html
            HTTPD_LOG_PATH=logs
            HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            HTTPD_TLS_CRT=/etc/pki/tls/certs/localhost.crt
            HTTPD_TLS_KEY=/etc/pki/tls/private/localhost.key
            DISTRO="RHEL7"
            COMPATIBLE=true
        fi
    elif [[ -f /etc/os-release ]]; then
        declare -A OSRELEASE

        while IFS='=' read -r col1 col2
        do
          OSRELEASE[$col1]="${col2//\"/}"
        done </etc/os-release

        if [[ ${OSRELEASE[ID]} == amzn  && ${OSRELEASE[VERSION_ID]} == 2 ]]
        then
            # Amazon Linux 2
            DIST="RHEL7"
            ANY_RPM=true
            ANY_RHEL=true
            ADDITIONAL_PACKAGES="mariadb-server"
            HTTPD="httpd"
            HTTPD_CONF_PATH=/etc/${HTTPD}/conf.d/
            HTTPD_SITES_PATH=/etc/${HTTPD}/conf.d/
            HTTPD_WWWROOT_PATH=/var/www/html
            HTTPD_LOG_PATH=logs
            HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            HTTPD_TLS_CRT=/etc/pki/tls/certs/localhost.crt
            HTTPD_TLS_KEY=/etc/pki/tls/private/localhost.key
            DISTRO="RHEL7"
            COMPATIBLE=true
        fi
    fi
    if [[ "${COMPATIBLE}" != true ]]; then
        echo "unknown."
        echo ""
        echo "Supported platforms:"
        echo "* Debian GNU/Linux 9 (Stretch)"
        echo "* Novell SuSE Linux Enterprise Server 12 (SLES12) >= SP1"
        echo "* RedHat Enterprise Linux 6 (RHEL6)"
        echo "* RedHat Enterprise Linux 7 (RHEL7)"
        echo "* Amazon Linux 2"
        echo "* CentOS 6"
        echo "* CentOS 7"
        exit 1
    fi
}

function checkRootPrivileges {
    echo -n "Checking user..."
    if [[ $(whoami) != "root" ]]; then
        echo "Failed."
        echo "Please run this script as superuser (root)."
        exit 1
    else
        echo "OK."
    fi
}

function setOXProperty {
    # key value file
    if [ ! -f /opt/open-xchange/lib/oxfunctions.sh ]; then
        echo "ERROR: oxfunctions.sh file does not exist"
        exit 1
    fi

    source /opt/open-xchange/lib/oxfunctions.sh

    if [ ! -f $3 ]; then
        echo "File $3 does not exist ... creating ..."
        touch $3
    fi

    echo "Setting ox property: $@"
    ox_set_property "$@"
}

function waitPort {
  echo "Waiting for port $2 on host $1 ..."
  
  while ! nc -z $1 $2; do 
    sleep 1 
    echo ". "
  done


  echo "Port available"
}

function exportProperties {
    #echo "Trying to export variables from $1 ..."
    # if [ ! -f "$1" ]; then
    #     echo "ERROR: Given properties file does not exist"
    #     exit 1
    # fi
    # val=$(grep -v '^#' "$1" | xargs)
    
    # export $(eval ${val})

    if [ -f "$1" ]
    then
    echo "$1 found."

    while IFS='=' read -r key value
    do
        if [[ $key != \#* ]] && [[ -n $key ]] ; then        
            export $key=$(eval echo "$value")
        fi

    done < "$1"

    else
        echo "$1 not found."
    fi
}