#! /bin/bash
# postrm script for tcsh
#
# 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' <r>overwrit>r> <new-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    upgrade)
	;;

       purge|remove|failed-upgrade|abort-install|abort-upgrade|disappear)
        rm -f /usr/share/emacs/site-lisp/csh-mode.elc

	rm -f /usr/bin/tcsh
    ;;

    *)
        echo "postrm called with unknown argument \`$1'" >&2
        exit 1

esac

# Using code from dash.postrm
#
# This is to allow downgrades to csh version 6.24.07-1 and
# earlier which do not use the update-shells trigger of debianutils to manage
# /etc/shells.
#
# update-shells will be called at the end of the downgrade because a trigger of
# debianutils on the removal of /usr/share/debianutils/shells.d/tcsh will be
# activated when downgrading to a version of tcsh that doesn't ship that file.
# But a non-existing /usr/share/debianutils/shells.d/tcsh tells update-shells
# to remove tcsh from /etc/shells.
#
# To prevent this from happening, remove /usr/share/debianutils/shells.d/tcsh
# which will be removed by the downgrade anyways, then run update-shells which
# will remove tcsh from /etc/shells and then add it again by running add-shell.
# Subsequent calls to update-shells in the debianutils trigger will now not
# remove tcsh from /etc/shells anymore because the update-shells call in this
# script updated /var/lib/shells.state with the information that it doesn't
# manage tcsh via update-shells anymore.

if [ "$1" = upgrade ] \
	&& dpkg --compare-versions "$2" le 6.24.10-1 \
	&& [ -e /usr/share/debianutils/shells.d/tcsh ] \
	&& [ -x "$(command -v update-shells 2>/dev/null)" ] \
	&& [ -x "$(command -v add-shell 2>/dev/null)" ]; then
	rm /usr/share/debianutils/shells.d/tcsh
	update-shells
	add-shell /bin/tcsh
fi

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
