
#
# 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 {
    
    if [[ "${INSTALL_TYPE}" == *"docker"* ]]; then
        echo "Restarting service $1 with supervisorctl ..."
        supervisorctl restart $1
    else
        #echo "ERROR: Not possible to determine installation type, could not restart service $1"
        echo "Restarting service $1 ..."    
        if [[ "${DIST}" == "SLES12" ]] || [[ "${DIST}" == "RHEL7" ]]; then
            systemctl restart $1.service
        else
            service $1 restart
        fi
    fi


}


function detectDistribution {
    if [[ -f /etc/debian_version ]]; then
        REV=$(cat /etc/debian_version)
        if [[ "${REV}" == "9."* ]]; then
            export DIST="DebianStretch"
            export DIST_VERSION="stretch"
            export ANY_DEB=true
            export HTTPD="apache2"
            export HTTPD_CONF_PATH=/etc/${HTTPD}/conf-enabled/
            export HTTPD_SITES_PATH=/etc/${HTTPD}/sites-enabled/
            export HTTPD_WWWROOT_PATH=/var/www/html
            export HTTPD_LOG_PATH=\$\{APACHE_LOG_DIR\}
            export HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            export HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            export HTTPD_TLS_CRT=/etc/ssl/certs/ssl-cert-snakeoil.pem
            export HTTPD_TLS_KEY=/etc/ssl/private/ssl-cert-snakeoil.key
            export DISTRO="DebianStretch"
            export COMPATIBLE=true
        elif [[ "${REV}" == "10."* ]]; then
            export DIST="DebianBuster"
            export DIST_VERSION="buster"
            export ANY_DEB=true
            export HTTPD="apache2"
            export HTTPD_CONF_PATH=/etc/${HTTPD}/conf-enabled/
            export HTTPD_SITES_PATH=/etc/${HTTPD}/sites-enabled/
            export HTTPD_WWWROOT_PATH=/var/www/html
            export HTTPD_LOG_PATH=\$\{APACHE_LOG_DIR\}
            export HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            export HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            export HTTPD_TLS_CRT=/etc/ssl/certs/ssl-cert-snakeoil.pem
            export HTTPD_TLS_KEY=/etc/ssl/private/ssl-cert-snakeoil.key
            export DISTRO="DebianBuster"
            export COMPATIBLE=true     
        fi
        export DEBIAN_FRONTEND=noninteractive
    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
            export DIST="SLES12"
            export ANY_RPM=true
            export ANY_SLES=true
            export ADDITIONAL_PACKAGES="mariadb"
            export HTTPD="apache2"
            export HTTPD_CONF_PATH=/etc/${HTTPD}/conf.d/
            export HTTPD_SITES_PATH=/etc/${HTTPD}/conf.d/
            export HTTPD_WWWROOT_PATH=/srv/www/htdocs
            export HTTPD_LOG_PATH=/var/log/apache2
            export HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            export HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            export HTTPD_TLS_CRT=/etc/apache2/ssl.crt/server.crt
            export HTTPD_TLS_KEY=/etc/apache2/ssl.key/server.key
            export DISTRO="SLE_12"
            export 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
            export DIST="RHEL6"
            export DIST_VERSION="6"
            export ANY_RPM=true
            export ANY_RHEL=true
            export HTTPD="httpd"
            export HTTPD_CONF_PATH=/etc/${HTTPD}/conf.d/
            export HTTPD_SITES_PATH=/etc/${HTTPD}/conf.d/
            export HTTPD_WWWROOT_PATH=/var/www/html
            export HTTPD_LOG_PATH=logs
            export HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            export HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            export HTTPD_TLS_CRT=/etc/pki/tls/certs/localhost.crt
            export HTTPD_TLS_KEY=/etc/pki/tls/private/localhost.key
            export DISTRO="RHEL6"
            export COMPATIBLE=true
        elif [[ "${REV}" == "7."* ]]; then
            export DIST="RHEL7"
            export DIST_VERSION="7"
            export ANY_RPM=true
            export ANY_RHEL=true
            export HTTPD="httpd"
            export HTTPD_CONF_PATH=/etc/${HTTPD}/conf.d/
            export HTTPD_SITES_PATH=/etc/${HTTPD}/conf.d/
            export HTTPD_WWWROOT_PATH=/var/www/html
            export HTTPD_LOG_PATH=logs
            export HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            export HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            export HTTPD_TLS_CRT=/etc/pki/tls/certs/localhost.crt
            export HTTPD_TLS_KEY=/etc/pki/tls/private/localhost.key
            export DISTRO="RHEL7"
            export COMPATIBLE=true
        fi
        REV=$(sed s/.*CentOS\ release\ // < /etc/redhat-release | sed s/\ .*//)
        if [[ "${REV}" == "6."* ]]; then
            export DIST="CENTOS6"
            export DIST_VERSION="6"
            export ANY_RPM=true
            export ANY_CENTOS=true
            export HTTPD="httpd"
            export HTTPD_CONF_PATH=/etc/${HTTPD}/conf.d/
            export HTTPD_SITES_PATH=/etc/${HTTPD}/conf.d/
            export HTTPD_WWWROOT_PATH=/var/www/html
            export HTTPD_LOG_PATH=logs
            export HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            export HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            export HTTPD_TLS_CRT=/etc/pki/tls/certs/localhost.crt
            export HTTPD_TLS_KEY=/etc/pki/tls/private/localhost.key
            export DISTRO="RHEL6"
            export COMPATIBLE=true
        fi
        REV=$(sed s/.*CentOS\ Linux\ release\ // < /etc/redhat-release | sed s/\ .*//)
        if [[ "${REV}" == "7."* ]]; then
            export DIST="CENTOS7"
            export DIST_VERSION="7"
            export ANY_RPM=true
            export ANY_CENTOS=true
            export HTTPD="httpd"
            export HTTPD_CONF_PATH=/etc/${HTTPD}/conf.d/
            export HTTPD_SITES_PATH=/etc/${HTTPD}/conf.d/
            export HTTPD_WWWROOT_PATH=/var/www/html
            export HTTPD_LOG_PATH=logs
            export HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            export HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            export HTTPD_TLS_CRT=/etc/pki/tls/certs/localhost.crt
            export HTTPD_TLS_KEY=/etc/pki/tls/private/localhost.key
            export DISTRO="RHEL7"
            export 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
            export DIST="RHEL7"
            export DIST_VERSION="7"
            export ANY_RPM=true
            export ANY_RHEL=true
            export HTTPD="httpd"
            export HTTPD_CONF_PATH=/etc/${HTTPD}/conf.d/
            export HTTPD_SITES_PATH=/etc/${HTTPD}/conf.d/
            export HTTPD_WWWROOT_PATH=/var/www/html
            export HTTPD_LOG_PATH=logs
            export HTTPD_TLS_CIPHERS='ECDH+AESGCM:ECDH+AES256:ECDH+AES128:!aNULL'
            export HTTPD_TLS_PROTOCOLS='-all +TLSv1 +TLSv1.1 +TLSv1.2'
            export HTTPD_TLS_CRT=/etc/pki/tls/certs/localhost.crt
            export HTTPD_TLS_KEY=/etc/pki/tls/private/localhost.key
            export DISTRO="RHEL7"
            export 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 {

    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
}