#!/usr/bin/make -f

SHELL := sh -e
DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_GNU_TYPE := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
SOURCE := $(shell dpkg-parsechangelog | sed -ne 's,^Source: *\(.*\)$$,\1,p')
VERSION_DEBIAN := $(shell dpkg-parsechangelog | sed -ne 's,^Version: *\(.*\)$$,\1,p')
VERSION := $(shell echo "$(VERSION_DEBIAN)" | sed -e 's,^[^:]*:,,' -e 's,-[^-]*$$,,')
VENDOR := $(shell dpkg-vendor --query vendor)

BUILD_DIR = debian/build

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
export CROSS_COMPILE = $(DEB_HOST_GNU_TYPE)-
endif

build: build-arch build-indep
build-indep:

setup: $(BUILD_DIR)/deb/.setup $(BUILD_DIR)/static/.setup $(BUILD_DIR)/udeb/.setup

$(BUILD_DIR)/%/.setup: SOURCE_FILES = $(filter-out debian, $(wildcard *))
$(BUILD_DIR)/%/.setup: DIR = $(BUILD_DIR)/$*
$(BUILD_DIR)/%/.setup: debian/config/pkg/% debian/config/os/$(DEB_HOST_ARCH_OS)
	dh_testdir
	rm -rf '$(DIR)'
	mkdir -p '$(DIR)'
	cp -a -l $(SOURCE_FILES) '$(DIR)'

# Generate the .config file. First variable assignment wins.
# Several steps..

# 1, rm it just in case
	rm -f $(DIR)/.config

# 2, put any arch-specific overrides so they will have higher precedence
# should be empty for linux
	cat debian/config/os/$(DEB_HOST_ARCH_OS) > $(DIR)/.config

# 3, on non-linux platform, disable all config symbols
# which select PLATFORM_LINUX.
# Configuration information is stored in source files and in Config.src,
# so we have to generate Config.in files first, -- done by gen_build_files.
	$(MAKE) -C $(DIR) gen_build_files
	if [ linux != $(DEB_HOST_ARCH_OS) ]; then \
	  find $(DIR) -name Config.in -exec \
	    awk '$$1 == "config" { conf = $$2 } \
	         $$1 == "select" && $$2 == "PLATFORM_LINUX" \
	         { print "CONFIG_" conf "=n"; }' \
	     {} + \
          | sort -u >> $(DIR)/.config; \
	fi

# 4, use the flavour-specific base config file.
	cat debian/config/pkg/$* >> $(DIR)/.config

# 5, sort out the resulting mess using oldconfig Kbuild machinery
	$(MAKE) -C $(DIR) oldconfig

	ln -s .config $@

build-arch: $(BUILD_DIR)/deb/.built $(BUILD_DIR)/static/.built $(BUILD_DIR)/udeb/.built

$(BUILD_DIR)/%/.built: DIR = $(BUILD_DIR)/$*
$(BUILD_DIR)/%/.built: $(BUILD_DIR)/%/.setup
	dh_testdir
	$(MAKE) -C '$(DIR)' SKIP_STRIP=y install docs/busybox.1 BB_EXTRA_VERSION="$(VENDOR) $(VERSION_DEBIAN)"
	touch $@

clean:
	dh_testdir
	rm -rf $(BUILD_DIR)
	dh_clean

binary-indep: binary-indep_busybox-syslogd binary-indep_udhcpc binary-indep_udhcpd

binary-arch: binary-arch_busybox binary-arch_busybox-static binary-arch_busybox-udeb

binary-arch_busybox: DIR = $(BUILD_DIR)/deb
binary-arch_busybox: export DH_OPTIONS = -pbusybox
binary-arch_busybox: $(BUILD_DIR)/deb/.built
	dh_testdir
	dh_testroot
	dh_prep
	dh_install --sourcedir=$(DIR)
	@$(MAKE) -f debian/rules binary-arch_all

binary-arch_busybox-static: DIR = $(BUILD_DIR)/static
binary-arch_busybox-static: export DH_OPTIONS = -pbusybox-static
binary-arch_busybox-static: $(BUILD_DIR)/static/.built
	dh_testdir
	dh_testroot
	dh_prep
	dh_install --sourcedir=$(DIR)
	@$(MAKE) -f debian/rules binary-arch_all

binary-arch_busybox-udeb: DIR = $(BUILD_DIR)/udeb
binary-arch_busybox-udeb: export DH_OPTIONS = -pbusybox-udeb
binary-arch_busybox-udeb: $(BUILD_DIR)/udeb/.built
	dh_testdir
	dh_testroot
	dh_prep
	# Remove init link, but init support is still compiled in to be
	# used.
	rm -f $(DIR)/_install/sbin/init
	dh_install --sourcedir=$(DIR)
	@$(MAKE) -f debian/rules binary-arch_all

binary-arch_all:
	dh_installdirs
	dh_installdocs
	dh_installchangelogs
	dh_strip
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep_busybox-syslogd: export DH_OPTIONS = -pbusybox-syslogd
binary-indep_busybox-syslogd:
	dh_testdir
	dh_testroot
	dh_prep
	dh_link
	dh_installinit -u"defaults 10 90"
	dh_installinit -u"defaults 11 89" --name=busybox-klogd
	$(MAKE) -f debian/rules binary-indep_all

binary-indep_udhcpc: export DH_OPTIONS = -pudhcpc
binary-indep_udhcpc:
	dh_testdir
	dh_testroot
	dh_prep
	dh_link
	dh_install
	$(MAKE) -f debian/rules binary-indep_all

binary-indep_udhcpd: export DH_OPTIONS = -pudhcpd
binary-indep_udhcpd:
	dh_testdir
	dh_testroot
	dh_prep
	dh_link
	dh_installinit --onlyscripts
	dh_install
	$(MAKE) -f debian/rules binary-indep_all

binary-indep_all:
	dh_installdirs
	dh_installdocs
	dh_installchangelogs
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

