#!/bin/sh

case "$1" in
    configure)
        adduser --system zookeeper
        chown -R zookeeper /var/lib/yandex/zookeeper-jkp
        chown -R zookeeper /var/log/yandex/zookeeper-jkp


        ENV=`cat /etc/yandex/environment.type`
        ENV_CONFIG=/etc/yandex/zookeeper-jkp/zoo.cfg.$ENV

        if ! test -f $ENV_CONFIG ; then
            echo "FAIL: no zk config $ENV_CONFIG for current environment!"
            exit 1
        fi

        if ! grep "^server.[0-9]*=`hostname -f`:[0-9]*:[0-9]*$" $ENV_CONFIG ; then
            echo "FAIL: current host \"`hostname -f`\" is missing in $ENV_CONFIG!"
            exit 1
        fi

        # TODO check conductor group, too

        echo "Using zoo.cfg.$ENV ..."
        if [ -f /etc/yandex/zookeeper-jkp/zoo.cfg ]; then
            rm /etc/yandex/zookeeper-jkp/zoo.cfg
        fi
        ln -s $ENV_CONFIG /etc/yandex/zookeeper-jkp/zoo.cfg

        ENV_REST_CONFIG=/etc/yandex/zookeeper-jkp/rest.cfg.$ENV
        if test -f $ENV_REST_CONFIG ; then
            if [ -f /etc/yandex/zookeeper-jkp/rest.cfg ]; then
                rm /etc/yandex/zookeeper-jkp/rest.cfg
            fi
            ln -s $ENV_REST_CONFIG /etc/yandex/zookeeper-jkp/rest.properties
        fi

        SLNG_CONFIG="/etc/syslog-ng/conf-enabled/yandex-zookeeper-jkp.conf"
        ENV_SLNG_CONFIG="/etc/syslog-ng/conf-available/yandex-zookeeper-jkp-$ENV.conf"
        if test -f $ENV_SLNG_CONFIG; then
            if [ -f $SLNG_CONFIG ]; then
                rm $SLNG_CONFIG
            fi
            ln -s $ENV_SLNG_CONFIG $SLNG_CONFIG
        fi
    ;;
    
    abort-upgrade|abort-remove|abort-deconfigure)
    ;;
    
    *)
        echo "postinst called with unknown argument $1" >&2
        exit 1
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

# vim: set ts=4 sw=4 et:
