#!/bin/bash
#
#
#   @copyright Copyright (c) Open-Xchange GmbH, Germany <info@open-xchange.com>
#   @license AGPL-3.0
#
#   This code is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program 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 Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with OX App Suite.  If not, see <https://www.gnu.org/licenses/agpl-3.0.txt>.
#
#   Any use of the work other than as authorized under this license or copyright law is prohibited.
#
#

OXFUNCTIONS=/opt/open-xchange/lib/oxfunctions.sh
OXCONFIG=/opt/open-xchange/etc/ox-scriptconf.sh
OXPREFIX=/opt/open-xchange
OXBUNDLES=$OXPREFIX/bundles

usage() {
    echo -e "Usage: checkconfigurationconsistency\n"
    echo -e "  -h, --help   Shows this help screen\n"
    echo -e "Checks the middleware's configuration for some well known configuration mistakes.\n"
}

if [[ $1 == "-h" || $1 == "--help" ]]; then
    usage
    exit 0
fi

test -f $OXFUNCTIONS || {
	echo "missing common shell functions file"
	exit 1
}

. $OXFUNCTIONS

test -f $OXCONFIG && . $OXCONFIG

warn() {
    echo -e "\e[1m$1\e[0m"
}

warnPortOrServer() {
    local udpenabled="$1"
    local tcpserver="$2"
    local cache="$3"

    if [ -n "$udpenabled" ] && [ "$udpenabled" == "true" ]; then
	if [ -n "$tcpserver" ]; then
	    warn "Warning: TcpServers is set and"
	    warn "UdpDiscoveryEnabled is set to true"
	    warn "in $cache"
	    warn "either set UdpDiscoveryEnabled to false or uncomment TcpServers"
	    echo
	    echo
	fi
    fi
}

warnUdpIncomplete() {
    local udpenabled="$1"
    local udpport="$2"
    local udpaddr="$3"
    local cache="$4"

    if [ -n "$udpenabled" ] && [ "$udpenabled" == "true" ]; then
	if [ -z "$udpaddr" ] || [ -z "$udpport" ]; then
	    warn "Warning: UdpDiscoveryEnabled is enabled, but"
	    warn "either UdpDiscoveryAddr or"
	    warn "UdpDiscoveryPort is not set"
	    warn "in $cache"
	    warn "when UdpDiscoveryEnabled is true, both options must be set"
	    echo
	    echo
	fi
    fi
}


readProp() {
    local prop="$1"
    local file="$2"
    if ox_exists_property $prop $file; then
	ox_read_property $prop $file
    fi
}

CACHECCF=$OXPREFIX/etc/cache.ccf



SERVERUDPPORT=$(readProp jcs.auxiliary.LTCP.attributes.UdpDiscoveryPort $CACHECCF)
SERVERUDPDISCADDR=$(readProp jcs.auxiliary.LTCP.attributes.UdpDiscoveryAddr $CACHECCF)
SERVERTCPSERVER=$(readProp jcs.auxiliary.LTCP.attributes.TcpServers $CACHECCF)
SERVERLPORT=$(readProp jcs.auxiliary.LTCP.attributes.TcpListenerPort $CACHECCF)
SERVERUDPDISCENABLED=$(readProp jcs.auxiliary.LTCP.attributes.UdpDiscoveryEnabled $CACHECCF)

warnPortOrServer "$SERVERUDPDISCENABLED" "$SERVERTCPSERVER" "$CACHECCF"
warnUdpIncomplete "$SERVERUDPDISCENABLED" "$SERVERUDPDISCADDR" "$SERVERUDPPORT" "$CACHECCF"


ADMINCACHECCF=$OXPREFIX/etc/cache.ccf
ADMINSYSTEMPROPS=$OXPREFIX/etc/system.properties

