#!/bin/sh
# postrm script for eole-common-pkg
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postrm> `remove'
#        * <postrm> `purge'
#        * <old-postrm> `upgrade' <new-version>
#        * <new-postrm> `failed-upgrade' <old-version>
#        * <new-postrm> `abort-install'
#        * <new-postrm> `abort-install' <old-version>
#        * <new-postrm> `abort-upgrade' <old-version>
#        * <disappearer's-postrm> `disappear' <overwriter>
#          <overwriter-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

SSL_CERT_DIR=/etc/ssl/certs
CA_CERT_DIR=/usr/local/share/ca-certificates
CERT_LINK_DIR="$CA_CERT_DIR/menesr"
CHAIN_LINK="ACInfraEducation.crt"
INFRA_LINK="infrastructures.crt"
ENS_LINK="enseignement_scolaire.crt"
EN_LINK="education_nationale.crt"
CERT_LINKS="${CERT_LINK_DIR}/${INFRA_LINK} ${CERT_LINK_DIR}/${ENS_LINK} ${CERT_LINK_DIR}/${EN_LINK} ${CERT_LINK_DIR}/${CHAIN_LINK}"

unlink_ca_cert() {
    [ ! -L "$1" ] || rm -f "$1"
}

case "$1" in
    upgrade|remove|purge)
        for CERT_LINK in $CERT_LINKS
        do
            unlink_ca_cert $CERT_LINK
        done

        # Remove dangling symlinks
        find "$SSL_CERT_DIR" -empty -delete 2> /dev/null || true
        find "$CA_CERT_DIR" -empty -delete 2> /dev/null || true
        update-ca-certificates --fresh

        # Remove workaround https://dev-eole.ac-dijon.fr/issues/21207
        if [ -x /usr/sbin/policy-rc.d ]
        then
            rm -f /usr/sbin/policy-rc.d
        fi
        ;;
    failed-upgrade|abort-install|abort-upgrade|disappear)
        ;;

    *)
        echo "postrm 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
