#!/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: /var/log/yandex/passport-sso-frontend
    chown -R www-data: /usr/lib/yandex/passport-sso-frontend

    # pick a config
    #rm /usr/lib/yandex/passport-sso-frontend/configs/current
    if [ -f /etc/yandex/environment.name ] && [ `cat /etc/yandex/environment.name` != 'intranet' ]
    then
        if [ `cat /etc/yandex/environment.type` = 'development' ]
        then
            ln -fs /usr/lib/yandex/passport-sso-frontend/configs/testing /usr/lib/yandex/passport-sso-frontend/configs/current
        else
            ln -fs /usr/lib/yandex/passport-sso-frontend/configs/`cat /etc/yandex/environment.type` /usr/lib/yandex/passport-sso-frontend/configs/current
        fi
    else
        ln -fs /usr/lib/yandex/passport-sso-frontend/configs/testing /usr/lib/yandex/passport-sso-frontend/configs/current
    fi

    YA_ENV_TYPE=`cat /etc/yandex/environment.type`
    # if deb is installed by admin
    yav-deploy -c /etc/yandex/yav-deploy/pkg/yandex-passport-sso-frontend/ || /bin/true
    # if deb is installed by conductor
    if [ "$YA_ENV_TYPE" = production ] || [ "$YA_ENV_TYPE" = rc ]; then
        ROBOT_LOGIN=robot-passport-front
    else
        ROBOT_LOGIN=robot-passport-front
    fi
    yav-deploy --debug --rsa-login ${ROBOT_LOGIN} --rsa-private-key /usr/lib/yandex/passport-frontend/robot-passport-front-private-key -c /etc/yandex/yav-deploy/pkg/yandex-passport-sso-frontend/ || /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
