#!/bin/sh
#
#    Copyright (c) 2001-2002 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    File name:   cleanup 
#    Module name: utils/firmware
#
#    This script will remove files that don't belong to any packages
#	in the primary partition, excluding a list of known loose 
#	files - files that don't belong to any packages by default.
#
#	It can not remove files in the secondary partition. 	
#
#
#

PATH=/bin:/usr/bin:/sbin:/usr/sbin

#Files that are known loose - don't belong to any package 
LOOSE_FILES=" 
/bin/rcp
/lib/libc.so
/fabos/lib/libhil.so
/fabos/lib/libhil.so.1.0
/fabos/share/release
/fabos/webtools/htdocs/loc_res.jar
/fabos/webtools/htdocs/loc_resjar.properties
/fabos/webtools/jars/loc_res.jar
/fabos/webtools/jars/loc_resjar.properties
/lib/modules/2.6.14.2
/lib/modules/2.4.2_hhl20
/lib/modules/preferred
/lib/libldap-2.3.so.0
"

usage()
{
	echo "Usage: cleanup [ -h ] [ -n ] [ -p | -s ]"
	echo "          -h: show the help message."
	echo "		-n: show the junk files but not remove them."
	echo "		-p: do cleanup on primary partition." 
	echo "		-s: do cleanup on secondary partition."
#	echo "		-r: internally used by resource monitor CF function."
}
#
# Exclude the known loose files
#
# Input: 	
#	a file
#
# Output:  
#	STATUS: 	0 - belong to the list
#	  		1 - doesn't belong to the list
#
#
cleanup_msg()
{
	echo ""
	echo "This utility will remove obsoleted files on the local CP."
	echo ""
	echo "Be aware the tool will remove all unauthorized code under"
	echo "following directories on BOTH partitions:"
	echo ""
	echo "	/bin"
	echo " 	/sbin"
	echo "	/lib"
	echo "	/fabos"
	echo "	/root"
	echo "	/usr "
	echo "	/core_files"
	echo ""
	echo "Note that all the core files will be removed as well."
	echo "In case you want to save any of your private code or core,"
	echo "please copy them before running this command."
	echo ""
	echo -n "Do you want to continue [Y]: " 
	read ans
	if [ "$ans" = "" -o "$ans" = "y" -o "$ans" = "Y" -o "$ans" = "yes" -o "$ans" = "Yes" ]; then
		return 0
	else
		return 1
	fi
}
	

check_loose()
{
	FILE=$1
	STATUS=1
	for k in $LOOSE_FILES; do
		if [ "$k" = "$FILE" ]; then
			STATUS=0
			return $STATUS
		else
			continue
		fi
	done
	return $STATUS
}

do_cleanup()
{
	ROOT=$1
	NO_REMOVE=$2
	
	# Testing chroot is working.
	if [ "$ROOT" = "/mnt" ]; then
		chroot /mnt ls 1>/dev/null
		if [ $? -ne 0 ]; then
			echo ""
			echo "Can not cleanup the secondary partition."
			echo -n "Do you want to continue [Y]: "
			read ret
			if [ "$ret" = "" -o "$ret" = "y" -o "$ret" = "Y" ]; then
				return;
			fi
		fi
	fi	
	for DIR in `chroot $ROOT /bin/cat /etc/cleanup.conf`; do 
		echo "Checking $ROOT$DIR, please wait ..."
		for FILE in `chroot $ROOT /usr/bin/find $DIR -name "*" -print`; do
			chroot $ROOT /bin/rpm -qf $FILE > /dev/null 2>&1
			if [ $? -ne 0 ]; then 
				#
				# Skip any dir which isn't in rpm list. 
				#
				if [ ! -d $ROOT$FILE ]; then 
					check_loose $FILE
					if [ $? -ne 0 ]; then
						if [ "$NO_REMOVE" = 0 ]; then
							echo "--Remove $ROOT$FILE"
							/bin/rm -f $ROOT$FILE  > /dev/null 2>&1
						elif [ "$NO_REMOVE" = 1 ]; then
							echo "--Junk $ROOT$FILE"	
						fi
					fi
				fi
			fi
		done 
	done
	echo "Finish cleanup of $ROOT"
	echo ""
}

#
# main routine
#

PRI=0
SEC=0
NO_REMOVE=0
while getopts hnpsr opt; do
	case $opt in
		h) usage
	 	   exit 1	
		   ;;
		n) NO_REMOVE=1 
		   ;;
		p) PRI=1
		   ;;
		s) SEC=1
		   ;;
		r) do_cleanup / $NO_REMOVE
		   # called from RMCF staraightaway start cleaning up "/"
		   exit 0
		   ;;
		\?) usage;
		    exit 1
		   ;;
	esac
done
if [ $NO_REMOVE = 0 ]; then
	# Only prompt when do remove.
	cleanup_msg
fi
if [ $? -ne 0 ]; then
	exit 0
fi

if [ $PRI = 0 -a $SEC = 0 ]; then
	do_cleanup / $NO_REMOVE
	do_cleanup /mnt $NO_REMOVE
elif [ $PRI = 0 -a $SEC = 1 ]; then
	do_cleanup /mnt $NO_REMOVE 
elif [ $PRI = 1 -a $SEC = 0 ]; then
	do_cleanup / $NO_REMOVE 
elif [ $PRI = 1 -a $SEC = 1 ]; then
	do_cleanup / $NO_REMOVE
	do_cleanup /mnt $NO_REMOVE
fi
