#!/bin/sh

set -e

# Automatically added by dh_installinit/13.11.4
if [ "$1" = "remove" ] && [ -x "/etc/init.d/prosody" ] ; then
	chmod -x "/etc/init.d/prosody" >/dev/null || true
fi
if [ -z "${DPKG_ROOT:-}" ] && [ "$1" = "purge" ] ; then
	update-rc.d prosody remove >/dev/null
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.11.4
if [ "$1" = remove ] && [ -d /run/systemd/system ] ; then
	systemctl --system daemon-reload >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.11.4
if [ "$1" = "purge" ]; then
	if [ -x "/usr/bin/deb-systemd-helper" ]; then
		deb-systemd-helper purge 'prosody.service' >/dev/null || true
	fi
fi
# End automatically added section


dpkg-maintscript-helper rm_conffile \
  /etc/prosody/certs/localhost.crt 0.11.3-1 prosody -- "$@"

dpkg-maintscript-helper rm_conffile \
  /etc/prosody/certs/localhost.key 0.11.3-1 prosody -- "$@"

if [ "$1" = "purge" ] ; then
	# Delete stale symlink which was created in postinst script
	if [ -h "/etc/prosody/conf.d/localhost.cfg.lua" ] && \
	   ! readlink -e "/etc/prosody/conf.d/localhost.cfg.lua" ; then
		rm "/etc/prosody/conf.d/localhost.cfg.lua"
	fi

	# Delete configs directory
	if [ -d "/etc/prosody/conf.d" ]; then
		rmdir --ignore-fail-on-non-empty "/etc/prosody/conf.d";
	fi

	# Delete directory for .pid
	if [ -d "/run/prosody" ]; then
		rm -rf "/run/prosody";
	fi

	# Delete directory for users data
	if [ -d "/var/lib/prosody" ]; then
		rm -rf "/var/lib/prosody";
	fi

	# Delete directory for log files
	if [ -d "/var/log/prosody" ]; then
		rm -rf "/var/log/prosody";
	fi

	# Delete user 'prosody'
	userdel prosody > /dev/null || true
	groupdel prosody > /dev/null || true

fi
