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

# snmp_notify 1|0
# snmp_trap_host <hostname|ip>
# smtp_notify 1|0
# smtp_source <email address>
# smtp_destination <email address>

source /opt/zimbra/bin/zmshutil || exit 1
zmsetvars

if [ ${snmp_notify} = "1" -o x${snmp_notify} = x"yes" ]; then
	SOURCEFILE=/opt/zimbra/conf/snmpd.conf.in
	FILE=/opt/zimbra/conf/snmpd.conf
	echo creating $FILE
	cat ${SOURCEFILE} | sed -e "s/@@SNMPHOST@@/${snmp_trap_host}/g" > ${FILE}
fi

# This next block is completely wrong.  The values are single quoted, so do not need the slash
# In fact, the slash generates broken email headers

# Many backslashes to make sure '@' gets quoted in final perl
#
#if [ ${smtp_notify} = "1" -o x${smtp_notify} = x"yes" ]; then
#	smtp_source=`echo ${smtp_source} | sed -e 's/@/\\\\\\\@/'`
#	smtp_destination=`echo ${smtp_destination} | sed -e 's/@/\\\\\\\@/'`
#fi

SWFILE=/opt/zimbra/conf/swatchrc

echo creating $SWFILE
cat ${SWFILE}.in | \
	sed -e "s/@@DOSNMPNOTIFICATIONS@@/${snmp_notify}/g" \
	-e "s/@@DOSMTPNOTIFICATIONS@@/${smtp_notify}/g" \
	-e "s/@@ADMINEMAIL@@/${smtp_source}/g" \
	-e "s/@@PHCEMAIL@@/${smtp_destination}/g" \
	-e "s/@@HOSTNAME@@/${zimbra_server_hostname}/g" \
	-e "s/@@TRAPHOST@@/${snmp_trap_host}/g" \
	> ${SWFILE}
