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

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 "$@"
