#!/bin/sh
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


# Disable apache2 configuration
disable_apache2_conf()
{
    if [ -e /usr/share/apache2/apache2-maintscript-helper ]
    then
        . /usr/share/apache2/apache2-maintscript-helper
        apache2_invoke disconf pluxml
    fi
    rm -f /etc/apache2/conf-available/pluxml.conf
}


# Reload apache2
reload_apache2()
{
    # Nothing, since `apache2_invoke disconf` already did what had to be done
    :
}


# Disable lighttpd configuration
disable_lighttpd_conf()
{
    if command -v lighty-disable-mod >/dev/null 2>/dev/null
    then
        lighty-disable-mod pluxml || true
    fi
    rm -f /etc/lighttpd/conf-available/50-pluxml.conf
}


# Reload lighttpd
reload_lighttpd()
{
    # That may fail if lighttpd is not running: this is not a real problem, so
    # ignore it
    invoke-rc.d lighttpd reload || true
}


# Unregister and remove files managed by ucf, and all their avatars
remove_ucffiles()
{
    if command -v ucf >/dev/null 2>/dev/null
    then
        for file in "$@"
        do
            ucf --purge "$file"
        done
    fi
    if command -v ucfr >/dev/null 2>/dev/null
    then
        for file in "$@"
        do
            ucfr --purge pluxml "$file"
        done
    fi
    for ext in '' '~' '%' .bak .ucf-new .ucf-old .ucf-dist
    do
        for file in "$@"
        do
            rm -f "$file$ext"
        done
    done
}


case "$1" in
    purge)
        # Remove special permissions overrides
        if dpkg-statoverride --list /etc/pluxml >/dev/null 2>&1
        then
            dpkg-statoverride --remove /etc/pluxml
        fi
        if dpkg-statoverride --list /etc/pluxml/plugins >/dev/null 2>&1
        then
            dpkg-statoverride --remove /etc/pluxml/plugins
        fi

        # Remove dynamic configuration files managed by ucf
        remove_ucffiles /etc/pluxml/apache2.conf /etc/pluxml/lighttpd.conf \
                        /etc/pluxml/parametres.xml /etc/pluxml/adminkey \
                        /etc/pluxml/users.xml

        if [ -e /usr/share/debconf/confmodule ]
        then
            . /usr/share/debconf/confmodule
            # Purge blog data if chosen by the user during the installation
            db_get pluxml/system/purgedata
            if [ "$RET" = "true" ]
            then
                # Removing blog text
                rm -f /var/lib/pluxml/data/articles/*
                rm -f /var/lib/pluxml/data/statiques/*
                rm -f /var/lib/pluxml/data/commentaires/*
                # Removing media
                rm -rf /var/lib/pluxml/data/documents/*
                rm -rf /var/lib/pluxml/data/documents/.thumbs
                rm -rf /var/lib/pluxml/data/images/*
                rm -rf /var/lib/pluxml/data/images/.thumbs
                # Removing pseudo-configuration
                rm -f /var/lib/pluxml/data/configuration/plugins.xml \
                      /var/lib/pluxml/data/configuration/categories.xml \
                      /var/lib/pluxml/data/configuration/statiques.xml \
                      /var/lib/pluxml/data/configuration/tags.xml
            fi
        fi
    ;;

    remove|disappear)
        if [ -e /usr/share/debconf/confmodule ]
        then
            . /usr/share/debconf/confmodule
            # Remove the configuration from the chosen webservers (it makes no
            # sense to keep webservers configured for a blog that has been
            # removed)
            db_get pluxml/system/webservers
            for webserver in $RET
            do
                webserver=${webserver%,}
                disable_${webserver}_conf "$@"
                if [ "$reload_webserver" = "true" ]
                then
                    reload_$webserver "$@"
                fi
            done
        fi
    ;;

    upgrade|failed-upgrade|abort-install|abort-upgrade)
    ;;

    *)
        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#

# Stop debconf, or the script would stall forever if we restarted lighttpd
# (cf. debconf-devel(7) (search for “daemon”) and bug #133029)
if [ "$1" = "purge" ]
then
    if [ -e /usr/share/debconf/confmodule ]
    then
        db_stop
    fi
fi

exit 0
