#!/bin/sh

set -e

U_NAME='hbf'
U_ID='1047'
U_HOME='/var/run/yandex-hbf-agent'

case "$1" in
    configure)

        if ! getent group ${U_NAME} > /dev/null; then
            groupadd -g ${U_ID} ${U_NAME}
        else
            echo "Group '${U_NAME}' already exists."
        fi

        if ! getent passwd ${U_NAME} > /dev/null; then
            useradd --system --uid ${U_ID} --gid ${U_NAME} --shell /bin/false \
                    --home-dir ${U_HOME} ${U_NAME}
        else
            echo "Group '${U_NAME}' already exists."
        fi

        install -d -m755 -o ${U_NAME} -g ${U_NAME} /var/log/yandex-hbf-agent
        install -d -o ${U_NAME} -g ${U_NAME} /var/run/yandex-hbf-agent
        for f in \
            '/var/log/yandex-hbf-agent/Y_FW_drop.log' \
            '/var/log/yandex-hbf-agent/Y_FW_OUT_drop.log' \
            '/var/log/yandex-hbf-agent/Y_END_OUT_INET_drop.log' \
            '/var/log/yandex-hbf-agent/PREROUTING_MTN_NAT.log' \
            '/var/log/yandex-hbf-agent/OUTPUT_MTN_NAT.log' \
        ;do

            touch ${f}
            chmod 644 ${f}
            chown syslog:adm ${f}
        done
        service rsyslog restart || :
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

exit 0
