daemon_reconfigure() {
	daemon="$1"
	action="$2"
	cfgdir="/etc/$pkg"
	cfgfile="$cfgdir/$daemon.conf"
	case "$daemon" in
	    apache*)
		cfgfile="$cfgdir/apache.conf"
		symlinkdir="/etc/$daemon/conf.d"
		symlink="$symlinkdir/$pkg"
		mod_enable="/usr/sbin/a2enmod"
		mod_disable="/usr/sbin/a2dismod"
		modules_required="alias cgi rewrite"
		;;
	    lighttpd)
		symlinkdir="/etc/$daemon/conf-available"
		symlink="$symlinkdir/50-$pkg.conf"
		mod_enable="/usr/sbin/lighty-enable-mod"
		mod_disable="/usr/sbin/lighty-disable-mod"
		modules_provided="$pkg"
#		modules_required="cgi redirect"
		modules_required="cgi"
		;;
	    *)
		echo 1>&2 "Warning: unknown daemon \"$daemon\", skipping reconfiguration"
		return 1
		;;
	esac
	if [ ! -d "$symlinkdir" ]; then
		echo 1>&2 "Warning: $daemon configpath missing, skipping reconfiguration"
		return 1
	fi
	case "$action" in
	    enable)
		if [ ! -e "$symlink" ]; then
			ln -s "$cfgfile" "$symlink"
		elif [ "$cfgfile" != "$(readlink "$symlink")" ]; then
			echo 1>&2 "Warning: $pkg config for $daemon was customized, please remove $symlink and reconfigure $pkg if that customization is unwanted, skipping reconfiguration"
			return 1
		fi
		if [ -n "$mod_enable" ] && [ -n "$modules_required$modules_provided" ]; then
			if [ -x "$mod_enable" ] ; then
				for module in $modules_required $modules_provided; do
					"$mod_enable" "$module"
				done
			else
				echo 1>&2 "Warning: $daemon not installed, $pkg config added but not enabled"
			fi
		fi
		;;
	    disable)
		if [ -n "$mod_disable" ] && [ -n "$modules_provided" ]; then
			if [ -x "$mod_disable" ]; then
				for module in $modules_provided; do
					"$mod_disable" "$module"
				done
			else
				echo 1>&2 "Warning: $daemon not installed, $pkg config will be removed without first getting disabled"
			fi
		fi
		if [ -n "$mod_disable" ] && [ -n "$modules_required" ]; then
			echo 1>&2 "Warning: $daemon module(s) $modules_required possibly enabled by $pkg was not disabled (might still be needed), please disable manually if unused"
		fi
		if [ -e "$symlink" ]; then
			if [ "$cfgfile" = "$(readlink "$symlink")" ]; then
				rm -f "$symlink"
			else
				echo 1>&2 "Warning: $pkg config for $daemon was customized, please remove $symlink manually if unwanted"
			fi
		fi
		;;
	    *)
		echo 1>&2 "Error: unknown action \"$action\", script is broken!"
		exit 1
		;;
	esac
}
