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

    yav-deploy --force --skip-post-update \
        -c /etc/yandex/yav-deploy/pkg/yandex-passport-historydb-api2/ ||
        true

    CURRENT_ENV=$(cat "/etc/yandex/environment.type")

    ## common
    chown -R www-data: /etc/yandex/passport-historydb-api2
    chown -R www-data: /var/log/yandex/passport-historydb-api2

    if [[ -d /run/systemd/system ]]; then
        systemctl daemon-reload

        if ! systemctl is-enabled yandex-passport-historydb-api2 >/dev/null; then
            systemctl enable yandex-passport-historydb-api2
        fi
    fi

    ## nginx
    echo "Reloading nginx."
    if [ "${CURRENT_ENV}" = "production" ]; then
        ln -s -f /etc/nginx/sites-available/historydb-api.passport.yandex.net.conf          /etc/nginx/sites-enabled/historydb-api.passport.yandex.net.conf
        ln -s -f /etc/nginx/sites-available/historydb-api-internal.passport.yandex.net.conf /etc/nginx/sites-enabled/historydb-api-internal.passport.yandex.net.conf
    elif [ "${CURRENT_ENV}" = "testing" ]; then
        ln -s -f /etc/nginx/sites-available/kvs-loader-test-m1.passport.yandex.net.conf /etc/nginx/sites-enabled/historydb-api.passport.yandex.net.conf
    else
        rm -f /etc/nginx/sites-enabled/historydb-api.passport.yandex.net.conf
    fi
    invoke-rc.d nginx reload || /bin/true

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

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

    ## build config
    echo "Building config."
    python /etc/yandex/passport-historydb-api2/prepare_config.py \
        "/etc/yandex/passport-historydb-api2/template-config.json" \
        "$CURRENT_ENV" \
        "$(hostname -f)" > /etc/yandex/passport-historydb-api2/config.json

    #echo "Restarting historydb_api2."
    #if [[ -d /run/systemd/system ]]; then
    #    sudo systemctl restart yandex-passport-historydb-api2 || true
    #else
    #    sudo /etc/init.d/yandex-passport-historydb-api2 restart || true
    #fi

    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

    ;;

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

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

exit 0
