#!/bin/sh

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

. $TOPDIR/scripts/common-funcs.sh

VERSION=1.5
echo "----------------------------------------------------------"
echo "********** Creating Root File System Tree: $VERSION  **********"	   	
echo "----------------------------------------------------------"

# 
# Required Directories 
# 
PROJDIR=$TOPDIR/development						# PROJECT Directory
FILESDIR=$TOPDIR/$CONF_DIR/config.Root			# File Listings Directory
NEWDIR=$TOPDIR/$BUILD_DIR/target.new			# Modified Files Directory
BINARYDIR=$TOPDIR/development/binaries			# Binaries location
FILES_CIM_DIR=$TOPDIR/development/oem/cim		# CIM Provider files Directory

remove_comments() {
	sed -e 's/#.*$//;/^[[:space:]]*$/d' "$1"
}

ValidateRootNo()
{
	if [ "$EXTRAROOT" == "" ]
	then 
	    ROOTNO=
	    return
	fi
	if [ $EXTRAROOT -eq 0 ]
	then
	   ROOTNO=
	   return 
	fi
	return

	if [ "$ROOTNO" == "" ]
	then 
	    ROOTNO=
	    return
	fi
	if [ $ROOTNO -gt $LOOP ]
	then
	   ROOTNO=
	   return 
	fi
	return
}


#On Input $1 hss the string to be stiped and on output will have stipped string
StripPrefixDir () {
	# If nothing to strip return
	if [ "$ROOTNO"  == "" ]
	then
	   return
	fi
	if [ "$STRIP"  == "" ]
	then
	   return
	fi

	# Remove the prefix P
	STRIP=`echo $STRIP | sed "s/P//g"`
	STRIP=`expr $STRIP + 1`			# For leading .
	INDEX=0

 	for i in `echo $ARG1 | sed "s/\// /g"`
	do  
		INDEX=`expr $INDEX + 1`
		if [ $INDEX -eq 1 ]
		then 
			OUTSTR=$i		# For leading .	
		fi
		if [ $INDEX -gt $STRIP ]
		then 
			OUTSTR=$OUTSTR/$i
		fi
	 done
	 ARG1=$OUTSTR

}

if [ "x$CUSTOMIZE" == "x" ]
then
    export CUSTOMIZE=0
fi

if [ $CUSTOMIZE == 1 ]
then
	sh $TOPDIR/scripts/Customize CUSTROOT
	if [ $? != 0 ]
	then
		exit 1
	fi
fi




#
# Creating TARGET Directory 
#
if [ -d $ROOTDIR ]; then
	echo -n "Removing Old Root Directory ..."
	rm -r -f $ROOTDIR
	if [ $? != 0 ]; then
		echo " Failed"
		exit 1	
	fi 
	echo " Done"
fi


echo -n "Creating Root Directory..."
mkdir -p $ROOTDIR
if [ $? != 0 ]; then
	echo " Failed"
	exit 1	
fi 
echo " Done"

if [ "$EXTRAROOT" != "" ]
then 
	LOOP=0
	while [ $LOOP != $EXTRAROOT ]
	do
		LOOP=`expr $LOOP + 1`
		if [ -d $ROOTDIR$LOOP ]; then
			echo -n "Removing Old Root.$LOOP Directory ..."
			rm -r -f $ROOTDIR$LOOP
			if [ $? != 0 ]; then
				echo " Failed"
				exit 1	
			fi 
			echo " Done"
		fi
		echo -n "Creating Root.$LOOP Directory..."
		mkdir -p $ROOTDIR$LOOP
		if [ $? != 0 ]; then
			echo " Failed"
			exit 1	
		fi 
		echo " Done"
	done
fi


echo -n "Creating Params Directory..."
mkdir -p $PARAMSDIR
if [ $? != 0 ]; then
	echo " Failed"
	exit 1	
fi 
echo " Done"

#
# Presence of necessary files - Error Checking
#
echo -n "Checking existance of necessary files and directories..."

if ! [ -e $FILESDIR/files.dir ]; then
	echo " Failed"
	echo $FILESDIR/files.dir not found. Cannot build root file system
	exit 1
fi

if ! [ -e $FILESDIR/files.cp ]; then
	echo " Failed"
	echo $FILESDIR/files.cp not found. Cannot build root file system
	exit 1
fi

if ! [ -e $FILESDIR/files.ln ]; then
	echo " Failed"
	echo $FILESDIR/files.ln not found. Cannot build root file system
	exit 1
