#!/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 2 GB in case it's not defined from outside !
# value has to be defined in [Byte]

DEFAULT_MEMORY_LIMIT=2097152

# -----------------------------------------------------------------------------
# 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"

CLASSPATH="/opt/open-xchange/bundles/com.openexchange.common/lib/jakarta-slide-webdavlib-2.1.jar:/opt/open-xchange/bundles/com.openexchange.contactcollector.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/automaton-1.11-8.jar:/opt/open-xchange/bundles/com.openexchange.xerces.sun/lib/serializer-2.7.1.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/high-scale-lib-1.1.4.jar:/opt/open-xchange/bundles/com.openexchange.index.jar:/opt/open-xchange/bundles/com.openexchange.office.communication.link/lib/office-communication-link-0.0.1-SNAPSHOT.jar:/opt/open-xchange/bundles/org.apache.commons.io_2.0.1.v201105210651.jar:/opt/open-xchange/bundles/javax.mail/lib/libidn-1.9.jar:/opt/open-xchange/bundles/com.openexchange.osgi.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jersey-json-1.17.1.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/httpcore-4.2.1.jar:/opt/open-xchange/bundles/com.openexchange.push.jar:/opt/open-xchange/bundles/org.eclipse.osgi_3.9.1.v20140110-1610.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/httpclient-4.2.1.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/jericho-html-3.3.jar:/opt/open-xchange/bundles/com.openexchange.net.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/apache-mime4j-dom-0.7.2.jar:/opt/open-xchange/bundles/com.openexchange.messaging.jar:/opt/open-xchange/bundles/org.apache.commons.lang_2.6.0.v201205030909.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/apache-mime4j-core-0.7.2.jar:/opt/open-xchange/bundles/com.openexchange.dataretention.jar:/opt/open-xchange/bundles/com.openexchange.conversion.jar:/opt/open-xchange/bundles/com.openexchange.xml/lib/joox-1.2.0.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.jar:/opt/open-xchange/bundles/com.openexchange.xml/lib/org.dynamicjava.stax-api-1.0.jar:/opt/open-xchange/bundles/com.openexchange.office.communication.link/com.openexchange.office.communication.link.jar:/opt/open-xchange/bundles/com.openexchange.server/lib/xmpcore.jar:/opt/open-xchange/bundles/com.openexchange.common/com.openexchange.common.jar:/opt/open-xchange/bundles/jcl-over-slf4j-1.7.5.jar:/opt/open-xchange/bundles/javax.servlet_2.5.0.v201103041518.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jaxb-api-2.2.2.jar:/opt/open-xchange/bundles/org.json.jar:/opt/open-xchange/bundles/com.openexchange.file.storage.composition.jar:/opt/open-xchange/bundles/com.openexchange.office.tools.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/jurt.jar:/opt/open-xchange/bundles/org.apache.tika/lib/tika-app-1.4.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/apache-mime4j-examples-0.7.2.jar:/opt/open-xchange/bundles/org.apache.felix.gogo.command_0.10.0.v201209301215.jar:/opt/open-xchange/bundles/com.openexchange.xml/lib/stax-1.2.0.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/activation-1.1.jar:/opt/open-xchange/bundles/com.openexchange.threadpool.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/commons-collections-3.2.1.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/jdo2-api-2.3-eb.jar:/opt/open-xchange/bundles/logback-classic-1.0.13.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/jsoup-1.7.3.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/juh.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/commons-fileupload-1.3.1.jar:/opt/open-xchange/bundles/com.openexchange.oauth.provider/com.openexchange.oauth.provider.jar:/opt/open-xchange/bundles/com.openexchange.office.calcengine.core.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/xml-apis-ext.jar:/opt/open-xchange/bundles/com.openexchange.tx.jar:/opt/open-xchange/bundles/com.openexchange.tokenlogin.jar:/opt/open-xchange/bundles/com.openexchange.oauth.provider/lib/oauth-googlecode.jar:/opt/open-xchange/bundles/javax.mail/lib/tnef-1.8.0.jar:/opt/open-xchange/bundles/geronimo-annotation_1.0_spec-1.1.1.jar:/opt/open-xchange/bundles/com.google.common/lib/guava-17.0.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/batik-codec.jar:/opt/open-xchange/bundles/com.openexchange.preview/lib/itextpdf-5.1.2.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jersey-core-1.17.1.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/xml-apis_ox.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/chardet-1.1.jar:/opt/open-xchange/bundles/com.openexchange.continuation.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/ical4j-zoneinfo-outlook-1.0.2.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jackson-mapper-asl-1.9.2.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/jsr305.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/htmlcleaner-2.9.jar:/opt/open-xchange/bundles/com.openexchange.global/lib/stringsearch-1.0.0.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/js.jar:/opt/open-xchange/bundles/com.openexchange.datatypes.genericonf.jar:/opt/open-xchange/bundles/com.openexchange.html/lib/commons-validator-1.4.0.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/mysql-connector-java-5.1.30-bin.jar:/opt/open-xchange/bundles/com.openexchange.secret.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/backport-util-concurrent-3.1.jar:/opt/open-xchange/bundles/com.hazelcast/com.hazelcast.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jersey-bundle-1.17.1.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jersey-client-1.17.1.jar:/opt/open-xchange/bundles/com.openexchange.file.storage.jar:/opt/open-xchange/bundles/com.openexchange.server/com.openexchange.server.jar:/opt/open-xchange/bundles/com.openexchange.realtime.jar:/opt/open-xchange/bundles/javax.mail/javax.mail.jar:/opt/open-xchange/bundles/com.openexchange.xml/lib/jdom-2.0.5.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/httpcore-nio-4.2.1.jar:/opt/open-xchange/bundles/com.openexchange.server/lib/jaudiotagger-2.0.3.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/java_uno.jar:/opt/open-xchange/bundles/com.openexchange.database.jar:/opt/open-xchange/bundles/com.openexchange.groupware.tools.jar:/opt/open-xchange/bundles/com.openexchange.preview/lib/juh-3.2.1.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/ical4j-1.0.4.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/apache-mime4j-storage-0.7.2.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/com.openexchange.documentconverter.external.jar:/opt/open-xchange/bundles/com.openexchange.html/com.openexchange.html.jar:/opt/open-xchange/bundles/com.openexchange.textxtraction/lib/pdfbox-1.7.0.jar:/opt/open-xchange/bundles/com.openexchange.documentation.jar:/opt/open-xchange/bundles/com.openexchange.global/com.openexchange.global.jar:/opt/open-xchange/bundles/logback-core-1.0.13.jar:/opt/open-xchange/bundles/com.openexchange.java.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/jboss-jms-api.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/batik-all.jar:/opt/open-xchange/bundles/com.openexchange.crypto/lib/PBKDF2-1.0.4.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/concurrentlinkedhashmap-lru-1.3.2.jar:/opt/open-xchange/bundles/com.openexchange.quota.jar:/opt/open-xchange/bundles/com.openexchange.monitoring.jar:/opt/open-xchange/bundles/com.openexchange.textxtraction/lib/poi-scratchpad-3.8.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/rabin-hash-function-2.0.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/httpmime-4.2.1.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/unoloader.jar:/opt/open-xchange/bundles/com.openexchange.version.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jettison-1.1.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jersey-server-1.17.1.jar:/opt/open-xchange/bundles/com.openexchange.system.extension.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/unoil.jar:/opt/open-xchange/bundles/com.openexchange.xerces.sun/lib/xml-apis-1.3.04.jar:/opt/open-xchange/bundles/org.apache.felix.gogo.runtime_0.10.0.v201209301036.jar:/opt/open-xchange/bundles/com.openexchange.textxtraction/lib/poi-contrib-3.6.jar:/opt/open-xchange/bundles/com.openexchange.caching/lib/concurrent-1.3.4.jar:/opt/open-xchange/bundles/org.apache.tika/org.apache.tika.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/concurrentlinkedhashmap-1.0.1-custom.jar:/opt/open-xchange/bundles/com.openexchange.textxtraction/com.openexchange.textxtraction.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/fluent-hc-4.2.1.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/commons-compress-1.3.jar:/opt/open-xchange/bundles/com.openexchange.server/lib/metadata-extractor-2.6.1.jar:/opt/open-xchange/bundles/com.openexchange.preview/lib/jodreports-2.4.0.jar:/opt/open-xchange/bundles/com.openexchange.sessionstorage.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/commons-httpclient-3.1.jar:/opt/open-xchange/bundles/org.apache.commons.net_3.2.0.v201305141515.jar:/opt/open-xchange/bundles/com.openexchange.xml/com.openexchange.xml.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/scribe-java-1.3.7.jar:/opt/open-xchange/bundles/com.openexchange.capabilities.jar:/opt/open-xchange/bundles/com.openexchange.proxy.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/xerces_ox.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/httpcore-ab-4.2.1.jar:/opt/open-xchange/bundles/slf4j-api-1.7.5.jar:/opt/open-xchange/bundles/org.apache.commons.codec_1.6.0.v201305230611.jar:/opt/open-xchange/bundles/com.openexchange.textxtraction/lib/dom4j-1.6.1.jar:/opt/open-xchange/bundles/com.openexchange.search.jar:/opt/open-xchange/bundles/com.openexchange.publish.jar:/opt/open-xchange/bundles/com.openexchange.sessiond.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/owasp-esapi-2.0.1.jar:/opt/open-xchange/bundles/commons-cli-1.2.jar:/opt/open-xchange/bundles/org.w3c.css.sac_1.3.1.v200903091627.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/ridl.jar:/opt/open-xchange/bundles/com.openexchange.server/lib/jacksum.jar:/opt/open-xchange/bundles/com.openexchange.xml/lib/spring-core_1.2.7.jar:/opt/open-xchange/bundles/com.openexchange.textxtraction/lib/poi-ooxml-3.8.jar:/opt/open-xchange/bundles/com.openexchange.preview/lib/ridl-3.2.1.jar:/opt/open-xchange/bundles/com.openexchange.freebusy.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/trove-3.0.3.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jsr311-api-1.1.1.jar:/opt/open-xchange/bundles/com.openexchange.html/lib/jTidy.jar:/opt/open-xchange/bundles/com.openexchange.config.cascade.jar:/opt/open-xchange/bundles/com.openexchange.preview/lib/jurt-3.2.1.jar:/opt/open-xchange/bundles/com.openexchange.documentconverter.external/lib/xalan-2.6.0.jar:/opt/open-xchange/bundles/org.xmlpull/lib/kxml2-2.3.0.jar:/opt/open-xchange/bundles/com.openexchange.preview/lib/jOpenDocument-1.2.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/nekohtml-1.9.18.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/asm-3.1.jar:/opt/open-xchange/bundles/com.openexchange.server/lib/java-image-scaling-0.8.5.jar:/opt/open-xchange/bundles/com.openexchange.configread.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jersey-servlet-1.17.1.jar:/opt/open-xchange/bundles/com.openexchange.management.impl.jar:/opt/open-xchange/bundles/com.openexchange.crypto/com.openexchange.crypto.jar:/opt/open-xchange/bundles/com.openexchange.management.jar:/opt/open-xchange/bundles/com.openexchange.caching.events.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/brownies-collections-0.9.6.jar:/opt/open-xchange/bundles/com.openexchange.jslob.jar:/opt/open-xchange/bundles/com.openexchange.conversion.simple.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jackson-jaxrs-1.9.2.jar:/opt/open-xchange/bundles/org.ho.jyaml/lib/jyaml-1.3.jar:/opt/open-xchange/bundles/com.openexchange.caching/com.openexchange.caching.jar:/opt/open-xchange/bundles/com.openexchange.xml/lib/spring-beans_1.2.7.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jaxb-impl-2.2.3-1.jar:/opt/open-xchange/bundles/com.hazelcast/lib/hazelcast-3.2.4.jar:/opt/open-xchange/bundles/com.openexchange.xerces.sun/lib/xerces.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/stax-api-1.0-2.jar:/opt/open-xchange/bundles/com.openexchange.preview/com.openexchange.preview.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/cssparser-0.9.7.jar:/opt/open-xchange/bundles/log4j-over-slf4j-1.7.5.jar:/opt/open-xchange/bundles/com.openexchange.annotation.jar:/opt/open-xchange/bundles/com.openexchange.preview/lib/unoil-3.2.1.jar:/opt/open-xchange/bundles/com.openexchange.preview/lib/jodconverter-core-3.0-beta-4.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jackson-core-asl-1.9.2.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/lib/jackson-xc-1.9.2.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/libidn-1.9.jar:/opt/open-xchange/bundles/com.openexchange.textxtraction/lib/poi-3.8.jar:/opt/open-xchange/bundles/com.openexchange.global/lib/stringsearch-2.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/gnu-rex.jar:/opt/open-xchange/bundles/osgi-over-slf4j-1.7.5.jar:/opt/open-xchange/bundles/com.openexchange.http.jersey/com.openexchange.http.jersey.jar:/opt/open-xchange/bundles/org.eclipse.osgi.services_3.3.100.v20130513-1956.jar:/opt/open-xchange/bundles/com.openexchange.caching/lib/jcs-1.3-custom.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/httpclient-cache-4.2.1.jar:/opt/open-xchange/lib/com.openexchange.office.calcengine.worker.jar"

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

ADMINCP=$(for jar in $LIBPATH/*.jar; do
    echo -n "${jar}:"
    done)

# 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 -classpath $ADMINCP:$CLASSPATH com.openexchange.office.calcengine.worker.WorkerMain $ARG_JAR_MAIN
exit $?
