
#
# These variables are only correct halfway through the build, so make must
# be run again after that to use them.  (Because autogen is generated in
# the first half and so are the component libraries.)
#
COMPONENT_INCLUDES := $(wildcard components/*/include/*.h* components/libDrawing/autogen/*.h* )
COMPONENT_LIBS     := $(wildcard components/*/lib/*.a)

#test:
#	@echo $(COMPONENT_LIBS)
#	@echo $(KIOSK_INCLUDES)

DEBUG := -g
#DEBUG := -O3

build: dirs build_components stage2 build_demo_app

dirs:
	mkdir -p include/kiosk lib

stage2:
	@echo ""
	@echo "Combining all into a single library."
	@echo ""
	@unset MAKELEVEL; make include/libkiosk.hh lib/libKIOSK.a

include/libkiosk.hh: $(COMPONENT_INCLUDES)
	@echo ""
	@echo "Copying include files for single unified library."
	@echo ""
	rm -f include/libkiosk.hh
	@for file in $(COMPONENT_INCLUDES); do outfile=`echo $$file | sed 's@.*/@kiosk/@'`; echo $$file include/$$outfile; echo '#include "'"$$outfile"'"' >>include/libkiosk.hh; sed 's@#include "@#include "kiosk/@' <$$file >include/$$outfile; done
	@echo ""
	@echo "Include file libkiosk.hh built."
	@echo ""

lib/libKIOSK.a:
	rm -rf tmp
	mkdir -p tmp
	(cd tmp; for f in ../components/*/lib/*.a; do ar x $$f; done)
	(cd tmp; ar rc ../lib/libKIOSK.a *.o)
	rm -rf tmp

build_components:
	@echo ""
	@echo "Building library components."
	@echo ""
	@( cd components; unset MAKELEVEL; make DEBUG=$(DEBUG))
	@echo ""

build_demo_app:
	@echo ""
	@echo "Building demo app"
	@echo ""
	@(cd demo_app; unset MAKELEVEL; make DEBUG=$(DEBUG))
	@echo ""
	@echo "Demo app is built.  To run it, do"
	@echo ""
	@echo "  sudo ./demo_app/exe/demo_app"
	@echo ""

clean:
	rm -f *~
	rm -rf include lib tmp
	(cd components; make clean)
	(cd demo_app; make clean)


