#!/bin/sh
#
# Try to de-activate mod_dav_svn in the installed httpd.conf and warn
# if this fails.
#
# $FreeBSD: /tmp/pcvs/ports/devel/subversion/files/pkg-deinstall.in,v 1.2 2012-02-14 16:06:21 lev Exp $
#

if [ "NO" != "YES" ] ; then
	exit 0
fi

if [ "$2" != "POST-DEINSTALL" ]; then
	exit 0
fi

TMPDIR=${TMPDIR:=/tmp}
PKG_TMPDIR=${PKG_TMPDIR:=${TMPDIR}}

apxscmd=${PKG_PREFIX}/sbin/apxs
tmpdir=${PKG_TMPDIR}/deinstmod_dav_svn.$$

if [ ! -x ${apxscmd} ]; then
	echo Can\'t find the apxs program: ${apxscmd}.
	exit 1
fi

confdir=`${apxscmd} -q SYSCONFDIR`

if [ ! -d "${confdir}" ]; then
	echo Can\'t find Apache conf dir: ${confdir}
	exit 1
fi

if [ -f "${confdir}/httpd.conf" ]; then
	conffile=httpd.conf
fi
if [ -f "${confdir}/httpd.conf.default" ]; then
	conffile="${conffile} httpd.conf.default"
fi
if [ -z "${conffile}" ]; then
	echo Can\'t find either of ${confdir}/httpd.conf or
	echo ${confdir}/httpd.conf.default.
	exit 1
fi

if ! mkdir ${tmpdir}; then
	echo Can\'t create temporary directory: ${tmpdir}
	exit 1
fi

for i in ${conffile}; do
	echo -n Removing dav_svn_module, authz_svn_module and dontdothat_module from $i in config dir: ${confdir}...
	awk '{if (!/LoadModule[[:blank:]]+dav_svn_module/    &&
		  !/LoadModule[[:blank:]]+authz_svn_module/  &&
		  !/LoadModule[[:blank:]]+dontdothat_module/ )
	    print $0}' < "${confdir}/$i" > "${tmpdir}/$i"
	AWKRC=$?
	diff "${confdir}/$i" "${tmpdir}/$i" | grep "^[<>]" | sed -e 's/#//g' | grep -Evq "^<[[:blank:]]+LoadModule"
	GREPRC=$?
	# last grep should not find anything
	if [ "x${AWKRC}" = "x0" -a "x${GREPRC}" = "x1"  ] ; then
		echo " Ok"
		cat "${tmpdir}/$i" > "${confdir}/$i"
	else
		echo " Error! Please, remove these modules manually"
	fi
done

rm -rf "${tmpdir}"

exit 0