if [ -s $ADMINSYSTEMPROPS ]; then
    ADMINCACHECCFPATH=$(readProp com.openexchange.caching.configfile $ADMINSYSTEMPROPS)
    if [ -z "$ADMINCACHECCFPATH" ] || \
	[ "$ADMINCACHECCFPATH" != "cache.ccf" ]; then
	warn "Warning: com.openexchange.caching.configfile in $ADMINSYSTEMPROPS is either empty"
	warn "or contains the wrong path."
	warn "correct path must be cache.ccf"
    fi
fi

if [ -s $ADMINCACHECCF ] && [ $ADMINCACHECCF != $CACHECCF ]; then

    ADMINUDPPORT=$(readProp jcs.auxiliary.LTCP.attributes.UdpDiscoveryPort $ADMINCACHECCF)
    ADMINUDPDISCADDR=$(readProp jcs.auxiliary.LTCP.attributes.UdpDiscoveryAddr $ADMINCACHECCF)
    ADMINTCPSERVER=$(readProp jcs.auxiliary.LTCP.attributes.TcpServers $ADMINCACHECCF)
    ADMINLPORT=$(readProp jcs.auxiliary.LTCP.attributes.TcpListenerPort $ADMINCACHECCF)
    ADMINUDPDISCENABLED=$(readProp jcs.auxiliary.LTCP.attributes.UdpDiscoveryEnabled $ADMINCACHECCF)

    warnPortOrServer "$ADMINUDPDISCENABLED" "$ADMINTCPSERVER" "$ADMINCACHECCF"
    warnUdpIncomplete "$ADMINUDPDISCENABLED" "$ADMINUDPDISCADDR" "$ADMINUDPPORT" "$ADMINCACHECCF"

    if [ "$ADMINUDPDISCENABLED" != "$SERVERUDPDISCENABLED" ];then
	warn "Warning: UdpDiscoveryEnabled is not, but must equal in"
	warn "$CACHECCF"
	warn "and"
	warn "$ADMINCACHECCF"
	echo
	echo
    elif [ -z "$ADMINUDPDISCENABLED" ] || [ "$ADMINUDPDISCENABLED" == "false" ]; then
        portfound=
        for i in ${ADMINTCPSERVER/,/ }; do
	    if [ "$SERVERLPORT" == "${i/*:}" ]; then
		portfound=true
	    fi
	done
	if [ -z "$portfound" ]; then
	    warn "Warning: TcpListenerPort in"
	    warn "$CACHECCF"
	    warn "must be the same port as in"
	    warn "TcpServers in"
	    warn "$ADMINCACHECCF"
	    warn "if not, cache co-operation of admin and server will not work properly"
	    echo
	    echo
	fi
        portfound=
        for i in ${SERVERTCPSERVER/,/ }; do
	    if [ "$ADMINLPORT" == "${i/*:}" ]; then
		portfound=true
	    fi
	done
	if [ -z "$portfound" ]; then
	    warn "Warning: TcpListenerPort in"
	    warn "$ADMINCACHECCF"
	    warn "must be the same port as in"
	    warn "TcpServers in"
	    warn "$CACHECCF"
	    warn "if not, cache co-operation of admin and server will not work properly"
	    echo
	    echo
	fi
    else
	if [ "$ADMINUDPDISCADDR" != "$SERVERUDPDISCADDR" ]; then
	    warn "Warning: UdpDiscoveryAddr in"
	    warn "$ADMINCACHECCF"
	    warn "is not, but must be the same address as in"
	    warn "$CACHECCF"
	    warn "if not, cache co-operation of admin and server will not work properly"
	    echo
	    echo
	fi
	if [ "$ADMINUDPPORT" != "$SERVERUDPPORT" ]; then
	    warn "Warning: UdpDiscoveryPort in"
	    warn "$ADMINCACHECCF"
	    warn "is not, but must be the same port as in"
	    warn "$CACHECCF"
	    warn "if not, cache co-operation of admin and server will not work properly"
	    echo
	    echo
	fi
    fi

fi
