#!/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

# we filter out -Werror=format-security from CFLAGS, because
# bb uses constructs like
#  bb_error_msg_and_die(bb_msg_memory_exhausted);
# in many places, and for these, gcc with -Werror=format-security
# will complain, since bb_msg_memory_exhausted is an extern.
EXTRA_CFLAGS = \
 $(filter-out -Werror=format-security, $(shell dpkg-buildflags --get CFLAGS))
EXTRA_LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)
EXTRA_CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS)

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

setup: $(BUILD_DIR)/deb/.setup $(BUILD_DIR)/static/.setup $(BUILD_DIR)/udeb/.setup $(BUILD_DIR)/initramfs/.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
ifneq (linux,$(DEB_HOST_ARCH_OS))
	find $(DIR) -name Config.in -exec \
	  awk '$$1 == "config" { conf = $$2 } \
	       $$1 == "select" && $$2 == "PLATFORM_LINUX" \
	       { print "CONFIG_" conf "=n"; }' \
	   {} + \
        | sort -u >> $(DIR)/.config
endif

# 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)/initramfs/.built

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

clean:
	dh_testdir
	rm -rf $(BUILD_DIR)
	dh_clean

binary-indep: binary-indep_busybox-syslogd

binary-arch: binary-arch_busybox binary-arch_busybox-static binary-arch_busybox-udeb binary-arch_busybox-initramfs
ifeq (linux,$(DEB_HOST_ARCH_OS))
binary-arch: binary-arch_udhcpc binary-arch_udhcpd
endif

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)
	dh_installdocs docs/syslog.conf.txt docs/mdev.txt
	dh_installexamples examples/mdev* examples/udhcp
	@$(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)
	dh_installdocs docs/syslog.conf.txt docs/mdev.txt
	dh_installexamples examples/mdev* examples/udhcp
	@$(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_busybox-initramfs: DIR = $(BUILD_DIR)/initramfs
binary-arch_busybox-initramfs: export DH_OPTIONS = -pbusybox-initramfs
binary-arch_busybox-initramfs: $(BUILD_DIR)/initramfs/.built
	dh_testdir
	dh_testroot
	dh_prep
	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-arch_udhcpc: export DH_OPTIONS = -pudhcpc
binary-arch_udhcpc:
	dh_testdir
	dh_testroot
	dh_prep
	dh_link
	dh_install
	$(MAKE) -f debian/rules binary-indep_all

binary-arch_udhcpd: export DH_OPTIONS = -pudhcpd
binary-arch_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

