#! /usr/bin/make -f

tmp     = $(CURDIR)/debian/tmp
sysvtmp = $(CURDIR)/debian/sysvinit
utiltmp = $(CURDIR)/debian/sysvinit-utils
rctmp   = $(CURDIR)/debian/sysv-rc
inittmp = $(CURDIR)/debian/initscripts
bootlogdtmp = $(CURDIR)/debian/bootlogd
doc	= /usr/share/doc

LC_ALL	= POSIX

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_GNU_SYSTEM ?= $(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM)
DEB_HOST_ARCH       ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS    ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_MULTIARCH  ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

INSTALL_DATA = install -o root -g root -m 644
INSTALL      = install -o root -g root -m 755

# Handle cross builds
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS=CC=$(DEB_HOST_GNU_TYPE)-gcc
# CROSS is passed to make, CC is called directly.
CC=$(DEB_HOST_GNU_TYPE)-gcc
endif

# Set arch specific build flags
ifeq ($(DEB_HOST_ARCH_OS),linux)
CONFFLAGS = WITH_SELINUX="yes"
endif

DH_OPTIONS =

# Use debhelper's dh
%:
	dh $@ $(DH_OPTIONS)

override_dh_auto_build:
	$(MAKE) $(CROSS) $(CONFFLAGS) -C src DISTRO=Debian LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH) CFLAGS="$(CFLAGS) $(CPPFLAGS)" LDFLAGS="$(LDFLAGS)"
	$(MAKE) $(CROSS) COPTS="$(CFLAGS) $(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" -C startpar
ifeq ($(DEB_HOST_ARCH_OS),linux)
	$(MAKE) $(CROSS) COPTS="$(CFLAGS) $(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" -C startpar startpar-upstart-inject
endif

override_dh_auto_install-arch:
	$(MAKE) $(CROSS) -C startpar DESTDIR=$(utiltmp) install
ifeq ($(DEB_HOST_ARCH_OS),linux)
	install -m755 startpar/startpar-upstart-inject $(utiltmp)/sbin/
endif
	$(MAKE) -C debian/src/bootlogd install DESTDIR=$(bootlogdtmp)
	$(MAKE) -C debian/src/initscripts install DESTDIR=$(inittmp)
	$(MAKE) -C src $(CROSS) ROOT=$(tmp) DISTRO=Debian install

override_dh_auto_install-indep:
	$(MAKE) -C debian/src/sysv-rc install DESTDIR=$(rctmp)

override_dh_install-arch:
	dh_install

ifneq (,$(filter sysvinit, $(shell dh_listpackages)))
	# sysvinit package
	if test -e debian/share/inittab.$(DEB_HOST_GNU_TYPE) ; \
	then \
		$(INSTALL_DATA) \
			debian/share/inittab.$(DEB_HOST_GNU_TYPE) \
			$(sysvtmp)/usr/share/sysvinit/inittab ; \
	elif test -e debian/share/inittab.$(DEB_HOST_GNU_SYSTEM) ; \
	then \
		$(INSTALL_DATA) \
			debian/share/inittab.$(DEB_HOST_GNU_SYSTEM) \
			$(sysvtmp)/usr/share/sysvinit/inittab ; \
	else \
		$(INSTALL_DATA) debian/share/inittab \
			$(sysvtmp)/usr/share/sysvinit/inittab ; \
	fi

	$(INSTALL) debian/share/update-rc.d $(sysvtmp)/usr/share/sysvinit/
endif

	# initscripts package may include /sys
ifneq (,$(findstring $(DEB_HOST_ARCH_OS),linux kfreebsd))
	$(INSTALL) -d $(inittmp)/sys
endif

debian/copyright: debian/copyright.in
	cat $< COPYRIGHT > $@

install-arch: build-arch debian/copyright
	dh $@ $(DH_OPTIONS)

override_dh_installchangelogs-arch:
	dh_installchangelogs -psysvinit doc/Changelog

	dh_installchangelogs -psysvinit-utils doc/Changelog
	sed -i -ne '/sysvinit (2.86.ds1-47)/q' -e p \
		$(utiltmp)$(doc)/sysvinit-utils/changelog.Debian

	dh_installchangelogs -pinitscripts
	sed -i -ne '/sysvinit (2.86.ds1-47)/q' -e p \
		$(inittmp)$(doc)/initscripts/changelog.Debian

	dh_installchangelogs -pbootlogd
	sed -i -ne '/sysvinit (2.86.ds1-47)/q' -e p \
		$(bootlogdtmp)$(doc)/bootlogd/changelog.Debian

override_dh_installchangelogs-indep:
	dh_installchangelogs
	sed -i -ne '/sysvinit (2.86.ds1-47)/q' -e p \
		$(rctmp)$(doc)/sysv-rc/changelog.Debian

override_dh_fixperms:
	dh_fixperms -X/etc/init.d/skeleton -X/etc/init.d/README

override_dh_installdeb-arch:
	dh_installdeb

	# Override autogenerated conffiles
	$(INSTALL_DATA) debian/initscripts.conffiles \
		$(inittmp)/DEBIAN/conffiles
	sh debian/deps-mount >> debian/initscripts.substvars

ifeq ($(DEB_HOST_ARCH_OS), hurd)
	# The Hurd has its own halt and reboot commands.
	mv $(sysvtmp)/usr/share/man/man8/halt.8.gz $(sysvtmp)/usr/share/man/man8/halt-sysv.8.gz
	rm $(sysvtmp)/usr/share/man/man8/reboot.8.gz
	ln -s halt-sysv.8.gz $(sysvtmp)/usr/share/man/man8/reboot-sysv.8.gz
	mv $(sysvtmp)/sbin/halt $(sysvtmp)/sbin/halt-sysv
	rm $(sysvtmp)/sbin/reboot
	ln -s halt-sysv $(sysvtmp)/sbin/reboot-sysv
endif

override_dh_installdeb-indep:
	dh_installdeb

	# Neither rc, rcS nor README are conffiles
	$(RM) $(rctmp)/DEBIAN/conffiles

override_dh_installinit-arch:
	dh_installinit --name startpar-bridge -n

override_dh_clean:
	dh_clean debian/copyright
	$(MAKE) -C src clobber
	if [ -d startpar ] && [ -f startpar/Makefile ] ; then \
		$(MAKE) -C startpar clean ; \
	fi
ifeq ($(DEB_HOST_ARCH_OS),linux)
	rm -f startpar/startpar-upstart-inject
endif

.PHONY: override_dh_auto_build override_dh_auto_install-arch override_dh_auto_install-indep override_dh_installchangelogs-arch override_dh_installchangelogs-indep override_dh_fixperms override_dh_installdeb-arch override_dh_installdeb-indep override_dh_clean install-arch
