#! /usr/bin/make -f
#
#	debian/rules file for sysvinit
#

# Uncomment this to turn on verbose mode.
# export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

tmp     = $(shell pwd)/debian/tmp
sysvtmp = $(shell pwd)/debian/sysvinit
utiltmp = $(shell pwd)/debian/sysvinit-utils
rctmp   = $(shell pwd)/debian/sysv-rc
inittmp = $(shell pwd)/debian/initscripts
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)

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

PATCH_STAMP=debian/stamp-patched
patch: $(PATCH_STAMP)
$(PATCH_STAMP):
	dh_testdir debian/patches/series \
		debian/startpar/patches/series
	QUILT_PATCHES=debian/patches quilt --quiltrc /dev/null push -a || test $$? = 2
	cd debian/startpar && \
		QUILT_PATCHES=patches quilt --quiltrc /dev/null push -a || test $$? = 2
	touch $(PATCH_STAMP)

unpatch:
	QUILT_PATCHES=debian/patches quilt --quiltrc /dev/null pop -a -R || test $$? = 2
	cd debian/startpar && \
		QUILT_PATCHES=patches quilt --quiltrc /dev/null pop -a -R || test $$? = 2
	rm -rf .pc debian/startpar/.pc $(PATCH_STAMP)

build: build-stamp
build-stamp: patch
# Builds the binary package.
	dh_testdir
	$(MAKE) $(CROSS) $(CONFFLAGS) -C src DISTRO=Debian
	$(MAKE) $(CROSS) -C debian/startpar
	touch $@

# Make a binary package (.deb file)
binary-arch: DH_OPTIONS=-a
binary-arch: build
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) -C src $(CROSS) ROOT=$(tmp) DISTRO=Debian install
	dh_install
	dh_link
	# Used by sysvinit and sysvinit-utils
	cat debian/copyright.in COPYRIGHT > debian/copyright
	#
	# 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/
ifeq ($(DEB_HOST_ARCH), hurd-i386)
	# The Hurd has its own halt and reboot commands.
	rm -f $(sysvtmp)/usr/share/man/man8/halt.8
	rm -f $(sysvtmp)/usr/share/man/man8/reboot.8
	rm -f $(sysvtmp)/sbin/halt
	rm -f $(sysvtmp)/sbin/reboot
endif
	dh_installchangelogs -psysvinit doc/Changelog
	dh_installdeb -psysvinit
	#
	# sysvinit-utils package
	#
	$(MAKE) $(CROSS) -C debian/startpar DESTDIR=$(utiltmp) install
	dh_installchangelogs -psysvinit-utils doc/Changelog
	sed -i -ne '/sysvinit (2.86.ds1-47)/q' -e p \
		$(inittmp)$(doc)/initscripts/changelog.Debian
	#
	# initscripts package
	#
	$(MAKE) -C debian/src/initscripts install DESTDIR=$(inittmp)
ifneq (,$(findstring $(DEB_HOST_ARCH_OS),linux kfreebsd))
	$(INSTALL) -d $(inittmp)/sys
endif
	dh_installchangelogs -pinitscripts
	sed -i -ne '/sysvinit (2.86.ds1-47)/q' -e p \
		$(inittmp)$(doc)/initscripts/changelog.Debian
	dh_installdeb -pinitscripts
	# Override autogenerated conffiles
	$(INSTALL_DATA) debian/initscripts.conffiles \
		$(inittmp)/DEBIAN/conffiles
	sh debian/deps-mount >> debian/initscripts.substvars

	# Wrap it all up
	dh_fixperms -X/etc/init.d/skeleton
	dh_installman
	dh_installdocs
	dh_lintian
	dh_shlibdeps
	dh_strip
	dh_compress
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Architecture independant files.
binary-indep: DH_OPTIONS=-i
binary-indep:
	#
	# sysv-rc package
	#
	dh_testroot
	dh_prep
	$(MAKE) -C debian/src/sysv-rc install DESTDIR=$(rctmp)
	dh_installchangelogs
	sed -i -ne '/sysvinit (2.86.ds1-47)/q' -e p \
		$(rctmp)$(doc)/sysv-rc/changelog.Debian
	dh_installdeb
	# Neither rc, rcS nor README are conffiles
	$(RM) $(rctmp)/DEBIAN/conffiles

	# Wrap it all up
	dh_fixperms -X/etc/init.d/README
	dh_installdebconf
	dh_installdocs
	dh_installman
	dh_lintian
	dh_compress
	dh_gencontrol
	dh_md5sums
	dh_builddeb

clean:
	dh_testdir
	$(MAKE) -C src clobber
	$(MAKE) -C debian/startpar clean
	$(MAKE) -f debian/rules unpatch
	dh_clean build-stamp debian/copyright

binary: binary-arch binary-indep

.PHONY: binary binary-arch binary-indep clean
