#!/bin/sh

set -e

if [ -f /usr/share/debconf/confmodule ]; then
	. /usr/share/debconf/confmodule
fi
if [ -f /usr/share/dbconfig-common/dpkg/postrm ]; then
	. /usr/share/dbconfig-common/dpkg/postrm
	dbc_go sympa $@
fi

case "$1" in
    "remove")

	if [ -f /usr/share/debconf/confmodule ]; then
		db_get sympa/use_soap || true
		use_soap="$RET"
	else
		use_soap="false"
	fi

	# Check whether the syslog configuration file is present
	# in case another system log daemon is used instead of syslog
	if [ -f /etc/sympa/facility ]; then
		if perl -ne '/(\S+)/ || exit 1; $ENV{facility}=$1; exit' /etc/sympa/facility \
		  && which syslog-facility >/dev/null 2>&1 \
		  && [ -e /etc/syslog.conf ] \
		  && syslog-facility remove "$facility" \
		  && [ -x /etc/init.d/sysklogd ]; then
			if which invoke-rc.d >/dev/null 2>&1; then
				invoke-rc.d sysklogd reload
			else
				etc/init.d/sysklogd reload
			fi
		fi
		rm -f /etc/sympa/facility
	fi

	if [ "$use_soap" = "true" ]; then
		## Remove Web server configuration
		db_get wwsympa/webserver_type
		webserver="$RET"

		case $webserver in
		    "Apache 2")
			webserver="apache2"
			;;
		    *)
			webserver="none"
			;;
		esac

		if [ "$use_soap" = "true" ]; then
			# Remove symbolic link to webserver configuration snippet
			link=`readlink /etc/$webserver/conf.d/sympa-soap || true`
			if [ "$link" = "/etc/sympa/apache-soap" ]; then
				rm -f /etc/$webserver/conf.d/sympa-soap
			fi
		fi

		if [ $webserver != "none" ]; then
			# Restarting web server if it was requested at configuration time.
			db_get wwsympa/webserver_restart
			restart="$RET"

			if [ "$restart" = "true" ]; then
				if [ -x /etc/init.d/$webserver ]; then
					if which invoke-rc.d >/dev/null 2>&1; then
						invoke-rc.d $webserver force-reload
					else
						etc/init.d/$webserver force-reload
					fi
				fi

			# End up with debconf
			db_stop
			fi
		fi
	fi
	;;

    "purge")
	db_input high wwsympa/remove_spool || true
	db_go
	db_get wwsympa/remove_spool
	remove_archives="$RET"

	if [ "$remove_archives" = "true" ]; then
		echo ""
		echo "Removing archives and spool subdirectories as requested ..."
		rm -rf /var/lib/sympa/wwsarchive 2>/dev/null || true
		rm -rf /var/spool/sympa/wws* 2>/dev/null || true
	fi

	# Delete the log files if purging
	# Remove aliases too.
	rm -f /var/log/sympa.log*

	# Remove static content directory
	if [ -d /var/lib/sympa/static_content ]; then
		rm -r /var/lib/sympa/static_content
	fi

	if [ -f /etc/aliases ]; then
		sed -i~ -e '/#-- SYMPA begin/,/#-- SYMPA end/d' /etc/aliases
		rm -f /etc/aliases~
		newaliases || true
	fi

	rm -f /etc/sympa/cookie 2>/dev/null || true
	rm -f /etc/sympa/cookies.history

	# Remove configuration files
	rm -f /etc/sympa/data_structure.version
	rm -f /etc/sympa/sympa.conf
	rm -f /etc/sympa/wwsympa.conf

	# Try to remove if empty
	rmdir /etc/sympa 2>/dev/null || true

	if [ -f /usr/share/debconf/confmodule ]; then
		db_input high sympa/remove_spool || true
		db_go
		db_get sympa/remove_spool
		remove_spool="$RET"

		if [ "$remove_spool" = "true" ]; then
			echo ""
			echo "Removing lists data and spool directory as requested ..."
			rm -rf /var/lib/sympa 2>/dev/null || true
			rm -rf /var/spool/sympa 2>/dev/null || true
		fi
	fi
	;;
esac

#DEBHELPER#
