# Makefile for EG51xx
# # #
# # # Copyright (C) 2023 Robustel Technology
# # #
# # This is free software, licensed under the GNU General Public License v2.
# #

BUILD_DIR=build_dir
INSTALL_DIR=install_dir

define func1
	-make -C $(1) clean
	if [ ! -d $(3) ]; then mkdir $(3); fi 
	rm $(3)/$(1)_build -rf && mkdir $(3)/$(1)_build && cp $(1) $(3)/$(1)_build -rf 
	cd $(3)/$(1)_build && tar --exclude=debian/ -czvf $(1).tar.gz $(1) \
	&& ln -sf $(1).tar.gz $(2).orig.tar.gz 
	cd $(3)/$(1)_build/$(1) && debuild -uc -us -ui -tc
endef

all: prepare sample-dido  sample-can sample-cpp sample-gps sample-serial sample-shell sample-uci
	make deb-install
	@echo "bulid done"

sample-dido:
	$(call func1,sample-dido,sample-dido,$(BUILD_DIR))	

sample-can:
	$(call func1,sample-can,sample-can,$(BUILD_DIR))	

sample-cpp:
	$(call func1,sample-cpp,sample-cpp,$(BUILD_DIR))	

sample-gps:
	$(call func1,sample-gps,sample-gps,$(BUILD_DIR))	

sample-serial:
	$(call func1,sample-serial,sample-serial,$(BUILD_DIR))	

sample-shell:
	$(call func1,sample-shell,sample-shell,$(BUILD_DIR))	

sample-uci:
	$(call func1,sample-uci,sample-uci,$(BUILD_DIR))	

sample-upload:
	$(call func1,sample-upload,sample-upload,$(BUILD_DIR))	

helloworld:
	$(call func1,helloworld,helloworld,$(BUILD_DIR))	

prepare:
	rm $(BUILD_DIR) -rf
	mkdir $(BUILD_DIR) 

clean:
	rm $(BUILD_DIR) -rf
	rm $(INSTALL_DIR) -rf

deb-install:
	rm $(INSTALL_DIR) -rf && mkdir $(INSTALL_DIR)
	find $(BUILD_DIR) -name "*deb" |xargs -I {} cp -r {} $(INSTALL_DIR)
	rm $(INSTALL_DIR)/*dbgsym* -rf

.PHONY: sample-dido  sample-can sample-cpp sample-gps sample-serial sample-shell sample-uci helloworld sample-upload
