TOPDIR := $(CURDIR)
DEFCONFIGS_DIR := $(CURDIR)/configs
BUILD_DIRECTORY := $(CURDIR)/output/build
IMAGES_DIRECTORY := $(CURDIR)/output/images

default: all

nconfig:
	kconfig-nconf Config.in

menuconfig:
	kconfig-mconf Config.in

config:
	kconfig-conf Config.in

py-menuconfig:
	scripts/menuconfig Config.in

include scripts/funcs.mk
include package/package.mk
include support/config/config.mk

Command := $(firstword $(MAKECMDGOALS))

UC = $(shell echo $(Command) | tr '[:lower:]' '[:upper:]')

# $(Command):
# 	$(call CMD_CONFIGURE_$(call UC))
# 	$(call CMD_BUILD_$(call UC))
# 	$(call CMD_INSTALL_$(call UC))

all:
	$(call download,busybox)
	$(call CMD_CONFIGURE_BUSYBOX)
	$(call CMD_BUILD_BUSYBOX)
	$(call CMD_INSTALL_BUSYBOX)

list-defconfigs:
	$(call list-defconfigs,$(TOPDIR))

%_defconfig:
	$(call msg,Copy defconfig: $@ to .config)
	$(if \
		$(wildcard $(DEFCONFIGS_DIR)/$@),\
		$(call copy_file,$(DEFCONFIGS_DIR)/$@,$(TOPDIR)/.config),\
		$(call errmsg,Can't find $@)\
	)
	$(call successmsg,Copy defconfig: $@ to .config is OK)

%-dirclean:
	$(call msg,Deleting: $(call split-,$@,1) build directory)
	$(if \
		$(wildcard $(TOPDIR)/output/build/$(call split-,$@,1)), \
		$(call delete,$(TOPDIR)/output/build/$(call split-,$@,1)), \
		$(call msg,$(TOPDIR)/output/build/$(call split-,$@,1): not exists) \
	)
	$(call successmsg,Delete: $(call split-,$@,1) build directory)

source:
	$(call msg,Downloading busybox)
	$(call download,busybox)
	$(call successmsg,Download busybox)

distclean:
	@if [ -d $(TOPDIR)/dl ]; then \
		rm -rv dl; \
	fi
	@if [ -d $(TOPDIR)/output ]; then \
		rm -rv output; \
	fi

.PHONY: %_defconfig source %-dirclean list-defconfigs
