#!/bin/bash
# 
# ***** BEGIN LICENSE BLOCK *****
# Zimbra Collaboration Suite Server
# Copyright (C) 2010, 2011, 2012, 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 /opt/zimbra/bin/zmshutil || exit 1
zmsetvars

NC=`which nc 2>/dev/null`; NC=${NC:-`which netcat 2>/dev/null`}

echo REWRITE $@ | $NC -w ${zimbra_configrewrite_timeout} localhost ${zmconfigd_listen_port} > /dev/null 2>&1
rc=$?

# If nc fails to connect, run zmconfigd directly
if [ $rc -ne 0 ]; then
	/opt/zimbra/libexec/zmconfigd $@
	rc=$?
	stty echo
else
	sleep 5
fi
exit $rc
