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

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)'
	cat debian/config/os/$(DEB_HOST_ARCH_OS) debian/config/pkg/$* > '$(DIR)'/.config
	$(MAKE) -C '$(DIR)' oldconfig
	ln -s .config $@

build: $(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 $@

maintainerclean:
	rm -rf $(filter-out .git debian, $(wildcard * .[^.]*))

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

