#!/bin/sh
#
# $NetBSD: DEINSTALL,v 1.2 2001/04/30 05:53:29 jlam Exp $

PKGNAME=$1
STAGE=$2

CAT="@CAT@"
RM="@RM@"
RMDIR="@RMDIR@"
TRUE="@TRUE@"

APACHE_SYSCONFDIR="@APACHE_SYSCONFDIR@"
SAMPLECONFDIR=${PKG_PREFIX}/share/examples/horde
CONFDIR=${APACHE_SYSCONFDIR}
HORDEDIR=${PKG_PREFIX}/@HORDEDIR@
PHPLIBDIR=${PKG_PREFIX}/@PHPLIBDIR@

CONFFILES="	${PHPLIBDIR}/local.inc					\
		${PHPLIBDIR}/prepend.php3				\
		${HORDEDIR}/config/horde.php3"
NONCONFFILES="	${HORDEDIR}/config/MOTD.html				\
		${HORDEDIR}/config/header.txt				\
		${HORDEDIR}/config/html.php3				\
		${HORDEDIR}/config/lang.php3				\
		${HORDEDIR}/config/menu.txt				\
		${HORDEDIR}/config/mime.php3"

case ${STAGE} in
DEINSTALL)
	# Remove configuration files if they don't differ from the default
	# config file.
	#
	FILE=${CONFDIR}/horde.conf
	SAMPLEFILE=${SAMPLECONFDIR}/horde.conf
	if diff -q ${FILE} ${SAMPLEFILE} >/dev/null
	then
		${RM} -f ${FILE}
	fi
	for FILE in ${CONFFILES} ${NONCONFFILES}
	do
		SAMPLEFILE=${FILE}.dist
		if diff -q ${FILE} ${SAMPLEFILE} >/dev/null
		then
			${RM} -f ${FILE}
		fi
	done
	;;

POST-DEINSTALL)
	modified_files=''
	for FILE in ${CONFDIR}/horde.conf ${CONFFILES} ${NONCONFFILES}
	do
		if [ -f ${FILE} ]
		then
			modified_files="${modified_files} ${FILE}"
		fi
	done

	${RMDIR} ${PHPLIBDIR} 2>/dev/null || ${TRUE}
	${RMDIR} ${HORDEDIR}/config 2>/dev/null || ${TRUE}
	${RMDIR} ${HORDEDIR} 2>/dev/null || ${TRUE}
	${RMDIR} ${CONFDIR} 2>/dev/null || ${TRUE}

	existing_dirs=''
	for dir in ${PHPLIBDIR} ${HORDEDIR}
	do
		if [ -d ${dir} ]
		then
			existing_dirs="${existing_dirs} ${dir}"
		fi
	done

	if [ -n "${modified_files}" -o -n "${existing_dirs}" ]
	then
		${CAT} << EOF
===========================================================================
If you won't be using ${PKGNAME} any longer, you may want to remove:
EOF
		if [ -n "${modified_files}" ]
		then
			${CAT} << EOF

  * the following files:

EOF
			for file in ${modified_files}
			do
				echo "	${file}"
			done
		fi
		if [ -n "${existing_dirs}" ]
		then
			${CAT} << EOF

  * the following directories:

EOF
			for dir in ${existing_dirs}
			do
				echo "	${dir}"
			done
		fi
		${CAT} << EOF
===========================================================================
EOF
	fi
	;;

*)
	echo "Unexpected argument: ${STAGE}"
	exit 1
	;;
esac
exit 0