fi

if ! [ -e $FILESDIR/files.new ]; then
	echo " Failed"
	echo $FILESDIR/files.new not found. Cannot build root file system
	exit 1
fi

if ! [ -e $FILESDIR/files.conf ]; then
	echo " Failed"
	echo $FILESDIR/files.conf not found. Cannot build root file system
	exit 1
fi

echo " Done"


#
# Create all the directories
#
echo -n "Creating Directory Tree..."
remove_comments $FILESDIR/files.dir | \
while read DIR MODE; do
	ARG1=$DIR
	DIR=$ARG1
	mkdir -p $ROOTDIR/$DIR
	if [ $? != 0 ]; then
		echo " Failed"
		echo Unable to create $ROOTDIR/$DIR. Aborting...
		exit 1
	fi
	case "$MODE" in
		0[01234567][01234567][01234567]) ;;
		*) MODE=0755 ;;
	esac
	chmod $MODE $ROOTDIR/$DIR || echo "Failed to set mode '$MODE' for '$DIR' in files.dir"
done
if [ $? != 0 ]
then
	echo "Failed. Could not create Directory Tree"
	exit 1
else
	echo " Done"
fi

#
# Copy All the Files 
#
echo -n "Copying files from the Build System..."
remove_comments $FILESDIR/files.cp | \
while read FILE MODE; do
	ORGFILE=$FILE
	ARG1=$FILE
	FILE=$ARG1
	if [ -e $BINARYDIR/$ORGFILE ]
	then
		#echo "Copying $BINARYDIR/$ORGFILE to $ROOTDIR/$FILE "
		cp -p $BINARYDIR/$ORGFILE $ROOTDIR/$FILE
	else
		if [ -e $TARGETDIR/$ORGFILE ]
		then
			#echo "Copying  $TARGETDIR/$ORGFILE to $ROOTDIR/$FILE "
			cp -p $TARGETDIR/$ORGFILE $ROOTDIR/$FILE
		else
			echo " Failed"
			echo Unable to find $ORGFILE to copy to $ROOTDIR/$FILE. Aborting...
			
			exit 1
		fi
	fi
	if [ $? != 0 ]; then
		echo " Failed"
		echo Unable to copy $ORGFILE to $ROOTDIR/$FILE. Aborting...
		exit 1
	fi
	[ -n "$MODE" ] && { chmod $MODE $ROOTDIR/$FILE || echo "Failed to set mode '$MODE' for '$FILE' in files.cp"; } || :
done
if [ $? != 0 ]
then
	echo "Failed. Could not copy required files"
	exit 1
else
	echo " Done"
fi

#Copying CIM Providers and related files 
if [ "$USE_CIM" == "YES" ]; then
if [ -e $FILES_CIM_DIR/files.cp.cimproviders ]; 
then
sort $FILES_CIM_DIR/files.cp.cimproviders | uniq > /tmp/files.cp.cimproviders
cat /tmp/files.cp.cimproviders >  $FILES_CIM_DIR/files.cp.cimproviders

echo -n "Copying CIM Provider files from the Build System..."
remove_comments $FILES_CIM_DIR/files.cp.cimproviders | \
while read FILE ROOTNO STRIP; do
	ValidateRootNo
	ORGFILE=$FILE
	ARG1=$FILE
	StripPrefixDir 
	FILE=$ARG1
	if [ -e $BINARYDIR/$ORGFILE ]
	then
		#echo "Copying $BINARYDIR/$ORGFILE to $ROOTDIR$ROOTNO/$FILE "
		cp -p $BINARYDIR/$ORGFILE $ROOTDIR$ROOTNO/$FILE
	else
		if [ -e $TARGETDIR/$ORGFILE ]
		then
			#echo "Copying  $TARGETDIR/$ORGFILE to $ROOTDIR$ROOTNO/$FILE "
			cp -p $TARGETDIR/$ORGFILE $ROOTDIR$ROOTNO/$FILE
		else
			echo " Failed"
			echo Unable to copy $ORGFILE to $ROOTDIR$ROOTNO/$FILE. Aborting...
			
			exit 1
		fi
	fi
	if [ $? != 0 ]; then
		echo " Failed"
		echo Unable to copy $ORGFILE to $ROOTDIR$ROOTNO/$FILE. Aborting...
		exit 1
	fi
done
if [ $? != 0 ]
then
	echo "Failed. Could not copy required files"
	exit 1
else
	echo " Done"
