#!/bin/sh
# --------------------------------------------
# Clean 1.2 - (C) 2000 Erich Roncarolo
# Clean 1.1 - (C) 1999 Erich Roncarolo
# Clean 1.0 - (C) 1999 Erich Roncarolo
# License: GPL2 (see LICENSE)
# --------------------------------------------

#
# Parsing commands
#

if [ -z "$1" ] ; then
	echo "Usage $0 {distribution} {linux} {root} {packages}"
	exit 1
fi

dircomm=$(dirname $0)
comm=$(basename $0)
if [ ! -x "./$comm" ] ; then
	echo "$comm should be executed in BYLD home: cd $dircomm"
	cd $dircomm
fi

. Environment

while [ "$1" ] ; do
	case $1 in
	d*) CLEAN_DIST=true ;;
	r*) CLEAN_ROOT=true ;;
	l*) CLEAN_LINUX=true ;;
	p*) CLEAN_PACK=true ;;
	a*) CLEAN_DIST=true 
	    CLEAN_ROOT=true 
	    CLEAN_LINUX=true 
	    CLEAN_PACK=true ;;
	*) echo "Usage $0 {distribution} {linux} {root} {packages} {all}"
	   exit 1 ;;
	esac
	shift
done

#
# Cleaning...
#

echo
echo "---------------------"
echo "Distribution cleaner"
echo "---------------------"
echo

if [ "$CLEAN_DIST" ] ; then
	FILE_TO_REMOVE="$RD_IMAGE $TMPFILE $DEST_IMG $LOGFILE \
		./lilo.conf /tmp/-BYLD-INFO-"
	DIR_TO_REMOVE="$MNTDIR $TMPDIR"
	for fd in $FILE_TO_REMOVE ; do
		test -f "$fd" && FTR="$FTR $fd"
	done
	for fd in $DIR_TO_REMOVE ; do
		test -d "$fd" && DTR="$DTR $fd"
	done
	if [ "$FTR" -o "$DTR" ] ; then
		echo "Following files and directories will be removed:"
		echo
		echo $FTR $DTR
		echo
		echo -n "If you don't really want this, press ctrl-c now; otherwise press [enter]..."
		read DUMMY
		echo -n "Cleaning distribution... "
		rm -f $FTR &> /dev/null
		rmdir -R $DTR &> /dev/null
		echo "done."
	else
		echo "Distribution already clean."
	fi
	echo
fi

if [ "$CLEAN_ROOT" ] ; then
	echo "Now every file in $RD_ROOT will be removed"
	echo
	echo -n "If you are not agree, press ctrl-c now; otherwise press [enter]..."
	read DUMMY
	echo -n "Cleaning root... "
	mv $RD_ROOT/CVS $RD_ROOT/.CVS
	rm -Rf $RD_ROOT/* &> /dev/null
	mv $RD_ROOT/.CVS $RD_ROOT/CVS
	rm -f $RD_ROOT/.* &> /dev/null
	touch $RD_ROOT/.empty &> /dev/null
	echo "done."
	echo
fi

if [ "$CLEAN_LINUX" ] ; then
	echo "All files in $RD_LINUX/* will be removed."
	echo "File $RD_LINUX/.config -if present- will be moved in ./"
	echo "!!! KERNEL IMAGE AND MODULES - IF PRESENT IN $RD_LINUX/ - WILL BE REMOVED !!!"
	echo
	echo -n "If you are not agree, press ctrl-c now; otherwise press [enter]..."
	read DUMMY
	echo -n "Cleaning linux... "
	cp $RD_LINUX/.config ./.config &> /dev/null
	mv $RD_LINUX/CVS $RD_LINUX/.CVS
	rm -Rf $RD_LINUX/* &> /dev/null
	mv $RD_LINUX/.CVS $RD_LINUX/CVS
	rm -f $RD_LINUX/.* &> /dev/null
	rm -f $RD_LINUX/.* &> /dev/null
	touch $RD_LINUX/.empty &> /dev/null
	echo "done."
	echo
fi

if [ "$CLEAN_PACK" ] ; then
	echo "All cleaning procedure will be executed in packages."
	echo
	echo -n "If you are not agree, press ctrl-c now; otherwise press [enter]..."
	read DUMMY
	echo "Cleaning packages... "
	cf=$(find $PACK_DIR -name "Clean" 2> /dev/null)
	for p in $cf ; do
		if [ -f $p ] ; then
			echo "Sourcing $p..."
			d=$(dirname $p)
			(cd $d ; source Clean)
			echo "sourced."
		fi
	done
	echo "done."
	echo
fi

sync
echo
echo "Distribution cleaned."
echo

