#!/bin/sh
# postrm script for jigasi
#
# 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

# Load debconf
. /usr/share/debconf/confmodule


case "$1" in
    purge)

        # Clear the debconf variable
        db_purge

        # clear user and group
        # ensure nothing is running as jigasi user
        if ps -u jigasi h; then
            `ps -u jigasi -o pid h | xargs kill`
        fi
        # and then remove the user
        if getent passwd jigasi > /dev/null ; then
            deluser jigasi
        fi

        # debconf hostname question
        . /usr/share/debconf/confmodule
        db_get jitsi-videobridge/jvb-hostname
        JVB_HOSTNAME="$RET"
        PROSODY_HOST_CONFIG="/etc/prosody/conf.avail/$JVB_HOSTNAME.cfg.lua"
        if [ -f $PROSODY_HOST_CONFIG ]; then
            prosodyctl deluser jigasi@auth.$JVB_HOSTNAME < /dev/null || true
        fi

        rm -rf /usr/share/jigasi/
        rm -f /etc/jitsi/jigasi/config
        rm -f /etc/jitsi/jigasi/sip-communicator.properties
        rm -f /etc/jitsi/jigasi/logging.properties

    ;;

    remove|upgrade|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#

db_stop

exit 0
