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

apply_settings()
{
    local _prefdir _sourcefile _sharedir _dist
    _prefdir="/etc/apt/preferences.d"
    _sourcefile="/etc/apt/sources.list"
    _sharedir="/usr/share/yandex-search-common-apt-rtc"
    _dist="none"

    # For each distro install custom repo
    if `lsb_release -c | grep -q xenial` ; then
        _dist="xenial"
    elif `lsb_release -c | grep -q bionic` ; then
        _dist="bionic"
    else
        echo "WARNING: Unsupported Linux Distro"
    fi
    if [ "${_dist}" != "none" ] ; then
        ln -sf ${_sharedir}/yandex-${_dist}-pin ${_prefdir}/yandex-${_dist}-pin
        mv ${_sourcefile} ${_sourcefile}.old
        ln -sf ${_sharedir}/yandex-${_dist}.list ${_sourcefile}
    fi

    # Remove temporary sources from "nalivka"
    rm -f ${_sourcedir}/ubuntu-security.list
    rm -f ${_sourcedir}/search-nalivka-temporary.list
}

case "$1" in
    configure)
        apply_settings
        ;;

    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