fi
fi #end of check for files.cp.cimproviders
fi #end of USE_CIM

#
# Copy modified and new  Files 
#
echo -n "Copying new files ..."
remove_comments $FILESDIR/files.new | \
while read FILE MODE; do
	ARG1=$FILE
	FILE=$ARG1
	cp -p $NEWDIR/$FILE $ROOTDIR/$FILE
	if [ $? != 0 ]; then
		echo " Failed"
		echo Unable to copy $ROOTDIR/$FILE. Aborting...
		exit 1
	fi
	[ -n "$MODE" ] && { chmod $MODE $ROOTDIR/$FILE || echo "Failed to set mode '$MODE' for '$FILE' in files.new"; } || :
done
if [ $? != 0 ]
then
	echo "Failed. Could not copy required new files"
	exit 1
else
	echo " Done"
fi

#	
# Creating Symbolic links for the necessary files
#
echo -n "Creating Symbolic links..."
remove_comments $FILESDIR/files.ln | \
while read DEST LNSEP SRC; do
	ARG1=$DEST
	DEST=$ARG1
	ln -s $SRC $ROOTDIR/$DEST
	if [ $? != 0 ]; then 
		echo " Failed"
		echo Unable to link $SRC to $ROOTDIR/$DEST
		exit 1
	fi
done
if [ $? != 0 ]
then
	echo "Failed. Could not create necessary symbolic links"
	exit 1
else
	echo " Done"
fi

#Create links for CIM Providers and related files
if [ "$USE_CIM" == "YES" ]; then
if [ -e $FILES_CIM_DIR/files.ln.cimproviders ]; then

sort $FILES_CIM_DIR/files.ln.cimproviders | uniq > /tmp/files.ln.cimproviders
cat /tmp/files.ln.cimproviders > $FILES_CIM_DIR/files.ln.cimproviders

echo -n "Creating Symbolic links for CIM Providers..."
remove_comments $FILES_CIM_DIR/files.ln.cimproviders | \
while read DEST LNSEP SRC ROOTNO STRIP; do
	ValidateRootNo
	ARG1=$DEST
	StripPrefixDir 
	DEST=$ARG1
	ln -s $SRC $ROOTDIR$ROOTNO/$DEST
	if [ $? != 0 ]; then 
		echo " Failed"
		echo Unable to link $SRC to $ROOTDIR$ROOTNO/$DEST
		exit 1
	fi
done
if [ $? != 0 ]
then
	echo "Failed. Could not create necessary symbolic links"
	exit 1
else
	echo " Done"
fi
fi #end of check for files.ln.cimproviders
fi #end of USE_CIM

##############CONFIGURATION FILES##############################
# Since our move to a separate config partition,
# Config section will have the default configuration files that
# keep getting modified often
# So I see a move of all files that are updated by us and need  
# to be preserved across reboots moved to this place
# This will mean that the original location are just a link to
# the conf location
###############################################################
#	
# Creating Configuration Files Directory
#
echo -n "Creating Configuration Files..."
remove_comments $FILESDIR/files.conf  | \
while read SRC TYPE MODE; do
	if [ -z "$MODE" ]; then
		if echo "$TYPE" | grep -q '^0[0-7][0-7][0-7]$'; then
			MODE="$TYPE"
			TYPE=""
		fi
	fi
	case "$TYPE" in
	n)
		# we have to copy from target.new
		cp -p $NEWDIR/$SRC $PARAMSDIR
		;;
	e)
		# create empty file
		touch $PARAMSDIR/$(basename $SRC)
		;;
	*)
		if [ -e $BINARYDIR/$SRC ]
		then
			cp -p $BINARYDIR/$SRC $PARAMSDIR
		else
			if [ -e $TARGETDIR/$SRC ]
			then
				cp -p $TARGETDIR/$SRC $PARAMSDIR
			else
				echo " Failed"
				echo Unable to find $BINARYDIR/$SRC or $TARGETDIR/$SRC. Aborting...
				exit 1
			fi
		fi
		;;
	esac

	if [ $? != 0 ]; then 
		echo " Failed"
		echo Unable to copy to $PARAMSDIR/$SRC Aborting ...
		exit 1
	fi
	[ -n "$MODE" ] && { chmod $MODE $PARAMSDIR/$(basename $SRC) || echo "Failed to set mode '$MODE' for '$SRC' in files.conf"; } || :
done
if [ $? != 0 ]
then
	echo "Failed . Unable to copy necessary files to params dir"
	exit 1
