#!/bin/bash

echo -n "build starts at "
date

absolute_dir () {
	local D
	D=$(dirname $1)
	echo "`cd \"$D\" 2>/dev/null && pwd || echo \"$D\"`"
}

retry () {
	# default retry = 10
	t=$2
	[[ x$t = x ]] && t=10
	expr=$1
	$expr && return
	for (( i=1; $i < $t; i=$i+1)); do
		echo retry = $i
		$expr && return
		echo failed.  sleep for 10 minutes.
		sleep 600
	done
}

OEDIR=$(absolute_dir $0)
RSYNC_TARGET=
if [ x$1 == "xtesting" ]; then
    LOCK_FILE=${OEDIR}/build-testing-lock
elif [ x$1 == "xexperimental" ]; then
    LOCK_FILE=${OEDIR}/build-experimental-lock
else
    LOCK_FILE=${OEDIR}/build-lock
fi

echo "Checking lock file ${LOCK_FILE}"

if [ -f ${LOCK_FILE} ] ; then
	echo "Aborted due to already running build.sh"
	exit 10
fi

trap "rm ${LOCK_FILE}" EXIT

touch ${LOCK_FILE}

./clean.sh || true

build() {
       echo "Runing build - ${OM_FEED} ${DISTRO_KERNEL}"
       ####################################################################################
       ## this is a bad hack that needs patching upstream
       patch -N -p0 < openocd.patch 
       ## end bad hack
       ####################################################################################

       retry "bitbake -c fetch virtual/kernel"
       retry "bitbake -c fetch fso-paroli-image"

       ####################################################################################
       ## this is a bad hack that needs patching upstream
       if [ ${OM_FEED} = "experimental" ]; then
	   bitbake -c rebuild linux-openmoko
	   case ${MACHINE} in
	       om-gta01 ) 
                   ;;
	       om-gta02 ) 
		   bitbake -c rebuild xf86-video-glamo ;;
	   esac
       fi
       ## end bad hack
       ####################################################################################

       if ! [ ${OM_FEED} = "experimental" ]; then
	   bitbake -c clean linux-openmoko-devel linux-openmoko-stable
	   bitbake -c rebuild virtual/kernel
       fi 
       bitbake -c buildall fso-console-image
       bitbake gst-plugin-sid
       bitbake -c buildall fso-paroli-image
       bitbake -c buildall -k task-openmoko-feed
       bitbake -c rebuild u-boot-openmoko
       bitbake -c rebuild qi
}

post_build () {
	true
}

#### include config file to overwrite ####
#. ${OEDIR}/build.env

if [ x${1} == "xtesting" ]; then
    . ${OEDIR}/build_testing.env
elif [ x${1} == "xexperimental" ]; then
    . ${OEDIR}/build_experimental.env
else
    . ${OEDIR}/build.env
fi

cd ${OEDIR}/openembedded
git checkout -f org.openembedded.dev
git reset --hard org.openembedded.dev
git pull 
cd ${OEDIR}

touch ${OEDIR}/local/conf/local.conf

# remove any old workdirs file
rm ${TMPDIR}/workdirs.txt || true

for OM_FEED in ${OM_FEED_TARGET}
do
        cd ${OEDIR}/openembedded 

        case ${OM_FEED} in
	    testing )
	        echo "Setting up testing build environment"
	        git checkout -f org.openembedded.dev
		git reset --hard org.openembedded.dev
		git pull 
		git checkout ${TESTING_BRANCH}
		export DISTRO_KERNEL=${TESTING_KERNEL} ;;

	    unstable )
	        echo "Setting up unstable build environment"
	        git checkout -f org.openembedded.dev
		git reset --hard org.openembedded.dev
		git pull
		git checkout ${UNSTABLE_BRANCH}
		export DISTRO_KERNEL=${UNSTABLE_KERNEL} ;;

	    experimental )
	        echo "Setting up experimental build environment"
	        git checkout -f org.openembedded.dev
		git reset --hard org.openembedded.dev
		git pull
		git checkout ${EXPERIMENTAL_BRANCH}
		export DISTRO_KERNEL=${EXPERIMENTAL_KERNEL} ;;
	esac

	export OM_FEED
	cd ${OEDIR}

        for MACHINE in ${MACHINE_TARGET}
	do
	        export BBPATH="${OEDIR}/${MACHINE}-${OM_FEED}-meta:${OEDIR}/local:${OEDIR}/openembedded"
		export MACHINE
		set +xe
		build
		set -xe
	done
done

bitbake package-index

post_build

rm ${LOCK_FILE}

echo -n "build ends at "
date


