#!/bin/sh
# postinst script for eole-common-pkg
#
# 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

IGCA_CERT="/usr/share/ca-certificates/menesr/igca.crt"
MEDDE_CERT="/usr/share/ca-certificates/medde/antsv3racine.crt"
CERTS="${IGCA_CERT}"
CERTS_MEDDE="${MEDDE_CERT}"
CA_CERT_DIR=/usr/local/share/ca-certificates
CERT_LINK_DIR="$CA_CERT_DIR/menesr"
CERT_LINK_DIR_MEDDE="$CA_CERT_DIR/medde"

PREF_FILE=/etc/apt/preferences.d/eole-2.4-experimental.pref
EOLE_LIST=/etc/apt/sources.list.d/eole.list
SOURCES_LIST=/etc/apt/sources.list

SERVICES_TO_MASK="apt-daily.timer apt-daily.service apt-daily-upgrade.timer apt-daily-upgrade.service"
SERVICES_V_TO_DISABLE=""

link_ca_cert() {
    CERT_LINK="${2}/$(basename ${1})"
    if [ ! -L "${CERT_LINK}" ]; then
		  [ -d "${2}" ] || mkdir -p "${2}" || true
		  if [ -d "${2}" ]; then
    	  ln -s "$1" "${CERT_LINK}" || true
		  fi
    fi
}


delete_pref_file() {
    if [ -f "${PREF_FILE}" ]; then
	rm -f "${PREF_FILE}"
    fi
}

delete_old_sources_list() {
    if [ -f "${EOLE_LIST}" ];then
        cat "${EOLE_LIST}" >> "${SOURCES_LIST}"
        rm -f "${EOLE_LIST}"
    fi
}

update_inputrc() {
    #Suppression des beeps dans la console
    grep -q "^set bell-style none" /etc/inputrc || echo "set bell-style none" >> /etc/inputrc
    # Une seule tabulation pour l'autocomplétion
    grep -q "^set show-all-if-ambiguous on" /etc/inputrc || echo "set show-all-if-ambiguous on" >> /etc/inputrc
    grep -q "^set mark-symlinked-directories on" /etc/inputrc || echo "set mark-symlinked-directories on" >> /etc/inputrc
}

purge_old_bash_completion() {
    # Since bash-completion 2.0, script should be under /usr
    for script in apt-eole creoleget
    do
	if [ -f "/etc/bash_completion.d/${script}" ]
	then
	    echo "Removing old bash-completion script /etc/bash_completion.d/${script}"
	    rm -f /etc/bash_completion
	fi
    done
}

mask_services() {
    # Disable unwanted services
    for service in ${@}
    do
        # Unmask non disabled service
        if systemctl status ${service} > /dev/null 2>&1 \
           && [ "$(systemctl is-enabled ${service})" = 'masked' ]
        then
            systemctl unmask ${service}
        fi

        if systemctl is-enabled ${service} > /dev/null 2>&1
        then
            systemctl stop ${service} > /dev/null 2>&1 || true
            systemctl disable ${service}
        fi
        systemctl mask ${service}
    done
}

disable_systemv_services() {
    # Disable unwanted services
    for service in ${@}
    do
	update-rc.d -f -n ${service} disable > /dev/null 2>&1
    done
}

disable_systemv_rsyslog() {
    if [ -L /dev/log -a "$(readlink /dev/log)" != '/run/systemd/journal/dev-log' ]
    then
        rm -f /dev/log
    fi
    systemctl disable rsyslog > /dev/null 2>&1
    systemctl enable rsyslog > /dev/null 2>&1
    if [ -d /run/systemd/system ]
    then
	service systemd-journald stop > /dev/null 2>&1
	service systemd-journald start > /dev/null 2>&1
    fi
}

case "$1" in
    configure)
        for cert in $CERTS
        do
            link_ca_cert "$cert" "${CERT_LINK_DIR}"
        done
        for cert in ${CERTS_MEDDE}
        do
            link_ca_cert "${cert}" "${CERT_LINK_DIR_MEDDE}"
        done
        if [ -n "$(find "${CA_CERT_DIR}" -xtype l)" ]
        then
            # clean dangling symlink
            find "${CA_CERT_DIR}" -xtype l -delete
            update-ca-certificates --fresh || true
        else
            update-ca-certificates || true
        fi
        delete_pref_file
        delete_old_sources_list
        update_inputrc
	purge_old_bash_completion
	mask_services ${SERVICES_TO_MASK}
	disable_systemv_services ${SERVICES_V_TO_DISABLE}
	disable_systemv_rsyslog
	if [ -f /etc/apt/apt.conf.d/02eoleapt ]; then
	    rm -f /etc/apt/apt.conf.d/02eoleapt
	fi
        ;;

    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
