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

say() {
    echo " * $1"
}

# Get hostname & IPs
HOSTNAME_SHORT=$(hostname -s)

# Detect Env
YANDEX_ENV_NAME="localhost"
YANDEX_ENV_TYPE=$(cat /etc/yandex/environment.type)
if [ -f "/etc/yandex/environment.name" ]; then
        YANDEX_ENV_NAME=$(cat /etc/yandex/environment.name)
fi
YANDEX_ENV="${YANDEX_ENV_NAME}/${YANDEX_ENV_TYPE}"

# Kolmogor
KOLMOGOR_CONFIG_NAME=/etc/kolmogor/kolmogor.conf
KOLMOGOR_CONFIG_PATH=/etc/kolmogor

case "$1" in
    configure)
        # Override env for pass-shg-* (PASSPADMIN-2539)
        case "${HOSTNAME_SHORT}" in
            pass-shg-*)
                YANDEX_ENV_NAME="localhost"
                YANDEX_ENV_TYPE="stress"
                ;;
        esac

        if [ -f ${KOLMOGOR_CONFIG_PATH}/kolmogor_${YANDEX_ENV_NAME}_${YANDEX_ENV_TYPE}.conf ]; then
            say "Install Kolmogor config for: ${YANDEX_ENV}"
            
            ln -sf ${KOLMOGOR_CONFIG_PATH}/kolmogor_${YANDEX_ENV_NAME}_${YANDEX_ENV_TYPE}.conf ${KOLMOGOR_CONFIG_NAME}
        else
            say "Unsupported environment ${YANDEX_ENV}, do nothing"
        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

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
