#!/bin/bash
#
#
#   OPEN-XCHANGE legal information
#
#   All intellectual property rights in the Software are protected by
#   international copyright laws.
#
#
#   In some countries OX, OX Open-Xchange, open xchange and OXtender
#   as well as the corresponding Logos OX Open-Xchange and OX are registered
#   trademarks of the Open-Xchange, Inc. group of companies.
#   The use of the Logos is not covered by the GNU General Public License.
#   Instead, you are allowed to use these Logos according to the terms and
#   conditions of the Creative Commons License, Version 2.5, Attribution,
#   Non-commercial, ShareAlike, and the interpretation of the term
#   Non-commercial applicable to the aforementioned license is published
#   on the web site http://www.open-xchange.com/EN/legal/index.html.
#
#   Please make sure that third-party modules and libraries are used
#   according to their respective licenses.
#
#   Any modifications to this package must retain all copyright notices
#   of the original copyright holder(s) for the original code used.
#
#   After any such modifications, the original and derivative code shall remain
#   under the copyright of the copyright holder(s) and/or original author(s)per
#   the Attribution and Assignment Agreement that can be located at
#   http://www.open-xchange.com/EN/developer/. The contributing author shall be
#   given Attribution for the derivative code and a license granting use.
#
#    Copyright (C) 2004-2010 Open-Xchange, Inc.
#    Mail: info@open-xchange.com
#
#
#    This program is free software; you can redistribute it and/or modify it
#    under the terms of the GNU General Public License, Version 2 as published
#    by the Free Software Foundation.
#
#    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 General Public License
#    for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc., 59
#    Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#

OXFUNCTIONS=@oxfunctions@
OXCONFIG=@oxscriptconf@
OXPREFIX=@prefix@
OXBUNDLES=$OXPREFIX/bundles


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 "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 "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/groupware/cache.ccf
SESSIONCACHECCF=$OXPREFIX/etc/groupware/sessioncache.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"

SESSIONUDPPORT=$(readProp jcs.auxiliary.SessionLTCP.attributes.UdpDiscoveryPort $SESSIONCACHECCF)
SESSIONUDPDISCADDR=$(readProp jcs.auxiliary.SessionLTCP.attributes.UdpDiscoveryAddr $SESSIONCACHECCF)
SESSIONTCPSERVER=$(readProp jcs.auxiliary.SessionLTCP.attributes.TcpServers $SESSIONCACHECCF)
SESSIONLPORT=$(readProp jcs.auxiliary.SessionLTCP.attributes.TcpListenerPort $SESSIONCACHECCF)
SESSIONUDPDISCENABLED=$(readProp jcs.auxiliary.SessionLTCP.attributes.UdpDiscoveryEnabled $SESSIONCACHECCF)

warnPortOrServer "$SESSIONUDPDISCENABLED" "$SESSIONTCPSERVER" "$SESSIONCACHECCF"
warnUdpIncomplete "$SESSIONUDPDISCENABLED" "$SESSIONUDPDISCADDR" "$SESSIONUDPPORT" "$SESSIONCACHECCF"



if [ -n "$SESSIONUDPPORT" ] && [ -n "$SERVERUDPPORT" ] && \
    [ "$SESSIONUDPPORT" == "$SERVERUDPPORT" ]; then
      warn "UdpDiscoveryPort in"
      warn "$SESSIONCACHECCF is configured to use the same port as"
      warn "UdpDiscoveryPort in"
      warn "$CACHECCF"
      warn "that will not work, please choose different ports"
      echo
      echo
fi

if [ -n "$SESSIONLPORT" ] && [ -n "$SERVERLPORT" ] && \
    [ "$SESSIONLPORT" == "$SERVERLPORT" ]; then
      warn "TcpListenerPort in"
      warn "$SESSIONCACHECCF is configured to use the same port as"
      warn "TcpListenerPort in"
      warn "$CACHECCF"
      warn "that will not work, please choose different ports"
      echo
      echo
fi

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

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

if [ -s $ADMINCACHECCF ]; 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)
    
    if [ -n "$SESSIONUDPPORT" ] && [ -n "$ADMINUDPPORT" ] && \
	[ "$SESSIONUDPPORT" == "$ADMINUDPPORT" ]; then
	warn "UdpDiscoveryPort in"
	warn "$SESSIONCACHECCF is configured to use the same port as"
	warn "UdpDiscoveryPort in"
	warn "$ADMINCACHECCF"
	warn "that will not work, please choose different ports"
	echo
	echo
    fi
    
    if [ -n "$SESSIONLPORT" ] && [ -n "$ADMINLPORT" ] && \
	[ "$SESSIONLPORT" == "$ADMINLPORT" ]; then
	warn "TcpListenerPort in"
	warn "$SESSIONCACHECCF is configured to use the same port as"
	warn "TcpListenerPort in"
	warn "$ADMINCACHECCF"
	warn "that will not work, please choose different ports"
	echo
	echo
    fi

    if [ -n "$SERVERLPORT" ] && [ -n "$ADMINLPORT" ] && [ "$SERVERLPORT" == "$ADMINLPORT" ]; then
	warn "TcpListenerPort in"
	warn "$CACHECCF is configured to use the same port as"
	warn "TcpListenerPort in"
	warn "$ADMINCACHECCF"
	warn "that will not work, please choose different ports"
	echo
	echo
    fi


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

    if [ "$ADMINUDPDISCENABLED" != "$SERVERUDPDISCENABLED" ];then
	warn "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 "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 "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 "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 "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
