#!/bin/bash
#
# 	postrm -- Post removal script for appweb
#
#	Copyright (c) Embedthis Software LLC, 2003-2009. All Rights Reserved.
#

BLD_PRODUCT=!!BLD_PRODUCT!!
BLD_NAME="!!BLD_NAME!!"
BLD_PREFIX=!!BLD_PREFIX!!
BLD_DOC_PREFIX=!!BLD_DOC_PREFIX!!
BLD_SAM_PREFIX=!!BLD_SAM_PREFIX!!
BLD_SRC_PREFIX=!!BLD_SRC_PREFIX!!
BLD_HTTP_PORT=!!BLD_HTTP_PORT!!

###############################################################################

remove() {

	local cdir=`pwd`

	if [ -x $BLD_PREFIX ]
	then
		cd $BLD_PREFIX
		removeIntermediateFiles access.log error.log '*.log.old' .dummy \
			$BLD_PRODUCT.conf .appweb_pid.log '.httpClient_pid.log' make.log
		cleanDir
		cd $cdir 
		rmdir $BLD_PREFIX 2>/dev/null || true
	fi

	ldconfig /usr/lib/libappweb.so.?.?.?

	if which update-rc.d >/dev/null 2>&1 
	then
		rm -f /etc/init.d/appweb
		update-rc.d appweb remove >/dev/null
	fi

}

###############################################################################

purge() {
	local cdir=`pwd`

	remove

	if [ -x $BLD_PREFIX ]
	then
		cd $BLD_PREFIX
		removeIntermediateFiles access.log error.log '*.log.old' .dummy \
			$BLD_PRODUCT.conf .appweb_pid.log '.httpClient_pid.log' make.log \
			appweb.conf groups.db mime.types php.ini server.crt server.key \
			server.key.pem users.db
		cleanDir
		cd $cdir 
		rmdir $BLD_PREFIX 2>/dev/null || true
	fi
}

###############################################################################
#
#	Clean a directory. Usage: removeFiles fileList
#

removeFiles() {
	local f

	if [ ! -f $1 ]
	then
		echo "Can't find file list: $1, continuing ..."
	fi
	echo "Removing files using the file list: $1 ..."
	cat $1 | while read f
	do
		echo rm -f "$f"
	done
}

###############################################################################
#
#	Cleanup empty directories. Usage: cleanDir directory
#
cleanDir() {
	local count f

	echo "Cleaning `pwd` ..."
	if [ `pwd` = "/" ]
	then
		echo "Configuration error: clean directory was '/'"
		return
	fi
	find . -type d -print | sort -r | grep -v '^\.$' | while read d
	do
		count=`ls "$d" | wc -l | sed -e 's/ *//'`
		[ "$count" = "0" ] && echo MOB rmdir "$d"
		if [ "$count" != "0" ] 
		then 
			f=`echo "$d" | sed -e 's/\.\///'`
			echo "Directory `pwd`/${f}, still has user data"
		fi
	done 
}

###############################################################################
#
#	Cleanup intermediate files
#
removeIntermediateFiles() {

	find `pwd` -type d -print | while read d
	do
		before=`pwd`
		cd "${d}"
		eval rm -f $*
		cd "${before}"
	done
}

###############################################################################
#
#	Main
#
cd /
action=$1

case $action in
	remove)					# when new
		remove
		;;
	purge)					# when new
		purge
		;;
	failed-upgrade)			# when new
		oldVersion=$2
		;;
	abort-upgrade)			# when new
		oldVersion=$2
		;;
	upgrade)				# when old
		newVersion=$2
		;;
	disappear)				# when disappearing
		# disappear overwriter overwriter-version
		;;
esac

echo
echo "$BLD_NAME $action successful"
set +x

exit 0
