#!/bin/bash
#
# Script to build iBMC platform firmware
# Must be placed in and invoked from the <Project>/scripts directory
#
# Revsion history:
#	2007-03-08 Initial version
#	2007-03-16 Simplify build process
#	2007-04-18 Link /root/cc to cc if it doesn't exist
#

####################################################################
# Usage

DisplayUsage()
{
	echo "Usage: $0 <BuildType> [<Platform Name>]"
	echo "   <BuildType> can be BUILD/REBUILD/CLEAN or BUILDDEV/REBUILDDEV/CLEANDEV or IMAGE or WIPE"
	echo "   Platform Name is needed only if there is more than one platform configs directory"
	exit 1
}

if [ $# -lt 1 ]  ||  [ $# -gt 2 ]
then
	DisplayUsage
fi

####################################################################
# Setup

TOPDIR=`dirname $PWD`
ROOT_DIR=`dirname $TOPDIR`
PROJ=`basename $TOPDIR`

BUILD_TYPE=$1

if [ "$#" == 2 ]
then
	PLATFORM=$2
else
	CONFIGS_LIST=`ls -d $TOPDIR/configs.* | grep -v configs.Core`
	case `ls -d $TOPDIR/configs.* | grep -v configs.Core | wc -l` in
	0)
		echo "Can't find configs.$PLATFORM directory, Aborting"
		exit 1
		;;
	1)
		PLATFORM=`ls -d $TOPDIR/configs.* | grep -v configs.Core`
		PLATFORM=`basename $PLATFORM | sed s/configs\.//g`
		;;
	*)
		echo "Must specify Platform when more than one configs.<Platform> directory present"
		exit 1
	esac
fi

if ! [ -e $TOPDIR/configs.$PLATFORM ]
then
	echo "Missing configs.$PLATFORM directory, Aborting"
	exit 1
fi

export PLATFORM

#$$ if ! [ -e $TOPDIR/Intel ]
#$$ then
#$$     echo -n "Creating softlink to Intel development tree ..."
#$$     ln -s $TOPDIR/development/oem/firmware/IPMI/oem/Intel $TOPDIR
#$$     echo "Done"
#$$ fi
    
####################################################################
# Cross-compiler version check

CCCONFIG=arm-linux.crosstoolconfig.txt

if ! [ -d $TOPDIR/cc ]
then
	if ! [ -d /root/cc ]
	then
		echo "Missing cross-compiler directory $TOPDIR/cc ...Aborting"
		exit 1
	fi

	ln -s /root/cc $TOPDIR/cc
	if [ $? != 0 ]
	then
		echo "Failed to link /root/cc as $TOPDIR/cc ...Aborting"
		exit 1
	fi
fi

if ! [ -e $TOPDIR/cc/$CCCONFIG ]
then
	echo "Missing cross-compiler version file $TOPDIR/cc/$CCCONFIG ...Aborting"
	exit 1
fi

if ! [ -e $TOPDIR/configs.$PLATFORM/$CCCONFIG ]
then
	echo "Missing cross-compiler version file $TOPDIR/configs.$PLATFORM/$CCCONFIG ...Aborting"
	exit 1
fi

cmp $TOPDIR/configs.$PLATFORM/$CCCONFIG $TOPDIR/cc/$CCCONFIG
if [ $? != 0 ]
then
	echo "Wrong version of cross-compiler is present"
	echo "...See $TOPDIR/configs.$PLATFORM/$CCCONFIG for expected version"
	echo "...Aborting"
	exit 1
fi


####################################################################
# Build

# Call PreBuildDev Hook in configs/scripts dir if any
if [ -e $TOPDIR/configs.$PLATFORM/scripts/PreBuildDev ]
then
	sh $TOPDIR/configs.$PLATFORM/scripts/PreBuildDev $1
	if [ $? != 0 ]
	then
		echo "ERROR executing platform PreBuildDev script. Aborting!!!"
		exit 1
	fi
fi

# Actual build
echo "Build type $BUILD_TYPE started at:  " `date`
nice ./BuildProject.oem $PROJ:$PLATFORM $BUILD_TYPE $ROOT_DIR 2>&1
RESULT=$?
echo "Build type $BUILD_TYPE finished at: " `date`

if [ $RESULT != 0 ]
then
	echo "Build script failed ... Aborting"
	exit 1
fi

# Call PostBuildDev Hook in configs/scripts dir if any
if [ -e $TOPDIR/configs.$PLATFORM/scripts/PostBuildDev ]
then
	sh $TOPDIR/configs.$PLATFORM/scripts/PostBuildDev $1
	if [ $? != 0 ]
	then
		echo "ERROR executing platform PostBuildDev script. Aborting!!!"
		exit 1
	fi
fi

####################################################################
# Done

exit 0

