#!/bin/sh

BLDNUM_H_FILE="$1"
DEFAULT_BLDNUM=


TIMESTAMP=`date`

# Get the 
echo "Identifying the correct Subversion Build Number"

#  The default for this value is the CURRENT revision.
VERS_BLDNUM=`svn info . | grep 'Revision' | cut -d ' ' -f 2`

if [ $? != 0 ]
then
	echo "There was a serious error encountered getting the subversion revision information, exiting out of build."
	exit 1
fi

if [ ! -n "$VERS_BLDNUM" ]
then
	echo "There is no subversion revision information, setting to default value of 65535"
	VERS_BLDNUM="65535"
	DEFAULT_BLDNUM= "/*  There is no subversion revision information, setting to default value of $VERS_BLDNUM  */"
fi

DEFINE_VERSION="#define VERS_BLDNUM $VERS_BLDNUM"

# Remove the build number file used for the previous build
if [ -e $BLDNUM_H_FILE ]
then
	if [ "`grep VERS_BLDNUM $BLDNUM_H_FILE`" == "$DEFINE_VERSION" ];
	then
		echo "Existing file is up to date"
		exit 0
	fi
fi

echo -n "Generating $BLDNUM_H_FILE with VERS_BLDNUM = $VERS_BLDNUM..."

cat <<-EOF > $BLDNUM_H_FILE
	/*
		   AutoGenerated File                      $TIMESTAMP
	*/


	/******************************************************************************
	*
	*              INTEL CORPORATION PROPRIETARY INFORMATION
	*  This software is supplied under the terms of a license agreement or
	*  nondisclosure agreement with Intel Corporation and may not be copied
	*  or disclosed except in accordance with the terms of that agreement.
	*
	*  Copyright (c) 2010 Intel Corporation. All Rights Reserved.
	*
	*******************************************************************************/

	#ifndef __BUILDNUMBER_H__
	#define __BUILDNUMBER_H__

	$DEFAULT_BLDNUM

	$DEFINE_VERSION

	#endif /* __BUILDNUMBER_H__ */
EOF

echo "done"

