.EXPORT_ALL_VARIABLES:

TARGET ?= bas

COMMON_MAKEFILES_DIR=./makefiles
include $(COMMON_MAKEFILES_DIR)/make.env

# make.bas is not used/avaliabe during step install
# it is needed during step config - providing configure settings to cmake
-include make.bas

# getting the full path of DIR
DIR_FULL_PATH := $(abspath $(DIR))

BAS_PREFIX ?= /usr/local/bas

# set BAS debug mode, use 0 for Release
BAS_DEBUG_LEVEL := 0
BAS_DEBUG_SANITIZER := 0
ifeq ($(BAS_DEBUG_LEVEL),1)
# -d cmake debug level 1, RelWithDebInfo, no symbol stripping
BAS_DEBUG_FLAGS := -d 1
endif
ifeq ($(BAS_DEBUG_LEVEL),2)
# -d cmake debug level 2, DEBUG -g
BAS_DEBUG_FLAGS := -d 2
endif
ifeq ($(BAS_DEBUG_SANITIZER),1)
# -s address sanitizer - runtime heap check with gcc address sanitizer
BAS_DEBUG_FLAGS += -s
endif

# define iperf3 path (based on iperf3_openbfc)
IPERF3_EXE ?= /bin/iperf3

# Configure Options
# ------------------------------------------------------------------------------
include $(BR2_CONFIG)
BCM_COMMON_DIR := $(abspath $(TOPDIR))/../../rbb_common
BCM_COMMON_DIR := $(abspath $(BCM_COMMON_DIR))
BAS_SRCDIR = $(BCM_COMMON_DIR)/bas/src


# BAS_DEPENDENCIES = libopenssl netsnmp custotp iperf3_openbfc fdt
# BAS_DEPENDENCIES += bas_cjson
# BAS_DEPENDENCIES += bas_libuv
# BAS_DEPENDENCIES += bas_mqtt
# BAS_DEPENDENCIES += bas_sqlite
# BAS_DEPENDENCIES += bas_uriparser

BAS_VERSION = 2.0

# Commands
# ------------------------------------------------------------------------------
all: all_

bas_repo_check:
	@echo "BAS repo check."
	cd $(BCM_COMMON_DIR) && ./.bas_repo_check.sh -b

#custom rsync operation, rsync src code from rbb_common/bas/src
bas_custom_rsync: bas_repo_check
	@echo "Custom rsync $(TARGET)."
	if [ -d $(BAS_SRCDIR) ]; then \
		rsync -LKau --exclude '.git' $(BAS_SRCDIR)/ $(DIR_FULL_PATH)/ || exit 2; \
	else \
		echo "Extract $(TARGET), BAS_SRCDIR=$(BAS_SRCDIR) does not exist."; exit 2; \
	fi


# Hook to run a custom untar step
$(TARGET)_untar_: bas_custom_rsync

untar: untar_

patch: patch_

prepare: untar patch

config: config_
	$(Q)if [ -d $(DIR_FULL_PATH) ] && [ ! -e $(DIR_FULL_PATH)/.stamp_configured ]; then \
		cd $(DIR_FULL_PATH) && VERBOSE=1 ./configure.sh $(BAS_DEBUG_FLAGS) -p /usr/local/bas -v cm -a $(BR2_ARCH) -g $(BAS_SRCDIR) -l || exit 2; \
		cd $(DIR_FULL_PATH) && VERBOSE=1 ./configure.sh $(BAS_DEBUG_FLAGS) -p /usr/local/bas -v wifi -a $(BR2_ARCH) -g $(BAS_SRCDIR) -l || exit 2; \
		touch $(DIR_FULL_PATH)/.stamp_configured; \
	fi

build: config build_
	mkdir -p $(DIR_FULL_PATH)/staging/out.cm
	mkdir -p $(DIR_FULL_PATH)/staging/out.wifi
	cd $(DIR_FULL_PATH) && ./build.sh -v -e cm -d $(DIR_FULL_PATH)/staging/out.cm || exit 2;
	cd $(DIR_FULL_PATH) && ./build.sh -v -e wifi -d $(DIR_FULL_PATH)/staging/out.wifi || exit 2;
	############################
	# create a precompiled binaries tarball
	############################
	$(Q)mkdir -p $(DIR_FULL_PATH)/staging/out.bas_binaries
