#!/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-2012 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=/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

BNAME=$(basename $0)
ACTION=
case "${BNAME}" in
    "changecontext")
    ACTION="context.Change"
    ;;
    "createcontext")
    ACTION="context.Create"
    ;;
    "deletecontext")
    ACTION="context.Delete"
    ;;
    "getmoduleaccessforcontext")
    ACTION="context.GetModuleAccess"
    ;;
    "getaccesscombinationnameforcontext")
    ACTION="context.GetAccessCombinationName"
    ;;
    "disablecontext")
    ACTION="context.Disable"
    ;;
    "disableallcontext")
    ACTION="context.DisableAll"
    ;;
    "enablecontext")
    ACTION="context.Enable"
    ;;
    "enableallcontext")
    ACTION="context.EnableAll"
    ;;
    "deleteinvisible")
    ACTION="context.DeleteInvisible"
    ;;
    "movecontextdatabase")
    ACTION="context.MoveContextDatabase"
    ;;
    "movecontextfilestore")
    ACTION="context.MoveContextFilestore"
    ;;
    "listcontext")
    ACTION="context.List"
    ;;
    "listcontextsbydatabase")
    ACTION="context.ListContextByDatabase"
    ;;
    "listcontextsbyfilestore")
    ACTION="context.ListContextByFilestore"
    ;;
    "changefilestore")
    ACTION="util.filestore.ChangeFilestore"
    ;;
    "listdatabase")
    ACTION="util.database.ListDatabase"
    ;;
    "listfilestore")
    ACTION="util.filestore.ListFilestore"
    ;;
    "listserver")
    ACTION="util.server.ListServer"
    ;;
    "registerdatabase")
    ACTION="util.database.RegisterDatabase"
    ;;
    "registerfilestore")
    ACTION="util.filestore.RegisterFilestore"
    ;;
    "registerserver")
    ACTION="util.server.RegisterServer"
    ;;
    "unregisterdatabase")
    ACTION="util.database.UnregisterDatabase"
    ;;
    "unregisterfilestore")
    ACTION="util.filestore.UnregisterFilestore"
    ;;
    "unregisterserver")
    ACTION="util.server.UnregisterServer"
    ;;
    "changedatabase")
    ACTION="util.database.ChangeDatabase"
    ;;
    "getschemaname")
    ACTION="context.GetSchemaName"
    ;;
    "getadminid")
    ACTION="context.GetAdminId"
    ;;
    "getcontextcapabilities")
    ACTION="context.GetContextCapabilities"
    ;;
    "existscontext")
    ACTION="context.Exists"
    ;;
esac

JAVAMAIN="com.openexchange.admin.console.${ACTION}"

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

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

CLASSPATH="-classpath /opt/open-xchange/bundles/com.openexchange.contactcollector.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/jakarta-slide-webdavlib-2.1.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.osgi.jar:/opt/open-xchange/bundles/javax.mail/lib/libidn-1.9.jar:/opt/open-xchange/bundles/org.apache.commons.io_2.0.1.v201105210651.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.sql.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/org.apache.commons.lang_2.6.0.v201205030909.jar:/opt/open-xchange/bundles/com.openexchange.messaging.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/apache-mime4j-core-0.7.2.jar:/opt/open-xchange/bundles/com.openexchange.conversion.jar:/opt/open-xchange/bundles/com.openexchange.dataretention.jar:/opt/open-xchange/bundles/com.openexchange.xml/lib/joox-1.2.0.jar:/opt/open-xchange/bundles/com.openexchange.xml/lib/org.dynamicjava.stax-api-1.0.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/javax.servlet_2.5.0.v201103041518.jar:/opt/open-xchange/bundles/org.json.jar:/opt/open-xchange/bundles/jcl-over-slf4j-1.7.5.jar:/opt/open-xchange/bundles/com.openexchange.file.storage.composition.jar:/opt/open-xchange/bundles/net.fortuna.ical4j/net.fortuna.ical4j.jar:/opt/open-xchange/bundles/org.apache.tika/lib/tika-app-1.4.jar:/opt/open-xchange/bundles/com.openexchange.file.storage.infostore.jar:/opt/open-xchange/bundles/com.openexchange.server/lib/jaudiotagger-2.2.2.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.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/javax.mail/lib/EmailAddress.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.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.preview/lib/itextpdf-5.1.2.jar:/opt/open-xchange/bundles/com.openexchange.admin.rmi.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/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.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.openexchange.file.storage.jar:/opt/open-xchange/bundles/com.openexchange.server/com.openexchange.server.jar:/opt/open-xchange/bundles/javax.mail/javax.mail.jar:/opt/open-xchange/bundles/com.openexchange.datatypes.genericonf.storage.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.admin.plugin.hosting.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/apache-mime4j-storage-0.7.2.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.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/net.fortuna.ical4j/lib/ical4j-zoneinfo-outlook-1.0.2.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.version.jar:/opt/open-xchange/bundles/net.fortuna.ical4j/lib/commons-codec-1.8.jar:/opt/open-xchange/bundles/com.openexchange.system.extension.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.common/lib/commons-httpclient-3.1.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/ical4j-1.0.6.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.admin.console.common/lib/opencsv-2.0.jar:/opt/open-xchange/bundles/com.openexchange.proxy.jar:/opt/open-xchange/bundles/com.openexchange.capabilities.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/scribe-java-1.3.7.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.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.server/lib/jacksum.jar:/opt/open-xchange/bundles/com.openexchange.common/lib/jsoup-1.8.1.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.admin.console.common/com.openexchange.admin.console.common.jar:/opt/open-xchange/bundles/com.openexchange.admin.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/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.server/lib/imgscalr-lib-4.2.jar:/opt/open-xchange/bundles/com.openexchange.publish.basic.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.management.impl.jar:/opt/open-xchange/bundles/com.openexchange.crypto/com.openexchange.crypto.jar:/opt/open-xchange/bundles/com.openexchange.caching.events.jar:/opt/open-xchange/bundles/com.openexchange.management.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/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.xerces.sun/lib/xerces.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/net.fortuna.ical4j/lib/commons-logging-1.1.3.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/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.admin.console.jar"

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

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

exec $JAVA_BIN $JAVA_OPTS $CLASSPATH:$ADMINCP $JAVAMAIN "$@"
