#!/bin/sh
# postinst script
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

# No spaces allowed in name
_name="challenge-pushes"
_name_merged="challengepushes"

case "$1" in
    configure)

    chown -R www-data:www-data "/etc/yandex/logbroker-client/$_name/"
    chown -R www-data:www-data "/var/log/yandex/logbroker-client/$_name/"

    echo "do not delete" > "/etc/yandex/logbroker-client/$_name/logrotate.ping"
    chmod 0644 "/etc/yandex/logbroker-client/$_name/logrotate.ping" || /bin/true

    /usr/share/passport-admin-config-golovan/passport_golovan_config.py -i "passportlbc$_name_merged" -p "logbroker-client-$_name" > "/etc/yandex/yasmagent/instances.d/lbc-$_name"
    invoke-rc.d yasmagent restart

    if [ -x /bin/systemctl ]; then
        if ! systemctl is-enabled yandex-passport-xunistater@lbc-$_name > /dev/null; then
            systemctl enable yandex-passport-xunistater@lbc-$_name || true
        fi
        deb-systemd-invoke restart yandex-passport-xunistater@lbc-$_name || true
    else
        update-rc.d yandex-passport-xunistater defaults || true
        invoke-rc.d yandex-passport-xunistater restart || true
    fi

    ENV_TYPE=$(cat /etc/yandex/environment.type)

    PUSH_CLIENT_TPL="/etc/yandex/statbox-push-client/templates/push-client-passport-push.yaml.${ENV_TYPE}"
    PUSH_CLIENT_CONFIG="/etc/yandex/statbox-push-client/push-client-passport-push.yaml"
    PUSH_CLIENT_STATE="/var/lib/passport-push"
    PUSH_CLIENT_LOGDIR="/var/log/statbox/"
    PUSH_CLIENT_LOG="${PUSH_CLIENT_LOGDIR}/passport-push.log"

    mkdir -p "${PUSH_CLIENT_LOGDIR}"
    touch "${PUSH_CLIENT_LOG}"
    chown statbox:statbox "${PUSH_CLIENT_LOG}"
    if [ ! -d "${PUSH_CLIENT_STATE}" ]; then
        mkdir "${PUSH_CLIENT_STATE}"
        chown statbox:statbox "${PUSH_CLIENT_STATE}"
    fi
    echo "Install statbox template ${PUSH_CLIENT_TPL} > ${PUSH_CLIENT_CONFIG}"
    cp "${PUSH_CLIENT_TPL}" "${PUSH_CLIENT_CONFIG}"

    if [ ! -e "/etc/default/push-client" ]; then
        echo 'DAEMON_CONF="/etc/yandex/statbox-push-client/push-client*.yaml"' > /etc/default/push-client
    fi

    if [ -x "/etc/init.d/statbox-push-client" ]; then
        invoke-rc.d statbox-push-client restart
    fi

    if [ -x /bin/systemctl ]; then
        systemctl enable yandex-passport-logbroker-client-$_name
    fi
    service yandex-passport-logbroker-client-$_name restart || /bin/true

    ;;

    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#

exit 0
