#!/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 _prefdir_avail _aptdir _reposdir _sourcedir
    _prefdir_avail="/etc/apt/preferences.available"
    _prefdir="/etc/apt/preferences.d"
    _aptdir="/etc/apt/sources.list.d"
    _sourcedir="/usr/share/yandex-disk-common-apt/sources"

    # determine where we are: in anally restricted area or regular area
    if `hostname | grep -Eq '^proxy[0-9]{1,2}[a-z]\.disk\.*'` ; then
        _reposdir="/usr/share/yandex-disk-common-apt/repos/restricted"
    else
        _reposdir="/usr/share/yandex-disk-common-apt/repos/default"

    fi

    ln -sf ${_reposdir}/searchkernel-stable.list ${_aptdir}/searchkernel-stable.list

    # Install searchkernel-stable, disk-common and yandex-common repos
    cp -f ${_reposdir}/yandex.list ${_aptdir}/yandex.list

    # Install pins for kernel
    ln -sf ${_prefdir_avail}/kernel-pin ${_prefdir}/kernel-pin

    # For each distro install custom repo
    if `lsb_release -c | grep -q precise` ; then
        # add common and project distro repos for proper generating testing and QA branches
        cat ${_reposdir}/yandex-precise.list >> ${_aptdir}/yandex.list
        cat ${_reposdir}/yandex-disk-precise.list >> ${_aptdir}/yandex.list

        chattr -i ${_aptdir}/../sources.list || true
        cp -f ${_sourcedir}/precise-sources.list    ${_aptdir}/../sources.list

    elif `lsb_release -c | grep -q trusty` ; then
        # add common and project distro repos for proper generating testing and QA branches
        cat ${_reposdir}/yandex-trusty.list >> ${_aptdir}/yandex.list
        cat ${_reposdir}/yandex-disk-trusty.list >> ${_aptdir}/yandex.list

        chattr -i ${_aptdir}/../sources.list || true
        cp -f ${_sourcedir}/trusty-sources.list    ${_aptdir}/../sources.list

    elif `lsb_release -c | grep -q xenial` ; then
        # add common and project distro repos for proper generating testing and QA branches
        cat ${_reposdir}/yandex-xenial.list >> ${_aptdir}/yandex.list
        cat ${_reposdir}/yandex-disk-xenial.list >> ${_aptdir}/yandex.list

        chattr -i ${_aptdir}/../sources.list || true
        cp -f ${_sourcedir}/xenial-sources.list    ${_aptdir}/../sources.list

    else
        echo "WARNING: Unsupported Linux Distro"
    fi

}


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
