#!/bin/sh

set -e

if [ "$1" = "purge" ]; then
	### XXXX you must use temp files that are not predictable otherwise this script can be
	### object of symlinks attack. mktemp will take care of providing a unique and always
	### different filename with proper permissions.
	tempfile="$(mktemp)"
	### XXXX: note that >> can be extremely dangerous. Always use > if you don't need to
	### append info, so that the file is always cleared from junk. It avoids possible
	### (even if improbable) race conditions)
	BINDCONF=""
	if [ -e /etc/bind/named.conf.local ]; then
		BINDCONF="/etc/bind/named.conf.local"
	elif [ -e /etc/bind/named.conf ]; then
		BINDCONF="/etc/bind/named.conf"
	fi
	### XXX: note, i removed the call to cat, since grep can achive the same results
	### alone. It's nothing fancy, just one shell spawn less.
	if [ -n "$BINDCONF" ]; then
		if grep -v "/etc/sbs/named.conf.sbs" "$BINDCONF" > "$tempfile"; then
			cp "$tempfile" $BINDCONF
			if [ -x /etc/init.d/bind9 ]; then
				/etc/init.d/bind9 reload || true ;
			fi
		fi
	fi
	if [ -e /etc/dhcp3/dhcpd.conf ]; then
		if grep -v "/etc/sbs/sbsdhcpd.conf" /etc/dhcp3/dhcpd.conf > "$tempfile"; then
			cp "$tempfile" /etc/dhcp3/dhcpd.conf
		fi
	fi
	if [ -e /etc/webmin/webmin.catnames ]; then
		if grep -v "wizard" /etc/webmin/webmin.catnames; then
			if grep -v "wizard" /etc/webmin/webmin.catnames > "$tempfile"; then 
		        cp "$tempfile" /etc/webmin/webmin.catnames
			fi
		else
			echo "" > /etc/webmin/webmin.catnames
		fi
	fi
	if [ -x /usr/sbin/update-webmin ]; then
		/usr/sbin/update-webmin remove domaincontroller  || true
	fi
	rm -rf /etc/webmin/domaincontroller
	rm -f "$tempfile"
fi

exit 0

#DEBHELPER#
