#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2008, 2009, 2010, 2013, 2014, 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 *****
# 

source `dirname $0`/zmshutil || exit 1
zmsetvars

u=`id -un`
if [ $u != ${zimbra_user} ]; then
    echo "Error: must be run as ${zimbra_user} user"
    exit 1
fi

isrunning() {
    zmmailboxdctl status 2> /dev/null
    return
}

opth=0
for opt in $@; do 
    if [ $opt = "-h" -o $opt = "--help" ]; then
        opth=1
        break
    fi
done

ropid=${zimbra_log_directory}/zmplayredo.pid

if [ $opth -eq 0 ]; then
    if isrunning; then
        echo "Error: mailboxd still running.  Stop mailboxd before running zmplayredo."
        exit 1
    fi
    if [ -s $ropid ]; then
        echo "Error: another instance of zmplayredo (pid=`cat $ropid`) is already running"
        echo "       remove $ropid if this is not the case"
        exit 1
    fi
fi

if [ -d ${zimbra_java_home}/jre ]; then
    JRE_EXT_DIR=${zimbra_java_home}/jre/lib/ext
else
    JRE_EXT_DIR=/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/lib/ext
    #JRE_EXT_DIR=${zimbra_java_home}/lib/ext
fi

jardirs=${JRE_EXT_DIR}:/opt/zimbra/mailboxd/lib/*:/opt/zimbra/mailboxd/common/lib/*:/opt/zimbra/lib/jars/*
if [ -e /opt/zimbra/lib/ext-common ]; then
    jardirs=${jardirs}:/opt/zimbra/lib/ext-common/*
fi
for jd in `find /opt/zimbra/lib/ext -type d` ; do
    if [ ${jd} != /opt/zimbra/lib/ext ]; then
        jardirs="${jardirs}:${jd}/*"
    fi
done

jardirs="${jardirs}:/opt/zimbra/jetty/common/lib/*:/opt/zimbra/jetty/webapps/service/WEB-INF/lib/*"
#
# Memory for use by JVM
#
jm=${mailboxd_java_heap_size}
if [ -n "$jm" ] ; then
    xms_xmx_options="-Xms${jm}m -Xmx${jm}m"
fi

java_version=$(${zimbra_java_home}/bin/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=/opt/zimbra/log"
else
  java_options=""
fi

# Remove "-verbose:gc" and all options that start with "-XX:+PrintGC".  These pollute stdout/stderr.
for opt in $mailboxd_java_options ; do
    if [ "$opt" = "-verbose:gc" ] || [[ "$opt" == "-XX:+PrintGC"* ]] ; then
        continue
    fi
    if [ -z "$sanitized_options" ] ; then
      sanitized_options="$opt"
    else
      sanitized_options="$sanitized_options $opt"
    fi
done

echo $$ > $ropid
${zimbra_java_home}/bin/java \
    ${java_options} \
    ${xms_xmx_options} ${sanitized_options} \
    -Dzimbra.config=/opt/zimbra/conf/localconfig.xml \
    -Djava.library.path=/opt/zimbra/lib \
    -classpath ${jardirs} \
    com.zimbra.cs.redolog.util.PlaybackUtil "$@"
rc=$?
rm -f $ropid
exit $rc
