#!/bin/sh -e

# If /tmp/ZABBIX_PACKAGE_DEBUG file exists then enable debugging of this script.
#if [ -e /tmp/ZABBIX_PACKAGE_DEBUG ]; then
#    set -x
#fi

CONFFILE1="/usr/share/zabbix-agent/zabbix_agentd.conf"
CONFFILE_DEST1="/etc/zabbix/zabbix_agentd.conf"

. /usr/share/debconf/confmodule
db_version 2.0 || [ $? -lt 30 ]

if [ "$1" = "configure" ]; then

    RET=""
    db_get zabbix-agent/server || true
    zabbix_server="$RET"
    if [ "$zabbix_server" = "" ]; then
        zabbix_server="127.0.0.1"
    fi

    sed "s/^Server=.*/Server=$zabbix_server/g" < $CONFFILE1 > $CONFFILE_DEST1.new 
    ucf --debconf-ok $CONFFILE_DEST1.new $CONFFILE_DEST1
    rm -f $CONFFILE_DEST1.new

    if ! getent group zabbix > /dev/null 2>&1 ; then
        addgroup --system --quiet zabbix
    fi

    if ! getent passwd zabbix > /dev/null 2>&1 ; then
        adduser --quiet \
            --system --disabled-login --ingroup zabbix \
            --home /var/lib/zabbix/ --no-create-home \
            zabbix
    fi

    chown zabbix:zabbix /var/log/zabbix -R
fi

db_stop

# Automatically added by dh_installinit
# In case this system is running systemd, we need to ensure that all
# necessary tmpfiles (if any) are created before starting.
if [ -d /run/systemd/system ] ; then
	systemd-tmpfiles --create /usr/lib/tmpfiles.d/zabbix-agent.conf >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installinit
if [ -x "/etc/init.d/zabbix-agent" ] || [ -e "/etc/init/zabbix-agent.conf" ]; then
	if [ ! -e "/etc/init/zabbix-agent.conf" ]; then
		update-rc.d zabbix-agent defaults >/dev/null
	fi
	invoke-rc.d zabbix-agent start || exit $?
fi
# End automatically added section


exit 0
