###########################################################################
#
# get our software configure 
#
#ifeq (.config,$(wildcard .config))

CONFIGED:= $(shell if [ -a .config ]; then echo yes; else echo no; fi)
ifeq (${CONFIGED}, yes)
include .config
all: subdirs rootfs romfs
else
all: help
	@exit 1
endif

# config_error
ROOTDIR  = $(shell pwd)
#export KB_DIR=$(ROOTDIR)/build/blp/kernel
include BaseVar.mk
#include Rules.make

DIRS =  build/
-include quicktarget.mk
###########################################################################
help:
	@echo "*************************************************"
	@echo "The build sequence for this source tree is:"
	@echo "1. 'make tools'"
	@echo "2. 'make prepare'"
	@echo "3. 'make'"
	@echo "4. 'make rootfs'"
	@echo "5. 'make romfs'"
	@echo "**************************************************"
	@echo "**************************************************"
	@echo "make prodefconfig : recover product default config"
	@echo "make refresh_dirs : Refresh the symbol links      "	
	@echo "make mrproper     : Clean all build files         "	
	@echo "make switch     	 : Switch product                "	
	@echo "**************************************************"
	@echo "Quick Build Targets:$(QuickTarget) "
	@echo ""
	@echo "Quick Clean Targets:$(QuickCleanTarget) "
	@echo ""
	@echo "Quick DistClean Targets:$(QuickDistCleanTarget) "
	@echo ""
	@echo "Quick Install Targets:$(QuickInstallTarget) "

.PHONY: subdirs clean menuconfig scripts prepare tools build_lib
subdirs: build $(DIRS) build_lib
	for dir in $(DIRS) ; do [ ! -d $$dir ] || make -C $$dir || exit 1 ; done
install: $(DIRS)
	for dir in $(DIRS) ; do [ ! -d $$dir ] || make -C $$dir install || exit 1 ; done
clean:
	echo $(DIRS)
	for dir in $(DIRS) ; do [ ! -d $$dir ] || make -C $$dir clean || exit 1 ; done

checkautoproduct:
ifndef AUTOPRODUCT 
	@echo autobuild need define PRODUCT,ex:"make autobuild AUTOPRODUCT=UML"
	@exit -1
endif

ckconfig:
	$(Q)cp -f build/product/defconfig build/.config
	$(Q)cd build;mkdir -p include/config;../scripts/conf -s ../Config.in 2>/dev/null
	$(Q)defconfig=`readlink build/product/defconfig`;\
	SVNST=`svn st $$defconfig`;\
	if [ "-$${SVNST}" != "-" ]; then \
	  echo ;echo;echo;\
	  echo "******Warning!!!        ************";\
	  echo "*The defconfig need to commit";\
	  echo "*";\
	  echo $${SVNST}; \
	  echo "*";\
	  echo "************************************";\
	fi

autoprepare:checkautoproduct scripts
	@sh $(ROOTDIR)/tools/compatgit
	@sh $(ROOTDIR)/tools/mkbuilddir $(AUTOPRODUCT)
	@sh $(ROOTDIR)/tools/prepareBLP.sh
	cd build;mkdir -p include/config;../scripts/conf -s ../Config.in
	$(Q)rm -f .config; echo "ok!!!"
	$(Q)ln -s build/.config .config;
	
autobuild:checkautoproduct autoprepare tools 
	make subdirs rootfs romfs

##scriptsĿquicktarget.mk
menuconfig: scripts
	$(Q)if [ -f .config -a ! -L .config ]; then mv .config build/; fi;
	$(Q)cd build;mkdir -p include/config; ../scripts/mconf ../Config.in;../scripts/conf -s ../Config.in
	$(Q)rm -f .config; echo "ok!!!"
	$(Q)ln -s build/.config .config; 
#	$(Q)cp -f build/.config build/product/defconfig;

defconfig:
	svn revert product/$(PRODUCT)/defconfig;
	$(Q)cp -f build/product/defconfig build/.config;
	make menuconfig

mangle_config:
	$(Q)cp -f build/.config build/product/defconfig;

switch:
	$(Q)./tools/switch.sh	

mrproper:
	$(Q)echo  "Are you sure to remove ALL PRODUCTS files(yes/no)"; 
	$(Q)read item;\
		if [ ! "$${item}" = "yes" ]; then exit -1; fi;
	rm -rf build build.* arch.mk;
	$(Q)rm -f .config
	
distclean:clean
	-for dir in $(DIRS) ; do [ ! -d $$dir ] || make -C $$dir distclean || exit 1 ; done
	
