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

ln -fs /usr/lib/yandex/oauth-frontend/systemd/oauth-passport-frontend.service /lib/systemd/system/yandex-oauth-frontend.service

case "$1" in
    configure)

    if [ -d /run/systemd/system ]; then
	    if ! systemctl is-enabled yandex-oauth-frontend > /dev/null; then
		    systemctl enable yandex-oauth-frontend || true
	    fi
	    systemctl --system daemon-reload >/dev/null || true
    fi

    chown -R www-data: /var/log/yandex/oauth-frontend
    chown -R www-data: /usr/lib/yandex/oauth-frontend

    # pick a config
    rm -f /usr/lib/yandex/oauth-frontend/config/current

    if [ -f /etc/yandex/environment.name ] && [ `cat /etc/yandex/environment.name` = 'intranet' ]
    then
        # Team config for intranet
        ln -s /usr/lib/yandex/oauth-frontend/config/`cat /etc/yandex/environment.type`-team /usr/lib/yandex/oauth-frontend/config/current

    else
        ln -s /usr/lib/yandex/oauth-frontend/config/`cat /etc/yandex/environment.type` /usr/lib/yandex/oauth-frontend/config/current
    fi

    yav-deploy -c /etc/yandex/yav-deploy/pkg/oauth-frontend/ || /bin/true
    yav-deploy --rsa-login robot-passport-front --rsa-private-key /usr/lib/yandex/oauth-frontend/robot-passport-front-private-key -c /etc/yandex/yav-deploy/pkg/oauth-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
