#!/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)

    if [ -z "${2}" ]; then
        yav-deploy --force --skip-post-update \
            -c /etc/yandex/yav-deploy/pkg/yandex-passport-push-subscription/ \
            || true
    else
        yav-deploy --force \
            -c /etc/yandex/yav-deploy/pkg/yandex-passport-push-subscription/ \
            || true
    fi

    CURRENT_ENV=`cat "/etc/yandex/environment.type"`

    ## common
    chown -R www-data: /etc/yandex/passport-push-subscription
    chown -R www-data: /var/log/yandex/passport-push-subscription

    sudo setcap CAP_NET_BIND_SERVICE=+eip /usr/sbin/push_subscription

    ## pushclient
    if [[ "$CURRENT_ENV" != "development" ]]; then
        echo "Generating pushclient config."
        python3 /etc/yandex/passport-push-subscription/prepare_pushclient_passp.py \
            "/etc/yandex/passport-push-subscription/template_pushclient_passp.yaml" \
            "$CURRENT_ENV" > /etc/yandex/statbox-push-client/passp.yaml
        python3 /etc/yandex/passport-push-subscription/prepare_pushclient_push.py \
            "/etc/yandex/passport-push-subscription/template_pushclient_push.yaml" \
            "$CURRENT_ENV" > /etc/yandex/statbox-push-client/push.yaml
        echo '
# This file generated by yandex-passport-push-subscription
DAEMON_CONF="/etc/yandex/statbox-push-client/passp.yaml /etc/yandex/statbox-push-client/push.yaml "
' > /etc/default/push-client

        echo "Restarting pushclient."
        service statbox-push-client restart
    fi

    ## cfg
    echo "Generating config."
    python3 /etc/yandex/passport-push-subscription/prepare.py \
        "/etc/yandex/passport-push-subscription/template.json" \
        "$CURRENT_ENV" > /etc/yandex/passport-push-subscription/push_subscription.conf

    ## yasm
    echo "Restarting yasmagent."
    YENV_PROJECT="passport.push_subscription" /usr/share/passport-admin-config-golovan/passport_golovan_config.py \
        --itype="passportpush_subscription" > \
        /etc/yandex/yasmagent/instances.d/passport.push_subscription.instance
    YENV_PROJECT="passport.push_subscription" /usr/share/passport-admin-config-golovan/passport_golovan_config.py \
        --itype="common" >> \
        /etc/yandex/yasmagent/instances.d/passport.push_subscription.instance
    invoke-rc.d yasmagent restart || /bin/true

    ## push_subscription
    echo "Updating sysmtemctl."
    if ! systemctl is-enabled yandex-passport-push-subscription >/dev/null; then
        systemctl enable yandex-passport-push-subscription || true
    fi
    systemctl --system daemon-reload || true

    echo "Trying to check logstoreagent configs..."
    sudo -u www-data /usr/sbin/logstoreagent_config_checker
    echo "Trying to check logstoreagent configs...OK"
    service yandex-passport-logstoreagent restart

    echo "Restarting service"
    service yandex-passport-push-subscription restart

    ;;

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

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

exit 0