else
	echo "Done."
fi

#Creating files in /conf for CIM providers
if [ "$USE_CIM" == "YES" ]; then
if [ -e $FILES_CIM_DIR/files.conf.cimproviders ]; then

sort $FILES_CIM_DIR/files.conf.cimproviders | uniq > /tmp/files.conf.cimproviders
cat /tmp/files.conf.cimproviders > $FILES_CIM_DIR/files.conf.cimproviders

echo -n "Creating Configuration Files for CIM Providers..."
remove_comments $FILES_CIM_DIR/files.conf.cimproviders | \
while read SRC TYPE; do
	if [ "$TYPE" = "n" ]
    	then
        	#we have to copy from target.new
		cp -p $NEWDIR/$SRC $PARAMSDIR
	else
		if [ -e $BINARYDIR/$SRC ]
		then
			cp -p $BINARYDIR/$SRC $PARAMSDIR
		else
			if [ -e $TARGETDIR/$SRC ]
			then
				cp -p $TARGETDIR/$SRC $PARAMSDIR
			else
				echo " Failed"
				echo Unable to find $BINARYDIR/$SRC or $TARGETDIR/$SRC. Aborting...
				exit 1
			fi
		fi
	fi
	if [ $? != 0 ]; then 
		echo " Failed"
		echo Unable to copy to $PARAMDIR/$SRC Aborting ...
		exit 1
	fi
done
if [ $? != 0 ]
then
	echo "Failed . Unable to copy necessary files to params dir"
	exit 1
else
	echo "Done."
fi
fi #end of check for files.conf.cimproviders
fi #end of USE_CIM


echo -n "Copying all conf files to defconfig copy in root ..."
mkdir -p $ROOTDIR/etc/defconfig
if [ $? != 0 ]
then
	echo "Failed. Unable to create $ROOTDIR/etc/defconfig"
	exit 1
else
	echo "Done."
fi

cp -Rp $PARAMSDIR/* $ROOTDIR/etc/defconfig
if [ $? != 0 ]
then
	echo "Failed."
	exit 1
else
	echo "Done."
fi

#
# Add the /etc/version file
#
echo -n "Creating $ROOTDIR/etc/version file from $TOPDIR/$BUILD_DIR/projdef.mk: "
(
	. $TOPDIR/$BUILD_DIR/projdef.mk
	echo "FW_VERSION=$MAJOR_VERSION.$MINOR_VERSION.$BUILD_NUMBER" > $ROOTDIR/etc/versions
	echo "FW_VERSION=$MAJOR_VERSION.$MINOR_VERSION.$BUILD_NUMBER"
)

#
# Remove all SDR files from params (/conf).
#
echo -n "Removing all conf SDR files ..."
rm -f $PARAMSDIR/SDR*
if [ $? != 0 ]
then
	echo "SDR removal Failed."
	exit 1
else
	echo "Done."
fi

#
# Stripping Sumbols from ELF files to reduce the size 
#
cp -a $ROOTDIR ${ROOTDIR}.unstripped
echo -n "Stripping Symbols ..."
for i in `find $ROOTDIR -type f`
do
	file $i | grep ELF > /dev/null				 # Check if it is a ELF 
	if [ $? == 0 ]; then 
		file $i | grep "relocatable" > /dev/null
		if [ $? != 0 ]
		then
			[ ! -w $i ] && chmod u+w $i
			$CROSS_COMPILE"strip" $i
			if [ $? != 0 ]; then 
				echo " Failed"
				echo Unable to strip symbols form  $i
				exit 1
			fi
		else
			$CROSS_COMPILE"strip"  --strip-unneeded $i	# For kernel modules 
			if [ $? != 0 ]; then 
				echo " Failed"
				echo Unable to strip symbols form  $i
				exit 1
			fi
		fi
	fi
done
echo " Done"

# Now we have to install the busybox into this directory for the remaining files

cd $TOPDIR/core/busybox
if [ $? != 0 ]; then 
	echo "Unable to find busybox. Root Directory creation Failed"
	exit 1
fi
echo "Installing busybox into the root directory ... PREFIX=$ROOTDIR"
make install CONFIG_PREFIX=$ROOTDIR
if [ $? != 0 ]; then
	echo " Failed"
	exit 1	
fi 
echo " Done"


echo "----------------------------------------------------------"
echo "***** Successfully created Root Directory !! *******"
echo "----------------------------------------------------------"
exit 0

