#!/bin/sh

# check for options
while [ $# -gt 0 ]; do
	ARG="$1"
	if strstr "$ARG" "="; then
		eval "$ARG"
	else
		echo "unexpected argument '$ARG'"
		exit 1
	fi
	shift
done

# set -x
echo "IN BUILDIMAGE"

if [ -z "$TOPDIR" ]
then
    echo "TOPDIR is undefined. Must be defined and exported by calling script"        
    exit 1
fi
if [ -z "$DEF_FILE" ]
then
    echo "DEF_FILE is undefined. Must be defined and exported by calling script"        
    exit 1
fi

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

# import the cfg_parse and parse_number functions
. $TOPDIR/scripts/common-funcs.sh

# Set Path for Compiler and Misc tools
export PATH=$TOPDIR/cc/bin:$TOPDIR/tools:$TOPDIR/tools/rubZip:$TOPDIR/misctools:$PATH:$TOPDIR/tools/cramfs

# Set the file system directoies and output file names
export PARAMSDIR=$TOPDIR/$BUILD_DIR/Output/Params        # Conf FileSystem Directory
export ROOTDIR=$TOPDIR/$BUILD_DIR/Output/root        # Root FileSystem Directory

# WWW  FileSystem Directory
if [ -d $TOPDIR/development/oem ]
then
    if [ "$WEBSRC" == "" ]
    then
        export WWWDIR=$TOPDIR/development/oem/www
    else
        if  [ -d $TOPDIR/development/oem/$WEBSRC ]
        then 
            export WWWDIR=$TOPDIR/development/oem/$WEBSRC
        else
            export WWWDIR=$TOPDIR/development/oem/www
        fi    
    fi
else
    export WWWDIR=$TOPDIR/development/opensource/www
fi


export PARAMSFILE=$TOPDIR/$BUILD_DIR/Output/Config.bin        # Output Conf FS image
export ROOTFILE=$TOPDIR/$BUILD_DIR/Output/Root.bin            # Output Root FS image
export WWWFILE=$TOPDIR/$BUILD_DIR/Output/Www.bin              # Output Www  FS image
export OUTPUTDIR=$TOPDIR/$BUILD_DIR/Output

GENIMAGE_FILE=$TOPDIR/$CONF_DIR/config.genimage2


# Check if Build directory is present
if  [ -d $TOPDIR/$BUILD_DIR/Output ]
then
    echo -n "Cleaning $TOPDIR/$BUILD_DIR/Output ..."
    rm -rf $TOPDIR/$BUILD_DIR/Output
    if [ $? == 0 ]
    then    
        echo "Done"
    else
        echo "Failed"
        exit 1
    fi
fi

echo -n "Creating $TOPDIR/$BUILD_DIR/Output ..."
mkdir -p $TOPDIR/$BUILD_DIR/Output/www
if [ $? == 0 ]
then
    echo "Done"
else
    echo "Failed"
    exit 1
fi

if [ "$ARCH" == "powerpc" ]
then
    UIMAGE_PATH=arch/ppc/boot/images
else
    UIMAGE_PATH=arch/$ARCH/boot
fi

# Copy linux image 
if ! [ -e $TOPDIR/core/linux/$UIMAGE_PATH/uImage ]
then
    echo "Unable to find Linux Image. Aborting"
    exit 1
fi

echo -n "Copying Linux Image to Output Directory ..."
cp  $TOPDIR/core/linux/$UIMAGE_PATH/uImage $TOPDIR/$BUILD_DIR/Output
if [ $? == 0 ]
then    
    echo "Done"
else
    echo "Failed"
    exit 1
fi


if ! [ -e $TOPDIR/core/fsafe_boot/uboot/u-boot.bin ]
then
    echo "Unable to find U-Boot Image. Aborting"
    exit
fi

# Copy the u-boot  image 
echo -n "Copying U-Boot Image to Output Directory ..."
#cp  $TOPDIR/core/fsafe_boot/uboot/u-boot.bin.gz.rubix  $TOPDIR/$BUILD_DIR/Output
cp  $TOPDIR/core/fsafe_boot/uboot/u-boot.bin  $TOPDIR/$BUILD_DIR/Output
if [ $? == 0 ]
then    
    echo "Done"
else
    echo "Failed"
    exit 1
fi




# Find out the tool's option for Endian 
if [ "$ENDIAN" == "LITTLE" ]
then
   E_OPTION="l"
fi

if [ "$ENDIAN" == "BIG" ]
then
   E_OPTION="b"
fi
if [ "$E_OPTION" == "" ]
then
   echo "ENDIAN option not specified in $DEF_FILE file. Aborting"
   exit 1
fi

if [ "$ROOTFS_IN_JFFS2" == "" ]
then 
    IMAGE_ARCH=$ARCH
    if [ "$ARCH" == "powerpc" ]
    then
         IMAGE_ARCH=ppc
    fi

    # ---- Create Root filesystem as squashfs ----


    # Create Exrta root if any
    if [ "$EXTRAROOT" != "" ]
    then 
        LOOP=0
        while [ $LOOP != $EXTRAROOT ]
        do
            LOOP=$((LOOP + 1))
            echo -n "Building Root.$LOOP File system as CRAMFS ..."
            mkcramfs -r$E_OPTION $ROOTDIR$LOOP $OUTPUTDIR/Root"$LOOP"CRAMFS.bin > /dev/null 2>&1
            if [ $? != 0 ]
            then
                echo "Failed"
                exit 1
            fi
            echo "Done"
        done
    fi
else
    # ---- Create Root filesystem as rootfs ----
    echo -n "Building Root File system as JFFS2 ..."
    cfg_parse $TOPDIR/configs.IBMC/config.genimage2 PARAMS
    Alloc=$(parse_number $Alloc)
    PADSIZE=$((Alloc-ERASE_BLOCK_SIZE))
    mkfs.jffs2 -$E_OPTION --root=$ROOTDIR --output=$ROOTFILE  --eraseblock=$ERASE_BLOCK_SIZE --pad=$PADSIZE $NO_CLEAN_MARKERS
    if [ $? != 0 ]
    then
        echo "Failed"
        exit 1
    fi
    echo "Done"
fi




# Copy any other web to be merged
if [ "$WEBMERGE" != "" ]
then
        if [ -d $TOPDIR/development/oem/$WEBMERGE ]
        then
        echo -n "Copying WWW Merge file system to output ..."
          cp -rf $TOPDIR/development/oem/$WEBMERGE/* $OUTPUTDIR/www
        if [ $? != 0 ]
        then
            echo "Failed"
            exit 1
        fi
        echo "Done"
    fi
fi

echo -n "Removing .svn entries ..."
# set +x
find $OUTPUTDIR/www -name "\.svn" -exec rm -rf {} +
if [ $? != 0 ]
then
	# set -x
	echo "Failed"
	exit 1
fi
echo "Done"

echo -n "Removing any file with .keep in its filename ..."
# set +x
for i in `find $OUTPUTDIR/www | grep "\.keep"`
do
        rm -r -f $i
    if [ $? != 0 ]
    then
        # set -x
        echo "Failed"
        exit 1
    fi
done
# set -x
echo "Done"



echo -n "Removing Thumbs.db files..."
# set +x
for i in `find $OUTPUTDIR/www -name "Thumbs.db"`
do
    rm -r -f $i
    if [ $? != 0 ]
    then
        # set -x
        echo "Failed"
        exit 1
    fi
done    
# set -x
echo "Done"

echo -n "Removing GUISE/GUILIB unused files..."
# set +x
for i in `find $OUTPUTDIR/www  | grep "GUISE\/GUILIB"`
do
    rm -r -f $i
    if [ $? != 0 ]
    then
        # set -x
        echo "Failed"
        exit 1
    fi
done    
# set -x
echo "Done"






# Copy Build No file to Outputdir
if [ -e $TOPDIR/$BUILD_DIR/Output/BUILDNO ]
then
    echo -n "Removing Old BUILDNO ..."
    rm -f $TOPDIR/$BUILD_DIR/Output/BUILDNO
    if [ $? != 0 ]
    then
        echo "Failed"
        exit 1
    fi
    echo "Done"
fi
echo -n "Copying BUILDNO File ..."
cp -f $TOPDIR/development/BUILDNO $TOPDIR/$BUILD_DIR/Output/BUILDNO
if [ $? != 0 ]
then
    echo "Failed"
    exit 1
fi
echo "Done"

# Get the current subversion revision and use as the build number
SVN_BLDNUM=`svn info $TOPDIR | grep Revision | awk '{print $2}'`

if [ x$SVN_BLDNUM == "x" ]
then
    SVN_BLDNUM=0
fi
echo "Subversion Build Number is $SVN_BLDNUM"

# we are done here because signtool makes the rom.ima now
echo "LEAVING BUILDIMAGE"

exit 0
