#!/bin/sh
# postinst script for test-0.1
#
# 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


case "$1" in
    configure)

    chown -R www-data:www-data /var/log/yandex/passport-vault
    chown -R www-data:www-data /usr/lib/yandex/passport-vault
    chown -R www-data:www-data /var/run/yandex/passport-vault

    if [ -x /usr/bin/passport-golovan-config ]; then
        /usr/bin/passport-golovan-config || /bin/true
    fi

    YENV_TYPE=`/usr/lib/yandex/passport-vault/vault_yenv`
    LOGBROKER_CONFIG="/etc/yandex/statbox-push-client/yandex-passport-vault-push-client-${YENV_TYPE}.yaml"
    if [ -f $LOGBROKER_CONFIG ]; then
        ln -sf $LOGBROKER_CONFIG "/etc/yandex/statbox-push-client/yandex-passport-vault.yaml"
        service statbox-push-client restart || /bin/true
    fi

    /usr/sbin/regenerate-monrun-tasks
    invoke-rc.d juggler-client restart

    /usr/share/passport-admin-config-golovan/passport_golovan_config.py -i passportvaultapi -p "vault-api" > /etc/yandex/yasmagent/instances.d/vault-api-xunistater
    invoke-rc.d yasmagent restart

    if ! systemctl is-enabled yandex-passport-xunistater@passport-vault-api > /dev/null; then
        systemctl enable yandex-passport-xunistater@passport-vault-api || true
    fi
    deb-systemd-invoke restart yandex-passport-xunistater@passport-vault-api || true

    systemctl enable yandex-passport-vault

    ;;

    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
