#!/bin/sh
# preinst script for deabthena-cluster-cups-config
#
# see: dh_installdeb(1)

set -e

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


. /usr/lib/debathena-cupsys-config/restart-cups.sh

restart_cups_extra ()
{
    # CUPS needs to not be running while we do this.
    if [ "$1" -gt 0 ]; then
        echo "CUPS is running and cannot be stopped automatically!" >&2
        echo "Please shut down CUPS and retry deconfiguring this package." >&2
        return 1
    fi

    for file in printers.conf printers.conf.O classes.conf classes.conf.O; do
        if [ -e /etc/cups/"$file" ]; then
            rm /etc/cups/"$file"
        fi
    done

    if [ -d /etc/cups/debathena-cluster-cups-config-saved ]; then
        find /etc/cups/debathena-cluster-cups-config-saved -mindepth 1 \
            -exec mv {} /etc/cups \;
    fi
    # And nuke the directory so the old prerm doesn't fail
    rm -rf  /etc/cups/debathena-cluster-cups-config-saved
}


case "$1" in
    install|upgrade)
	# On an upgrade, cleanup after pre-2.0 versions
	if dpkg --compare-versions "$2" lt-nl 2.0~; then
	    restart_cups
	fi
    ;;

    abort-upgrade)
    ;;

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