prepare: prepare_dirs menuconfig

prepare_dirs:
	@sh $(ROOTDIR)/tools/compatgit
	@sh $(ROOTDIR)/tools/mkbuilddir
	@sh $(ROOTDIR)/tools/prepareBLP.sh

prepare_kernel:
	@sh $(ROOTDIR)/tools/prepareBLP.sh

refresh_dirs:
	$(Q)sh ${ROOTDIR}/tools/mksymlink src/ build/

rootfs:rootfs_clean install_rootfs install install_profile 
	$(Q)cd $(FSROOT);tar cf etc/var.tar var
	-find $(FSROOT) -name .svn | xargs rm -rf 

install_rootfs:
	@sh $(ROOTDIR)/tools/buildRootfs.sh
	@echo "install_rootfs ok!"

libopt:
	-$(Q)if [ ! -d ${BUILDDIR}/templib ]; then mkdir -p ${BUILDDIR}/templib; fi;
	-$(Q)python $(ROOTDIR)/tools/mklibs.py -D -L $(FSROOT)/lib/ -L $(TOOLCHAINS_SYSROOT)/lib/ --target=$(TOOLCHAINS_PATH)/rsdk-linux- -d $(BUILDDIR)/templib `find $(FSROOT) -path $(FSROOT)/lib -prune -o -type f -print | file -f - | grep ELF | cut -d':' -f1`
	-$(Q)cp ${BUILDDIR}/templib/* ${FSROOT}/lib/
	
kernel_strip:
	@echo "Stripping kernel modules..."
ifeq ($(CONFIG_MODULES), y)
	$(STRIP) --strip-debug --strip-unneeded `find $(FSROOT)/lib/modules -type f -print | file -f - | grep ELF | cut -d':' -f1`
	@echo "strip kernel modules ok!"
else
	@echo "modules disabled in current config, skipping ..."
endif

apps_strip:
	@echo "Stripping userapps and shared libraries more..."
	#$(SSTRIP) `find $(FSROOT) -path $(FSROOT)/lib/modules -prune -o -type f -print | file -f - | grep ELF | cut -d':' -f1`
	#$(LSTRIP) $(FSROOT)
	#$(ROOTDIR)/tools/rsdk-linux-lstrip $(FSROOT)
	@echo "strip userapps and shared libraries ok!"	
ifdef CONFIG_APPS_SSAP_ALLSTANDARD
	sed 's/Standard="[01]"//g' $(FSROOT)/etc/config_full.xml > config_full.xml
	cp -f config_full.xml $(FSROOT)/etc/;
	rm -f ./config_full.xml;
	sed 's/Standard="[01]"//g' $(FSROOT)/etc/config.xml > config.xml
	cp -f config.xml $(FSROOT)/etc/;
	rm -f ./config.xml;
endif
ifdef CONFIG_APPS_SSAP_ALLWRITABLE
	sed 's/Writable="[01]"//g' $(FSROOT)/etc/config_full.xml > config_full.xml
	cp -f config_full.xml $(FSROOT)/etc/;
	rm -f ./config_full.xml;
	sed 's/Writable="[01]"//g' $(FSROOT)/etc/config.xml > config.xml
	cp -f config.xml $(FSROOT)/etc/;
	rm -f ./config.xml;
endif
ifdef CONFIG_APPS_SSAP_STRIPTYPE
	cat $(FSROOT)/etc/config_full.xml | sed -f tools/sed_strip_type.script >config_full.xml
	cp -f config_full.xml $(FSROOT)/etc/;
	rm -f ./config_full.xml;
	cat $(FSROOT)/etc/config.xml | sed -f tools/sed_strip_type.script >config.xml
	cp -f config.xml $(FSROOT)/etc/;
	rm -f ./config.xml;
endif

install_profile:
	$(Q) if [ -f $(ROOTDIR)/build/product/installProfile.sh ]; then sh $(ROOTDIR)/build/product/installProfile.sh;fi;
	@echo "install profile file ok!"



multi_lang:
	@mkdir -p ${ROOTDIR}/build/romfs/multi_lang
#	do something

romfs:kernel_strip apps_strip  multi_lang
	@rm -rf $(FSROOT)/var/*
	@sh $(ROOTDIR)/tools/buildImage.sh

tools:
	$(Q)make -C tools/src install

tools_clean:
	$(Q)make -C tools/src clean 

rootfs_clean:
	@rm ${ROOTDIR}/build/romfs/rootfs/* -fr

build_lib:
#	make openssl
		
