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

patch_security_repos()
{
    local _r_dep _r_url _r_name _r_opts _rep_list
    local _security_list=${1}
    if [ ! -f ${_security_list} ] ; then
        echo "WARNING: Specified '${_security_list}' doesn't exists."
        return 0
    fi

    grep -vE '^\s*$|^\s*#.*$' ${_security_list} | while read _r_dep _r_url _r_name _r_opts ; do
        for _rep_list in /etc/apt/sources.list.d/* /etc/apt/sources.list ; do
            test -f ${_rep_list} || continue
            chattr -i ${_rep_list} > /dev/null 2>&1 || true
            sed -i "/\s${_r_name}\s/d" ${_rep_list}
        done
    done

    grep -vE '^\s*$|^\s*#.*$' ${_security_list} >> /etc/apt/sources.list
}

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

    # For each distro install custom repo
    if `lsb_release -c | grep -q precise` ; then
        patch_security_repos ${_sharedir}/ubuntu-security-precise.list
        ln -sf ${_sharedir}/yandex-search-precise-pin     ${_prefdir}/yandex-search-precise-pin
        ln -sf ${_sharedir}/yandex-precise.list           ${_sourcedir}/yandex-precise.list
        ln -sf ${_sharedir}/yandex-search-precise.list    ${_sourcedir}/yandex-search-precise.list

    elif `lsb_release -c | grep -q trusty` ; then
        sec_list=${_sharedir}/ubuntu-security-trusty.list
        if [ "$(uname -i)" = "aarch64" ]; then
            sec_list=${_sharedir}/ubuntu-ports-security-trusty.list
        fi
        patch_security_repos ${sec_list}
        ln -sf ${_sharedir}/yandex-search-trusty-pin     ${_prefdir}/yandex-search-trusty-pin
        ln -sf ${_sharedir}/yandex-trusty.list           ${_sourcedir}/yandex-trusty.list
        ln -sf ${_sharedir}/yandex-search-trusty.list    ${_sourcedir}/yandex-search-trusty.list
    elif `lsb_release -c | grep -q xenial` ; then
        sec_list=${_sharedir}/ubuntu-security-xenial.list
        patch_security_repos ${sec_list}
        ln -sf ${_sharedir}/yandex-search-xenial-pin     ${_prefdir}/yandex-search-xenial-pin
        ln -sf ${_sharedir}/yandex-xenial.list           ${_sourcedir}/yandex-xenial.list
        ln -sf ${_sharedir}/yandex-search-xenial.list    ${_sourcedir}/yandex-search-xenial.list
    elif `lsb_release -c | grep -q bionic` ; then
        sec_list=${_sharedir}/ubuntu-security-bionic.list
        patch_security_repos ${sec_list}
        ln -sf ${_sharedir}/yandex-search-bionic-pin     ${_prefdir}/yandex-search-bionic-pin
        ln -sf ${_sharedir}/yandex-bionic.list           ${_sourcedir}/yandex-bionic.list
        ln -sf ${_sharedir}/yandex-search-bionic.list    ${_sourcedir}/yandex-search-bionic.list
    else
        echo "WARNING: Unsupported Linux Distro"
    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
