# Set the default image to build here
IMG ?= console-image

# In general, do not touch below this line
GIT ?= git
SVN ?= svn

export WORKSPACE := $(shell pwd)
BB_DIR    :=${WORKSPACE}/bitbake
OE_DIR    :=${WORKSPACE}/openembedded
LOCAL_DIR :=${WORKSPACE}/local

export BBPATH  := ${LOCAL_DIR}:${OE_DIR}
export BITBAKE := BB_ENV_EXTRAWHITE="WORKSPACE" ${BB_DIR}/bin/bitbake
BB_VERSION=1.8.12

PACKAGE=world
CMD=



help:
	@echo "The following targets are supported in this Makefile:"
	@echo ""
	@echo "     make workspace"
	@echo "     make update"
	@echo "     make image [IMG=\"...\"]"
	@echo "     make bb PACKAGE=\"...\" [CMD=\"...\"]"

all: workspace image

workspace: ${BB_DIR}/.unpacked \
           ${OE_DIR}/.unpacked \
           ${LOCAL_DIR}/.unpacked

update: ${BB_DIR}/.unpacked ${OE_DIR}/.unpacked
	cd ${BB_DIR} && ${SVN} update
	cd ${OE_DIR} && ${GIT} pull

image:
	${BITBAKE} ${IMG}

bb:
	${BITBAKE} ${PACKAGE} ${CMD}

distclean: clean
	@echo "Removing BitBake and OpenEmbedded..."
	rm -Rf ${BB_DIR} ${OE_DIR}

clean:
	@echo "Removing files created during build process..."
	rm -Rf ${WORKSPACE}/tmp



${BB_DIR}/.unpacked:
	${SVN} checkout \
		svn://svn.berlios.de/bitbake/tags/bitbake-${BB_VERSION} \
		${BB_DIR}
	@touch $@

${OE_DIR}/.unpacked:
	${GIT} clone git://git.openembedded.net/openembedded
	rm -f ${OE_DIR}/conf/sanity.conf && touch ${OE_DIR}/conf/sanity.conf
	@touch $@

${LOCAL_DIR}/.unpacked: ${OE_DIR}/.unpacked ${LOCAL_DIR}/conf/local.conf
	mkdir -p ${LOCAL_DIR}/recipes/images
	touch ${LOCAL_DIR}/recipes/images/dummy-image.bb
	@touch $@

${LOCAL_DIR}/conf/local.conf: ${OE_DIR}/.unpacked
	mkdir -p ${LOCAL_DIR}/conf
	cp ${OE_DIR}/conf/local.conf.sample $@
