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

# -----------------------------------------------------------------------------
# define default memory limit to 3 GB in case it's not defined from outside !
# value has to be defined in [Byte]

DEFAULT_MEMORY_LIMIT=3145728

# -----------------------------------------------------------------------------
# enable virtual memory limitation by default
# Has to be set to 'false' in case worker do not start which can happen
# on systems with 'outdated glibc & openjdk'
# true  -> enable  limitation
# false -> disable limitation

LIMIT_VMEM=true

# -----------------------------------------------------------------------------
# main

OXFUNCTIONS=/opt/open-xchange/lib/oxfunctions.sh
OXCONFIG=/opt/open-xchange/etc/ox-scriptconf.sh

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

. $OXFUNCTIONS

test -f $OXCONFIG && . $OXCONFIG

ox_set_JAVA_BIN

test -z "$SERVERNAME" && SERVERNAME=$HOSTNAME

test -z "$JAVA_OXCMD_OPTS" && JAVA_OXCMD_OPTS="-Xmx256M"

JAVA_OPTS="${JAVA_OXCMD_OPTS} \
-Djava.awt.headless=true \
-Dopenexchange.propdir=$PROPERTIESDIR"

test -z "$LIBPATH" && LIBPATH=${PREFIX}/lib

# parse command line with all possible options ...

while [[ $# > 0 ]]
do
key="$1"
shift

case $key in
    -h|--help)
    ARG_HELP=true
    ;;
    -i|--interface)
    ARG_INTERFACE="$1"
    shift
    ;;
    -p|--port)
    ARG_PORT="$1"
    shift
    ;;
    -r|--recording)
    ARG_RECORDING="$1"
    shift
    ;;
    -s|--simulate)
    ARG_SIMULATE=true
    ;;
    -m|--memory)
    ARG_MEMORY="$1"
    shift
    ;;
    *)
    # unknown option
    ;;
esac
done

# apply all options directly within this script which needs to be handled here
# record all other options (provided by the JAR-Main called later) and pass it to the JAR !

ARG_JAR_MAIN=""

if [ ! -z $ARG_HELP ];
    then
        ARG_JAR_MAIN="$ARG_JAR_MAIN -h"
fi
if [ ! -z $ARG_INTERFACE ];
    then
        ARG_JAR_MAIN="$ARG_JAR_MAIN -i $ARG_INTERFACE"
fi
if [ ! -z $ARG_PORT ];
    then
        ARG_JAR_MAIN="$ARG_JAR_MAIN -p $ARG_PORT"
fi
if [ ! -z $ARG_RECORDING ];
    then
        ARG_JAR_MAIN="$ARG_JAR_MAIN -r $ARG_RECORDING"
fi
if [ ! -z $ARG_SIMULATE ];
    then
        ARG_JAR_MAIN="$ARG_JAR_MAIN -s"
fi
if [ ! -z $ARG_MEMORY ];
    then
        if [ "$LIMIT_VMEM" == "true" ];
        	then
	        echo "define virtual memory limit : $ARG_MEMORY KB ..."
	        ulimit -v $ARG_MEMORY
        fi
		echo "define memory limit         : $ARG_MEMORY KB ..."
        ulimit -m $ARG_MEMORY
    else
        if [ "$LIMIT_VMEM" == "true" ];
        	then
	        echo "define virtual memory limit (DEFAULT!) : $DEFAULT_MEMORY_LIMIT KB ..."
        	ulimit -v $DEFAULT_MEMORY_LIMIT
        fi
		echo "define memory limit         (DEFAULT!) : $DEFAULT_MEMORY_LIMIT KB ..."
        ulimit -m $DEFAULT_MEMORY_LIMIT
fi

# ensure we will find C++ part of calcengine core
export LD_LIBRARY_PATH=/opt/calcengine/lib

exec $JAVA_BIN $JAVA_OPTS -jar $LIBPATH/com.openexchange.office.calcengine.worker.jar $ARG_JAR_MAIN
exit $?
