#!/bin/bash
#
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016 Synacor, Inc.
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software Foundation,
# version 2 of the License.
#
# 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, see <https://www.gnu.org/licenses/>.
# ***** END LICENSE BLOCK *****
#
#
# We cannot rely on any config parameters when running zmlocalconfig for obvious
# chicken and egg reasons. So we just make assumptions about install layout.
#

ZMROOT=`dirname $0`/..
ZMROOT=`(cd "${ZMROOT}"; pwd)`

umask 0027

if [ -x "${ZMROOT}/common/bin/java" ]; then
    java="${ZMROOT}/common/bin/java"
else
    java=java
fi

if [ -f ".hotspot_compiler" ]; then
  cd $ZMROOT
fi

case "`uname`" in
CYGWIN*) PATHSEP=";";;
*) PATHSEP=":";;
esac

CP=${ZMROOT}/lib/jars/zimbracommon.jar\
${PATHSEP}${ZMROOT}/lib/jars/zimbrasoap.jar\
${PATHSEP}${ZMROOT}/lib/jars/zimbraclient.jar\
${PATHSEP}${ZMROOT}/lib/jars/zimbrastore.jar\
${PATHSEP}${ZMROOT}/lib/jars/zimbra-native.jar\
${PATHSEP}${ZMROOT}/lib/jars/apache-log4j-extras-1.0.jar\
${PATHSEP}${ZMROOT}/lib/jars/commons-logging.jar\
${PATHSEP}${ZMROOT}/lib/jars/commons-cli-1.2.jar\
${PATHSEP}${ZMROOT}/lib/jars/commons-httpclient-3.1.jar\
${PATHSEP}${ZMROOT}/lib/jars/commons-codec-1.7.jar\
${PATHSEP}${ZMROOT}/lib/jars/guava-23.0.jar\
${PATHSEP}${ZMROOT}/lib/jars/httpclient-4.5.2.jar\
${PATHSEP}${ZMROOT}/lib/jars/httpcore-4.4.5.jar\
${PATHSEP}${ZMROOT}/lib/jars/dom4j-1.5.2.jar\
${PATHSEP}${ZMROOT}/lib/jars/log4j-1.2.16.jar\
${PATHSEP}${ZMROOT}/lib/jars/json.jar\
${PATHSEP}${ZMROOT}/lib/jars/activation-1.1.1.jar\
${PATHSEP}${ZMROOT}/lib/jars/jaxb-api-2.3.1.jar\
${PATHSEP}${ZMROOT}/lib/jars/jaxb-impl-2.3.1.jar\
${PATHSEP}${ZMROOT}/lib/jars/istack-commons-runtime-3.0.8.jar

if [ -f "${ZMROOT}/lib/ext/backup/zimbrabackup.jar" ]; then
    CP="$CP${PATHSEP}${ZMROOT}/lib/ext/backup/zimbrabackup.jar"
fi

if [ -f "${ZMROOT}/lib/ext/voice/zimbravoice.jar" ]; then
    CP="$CP${PATHSEP}${ZMROOT}/lib/ext/voice/zimbravoice.jar"
fi

if [ -f "${ZMROOT}/lib/ext/com_zimbra_oo/com_zimbra_oo.jar" ]; then
    CP="$CP${PATHSEP}${ZMROOT}/lib/ext/com_zimbra_oo/com_zimbra_oo.jar"
fi

java_version=$(${java} -version 2>&1 | grep "java version" | sed -e 's/"//g' | awk '{print $NF}' | awk -F_ '{print $1}')
if [ x"$java_version" = "x1.6.0" ]; then
  java_options="-XX:ErrorFile=${ZMROOT}/log"
else
  java_options=""
fi

exec ${java} ${java_options} -client -cp "$CP" \
    -Djava.library.path=${ZMROOT}/lib -Dzimbra.home="${ZMROOT}" \
    com.zimbra.cs.localconfig.LocalConfigCLI "$@"