ifeq ($(BAS_DEBUG_LEVEL),2)
	# copy all the compiler's libasan libraries, based on the path of the $CC
	find $(shell echo $$(dirname $$(which $(CC))))/../ $(CONFIG_LIBGCC_ROOT_DIR) -path *root*/lib/libasan.so* -exec cp -a {} $(DIR_FULL_PATH)/staging/out.bas_binaries \;
endif
	# copy all the opensource libraries, and bas binaries
	$(Q)cd $(STAGING_DIR)$(BAS_PREFIX); cp -a lib*.so* $(DIR_FULL_PATH)/staging/out.bas_binaries
	# copy BAS launcher, BAS daemon and BAS RG client
	$(Q)cd $(DIR_FULL_PATH)/staging/out.cm$(BAS_PREFIX); cp -a lib*.so* bas basl rgapp  $(DIR_FULL_PATH)/staging/out.bas_binaries
	# stage BAS wifi client
	$(Q)cd $(DIR_FULL_PATH)/staging/out.wifi$(BAS_PREFIX); cp -a bas_wifi_client  $(DIR_FULL_PATH)/staging/out.bas_binaries
	# remove the unused libraries
	$(Q)cd $(DIR_FULL_PATH)/staging/out.bas_binaries; rm -f libpaho-mqtt3c.* libpaho-mqtt3a.* libpaho-mqtt3as.*
	# add the default iperf3 symlink
	$(Q)cd $(DIR_FULL_PATH)/staging/out.bas_binaries; ln -sf $(IPERF3_EXE) iperf
	# create tarball
	$(Q)mkdir -p $(DIR_FULL_PATH)/output
	tar -cz -C $(DIR_FULL_PATH)/staging/out.bas_binaries -f $(DIR_FULL_PATH)/output/bas_binaries.tgz .
	$(Q)$(RM) -r $(DIR_FULL_PATH)/staging/out.bas_binaries
	############################

release: release_
	cp $(DIR_FULL_PATH)/output/bas_binaries.tgz $(RELEASE_DIR)/bas_binaries.tgz

stage: stage_
	mkdir -p $(STAGING_DIR)$(BAS_PREFIX)
	tar -C $(STAGING_DIR)$(BAS_PREFIX) -xzf $(RELEASE_DIR)/bas_binaries.tgz
	### update the BAS's iperf symlink - in case a different iperf package is used
	@if [ -d "$(STAGING_DIR)" ]; then \
		iperf3=$$(find $(STAGING_DIR) -name iperf3 -type f -executable | head -1 | sed -e 's,^$(STAGING_DIR),,' ); \
		if [ -n "$${iperf3}" ]; then \
			echo "iperf3 is found - $${iperf3}"; \
			cd $(STAGING_DIR)$(BAS_PREFIX) && ln -sf $${iperf3} iperf; \
		else \
			echo "ERROR!  No iperf3 is found in STAGING_DIR - $(STAGING_DIR)!"; \
			exit 1; \
		fi \
	fi

install: install_
	mkdir -p $(INSTALL_DIR)$(BAS_PREFIX)
	tar -C $(INSTALL_DIR)$(BAS_PREFIX) -xzf $(RELEASE_DIR)/bas_binaries.tgz
	### update the BAS's iperf symlink - in case a different iperf package is used
	@if [ -d "$(INSTALL_DIR)" ]; then \
		iperf3=$$(find $(INSTALL_DIR) -name iperf3 -type f -executable | head -1 | sed -e 's,^$(INSTALL_DIR),,' ); \
		if [ -n "$${iperf3}" ]; then \
			echo "iperf3 is found - $${iperf3}"; \
			cd $(INSTALL_DIR)$(BAS_PREFIX) && ln -sf $${iperf3} iperf; \
		else \
			echo "ERROR!  No iperf3 is found in INSTALL_DIR - $(INSTALL_DIR)!"; \
			exit 1; \
		fi \
	fi

clean: clean_
	- @rm -rf $(DIR)
	- @rm -rf .release_$(ARCH)

clean_config:
	- @rm $(DIR_FULL_PATH)/.stamp_configured

distclean: distclean_


# Build thru custom
# Common Makefiles
# ------------------------------------------------------------------------------
include $(COMMON_MAKEFILES_DIR)/make.custom
