#!/bin/sh
# postrm script for frontaccounting

set -e

if [ "$DPKG_DEBUG" = "developer" ]; then
  set -x
fi

lighttpd_remove() {
	if [ -f /etc/lighttpd/conf-available/50-frontaccounting.conf ] ; then
		rm -f /etc/lighttpd/conf-available/50-frontaccounting.conf
		if which lighty-enable-mod >/dev/null 2>&1 ; then
			lighty-disable-mod frontaccounting
		else
			echo "Lighttpd not installed, skipping"
		fi
	fi
}

apache_remove() {
	if [ -d /etc/$webserver/conf.d ] && [ -L /etc/$webserver/conf.d/frontaccounting.conf ]; then
		rm -f /etc/$webserver/conf.d/frontaccounting.conf
	fi
}
if [ -f /usr/share/debconf/confmodule ]; then
    . /usr/share/debconf/confmodule
fi

includefile=/etc/frontaccounting/apache.conf


case "$1" in
	purge)
		db_get "frontaccounting/skipdb"
		if [ "$RET" != "true" ] ; then
			db_get "frontaccounting/postrm"
			if [ "$RET" = "true" ] ; then
				db_get "frontaccounting/db_user"
				dbuser="$RET"
				db_get "frontaccounting/db_pass"
				dbpass="$RET"
				db_get "frontaccounting/db_host"
				dbserver="$RET"
				db_get "frontaccounting/db_name"
				dbname="$RET"
				db_get "frontaccounting/db_admin_user"
				dbadmin="$RET"
				db_input critical "frontaccounting/db_admin_pass" || true
				db_go || true
				db_get "frontaccounting/db_admin_pass"
				dbadmpass="$RET"
				if [ -e /usr/share/wwwconfig-common/mysql-dropdb.sh ] ; then
					. /usr/share/wwwconfig-common/mysql-dropdb.sh
				else
					echo "Can't drop database (can't find wwwconfig-common script)." >&2
				fi
				if [ -e /usr/share/wwwconfig-common/mysql-dropuser.sh ] ; then
					. /usr/share/wwwconfig-common/mysql-dropuser.sh
				else
					echo "Can't drop user (can't find wwwconfig-common script)." >&2
				fi
			fi
		fi

		db_purge
		rm -rf /etc/frontaccounting
		for server in $webservers; do
			server=$(echo $server | sed 's/,$//')

			if [ -d "/etc/${server}/conf.d" ] ; then
				if [ -L "/etc/${server}/conf.d/frontaccounting" ] ; then
					rm "/etc/${server}/conf.d/frontaccounting"
				fi
			else 
				conffile="/etc/$server/httpd.conf"
				if [ -e /usr/share/wwwconfig-common/apache-uninclude.sh ] ; then
					. /usr/share/wwwconfig-common/apache-uninclude.sh
				else
					echo "Can't uninclude apache config part (can't find wwwconfig-common script)." >&2
				fi
			fi
			if [ "$status" = "purge" ] ; then
				restart="$restart $server"
			fi
		done

		servers="apache-ssl apache mysql"
		if [ -e /usr/share/wwwconfig-common/restart.sh ] ; then
			. /usr/share/wwwconfig-common/restart.sh ||
				echo "Couldn't restart servers (wwwconfig-common script failed)." >&2
		else
			echo "Can't restart servers (can't find wwwconfig-common script)." >&2
		fi
	;;
	remove)
	;;
	upgrade)
	;;
	failed-upgrade|abort-install|abort-upgrade|disappear)
	;;
	*)
		echo "postrm called with unknown argument \`$1'" >&2
		exit 0
	;;
esac

if [ "$1" != "upgrade" ] ; then
	db_reset "frontaccounting/db_admin_pass" || true
fi

#DEBHELPER#

db_stop

exit 0
