#!/bin/sh

# Change directory to project directory
cd $TOPDIR

if ! [ -e $TOPDIR/$DEF_FILE ]
then
	echo "$DEF_FILE is missing. Check if SUBPROJECT option is needed"		
	exit 1
fi
. $TOPDIR/$DEF_FILE

echo -n "Cleaning linux ..."
if [ -d $TOPDIR/core/linux ]
then
	cd $TOPDIR/core/linux
	make clean && make distclean
	if [ $? == 0 ]
	then
		echo "Done"
	else
		echo "Failed"
		exit 1
	fi
fi

echo -n "Cleaning rubix ..."
if [ -d $TOPDIR/core/fsafe_boot/rubix ]
then
	cd $TOPDIR/core/fsafe_boot/rubix

	make clean
	if [ $? == 0 ]
	then
		echo "Done"
	else
		echo "Failed, aborting..."
		exit 1
	fi
fi

echo -n "Cleaning uboot ..."
if [ -d $TOPDIR/core/fsafe_boot/uboot ]
then
	cd  $TOPDIR/core/fsafe_boot/uboot
	SAVED_BUILD_DIR=$BUILD_DIR
	export BUILD_DIR=
	make clean && make distclean
	if [ $? == 0 ]
	then
		echo "Done"
	else
		echo "Failed"
		exit 1
	fi
	export BUILD_DIR=$SAVED_BUILD_DIR
	SAVED_BUILD_DIR=
fi

echo -n "Cleaning busybox ..."
if [ -d $TOPDIR/core/busybox ]
then
	cd $TOPDIR/core/busybox
	make clean && make distclean
	if [ $? == 0 ]
	then
		echo "Done"
	else
		echo "Failed"
		exit 1
	fi
fi

# Clean libs
cd $TOPDIR/core/lib
./build_libs clean
if [ $? -ne 0 ]
then
	echo "Failed to clean libs. Aborting"
	exit 1
fi

# Clean apps
cd $TOPDIR/core/apps
./build_apps clean
if [ $? -ne 0 ]
then
	echo "Failed to clean apps. Aborting"
	exit 1
fi


echo -n "Cleaning SFCB ..."
if [ -d $TOPDIR/core/sblim-sfcb ]
then
	cd $TOPDIR/core/sblim-sfcb
	make clean 
	if [ $? == 0 ]
	then
		echo "Done"
	else
		echo "Failed"
		exit 1
	fi
fi



# How to clean target and target.new file system ???
#echo -n "Removing target ..."
#rm -r -f $TOPDIR/$BUILD_DIR/target
#if [ $? == 0 ]
#then
#	echo "Done"
#else
#	echo "Failed"
#	exit 1
#fi

exit 0
