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


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

#DEBHELPER#

. /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 configuring this package." >&2
        return 1
    fi

    # Make a directory to back up the old printers/classes.conf if we
    # haven't previously done so.
    if ! [ -e /etc/cups/debathena-cluster-cups-config-saved ]; then
        mkdir /etc/cups/debathena-cluster-cups-config-saved
        for file in printers.conf printers.conf.O classes.conf classes.conf.O; do
            if [ -e /etc/cups/"$file" ]; then
                mv /etc/cups/"$file" /etc/cups/debathena-cluster-cups-config-saved
            fi
        done
    fi
    # However, unconditionally copy printers.conf and classes.conf so we
    # pick up changes in them.
    cp /usr/share/debathena-cluster-cups-config/printers.conf /etc/cups
    cp /usr/share/debathena-cluster-cups-config/classes.conf /etc/cups
}

case "$1" in
    configure)
        restart_cups
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

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

exit 0
