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

tmp     = $(shell pwd)/debian/tmp
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)
export DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS 2>/dev/null || true)

# 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
else
CC=cc
endif

# Handle fact that sarge dpkg-architecture doesn't have DEB_HOST_ARCH_OS
ifeq ($(DEB_HOST_ARCH_OS),)
  DEB_HOST_ARCH_OS := $(subst -gnu,,$(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM))
  ifeq ($(DEB_HOST_ARCH_OS),gnu)
    DEB_HOST_ARCH_OS := hurd
  endif
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
ifeq ($(DEB_HOST_ARCH_OS),linux)
	$(MAKE) -C src $(CROSS) DISTRO=Debian WITH_SELINUX="yes"
else
	$(MAKE) -C src $(CROSS) DISTRO=Debian
endif
	$(MAKE) $(CROSS) -C debian/startpar
	$(CC) -W -Wall -s -o debian/readlink debian/readlink.c
	touch $@

# Make a binary package (.deb file)
binary-arch:	build
	dh_testroot
	#
	# sysvinit-utils package
	#
	-rm -rf $(tmp)
	install -d -o root -g root -m 755 $(tmp)
	install -d -o root -g root -m 755 $(tmp)/DEBIAN
	install -d -o root -g root -m 755 $(tmp)$(doc)/sysvinit-utils
	install -o root -g root -m 644 doc/Changelog \
		$(tmp)$(doc)/sysvinit-utils/changelog
	install -o root -g root -m 644 debian/changelog \
		$(tmp)$(doc)/sysvinit-utils/changelog.Debian
	gzip -9f $(tmp)$(doc)/sysvinit-utils/changelog*
	install -d -o root -g root -m 755 $(tmp)/bin
	install -d -o root -g root -m 755 $(tmp)/sbin
	install -d -o root -g root -m 755 $(tmp)/usr
	install -d -o root -g root -m 755 $(tmp)/usr/bin
	install -d -o root -g root -m 755 $(tmp)/usr/sbin
	install -d -o root -g root -m 755 $(tmp)/usr/share/man
	install -d -o root -g root -m 755 $(tmp)/usr/share/man/man1
	install -d -o root -g root -m 755 $(tmp)/usr/share/man/man8
	install -o root -g root -m 755 src/killall5 $(tmp)/sbin
	install -o root -g root -m 755 src/sulogin $(tmp)/sbin
	install -o root -g root -m 755 src/fstab-decode $(tmp)/sbin
	install -o root -g root -m 755 src/bootlogd $(tmp)/sbin
	install -o root -g root -m 755 src/last $(tmp)/usr/bin
	install -o root -g root -m 755 src/mesg $(tmp)/usr/bin
	$(MAKE) $(CROSS) -C debian/startpar DESTDIR=$(tmp) install
	strip -s -R comment -R .comment $(tmp)/sbin/startpar
	install -o root -g root -m 755 debian/service/service $(tmp)/usr/sbin
	ln -sf /usr/sbin/service $(tmp)/usr/bin
	strip -s -R .comment $(tmp)/sbin/killall5
	strip -s -R .comment $(tmp)/sbin/sulogin
	strip -s -R .comment $(tmp)/usr/bin/last
	strip -s -R .comment $(tmp)/usr/bin/mesg
	ln -sf /sbin/killall5 $(tmp)/bin/pidof
	ln -sf last $(tmp)/usr/bin/lastb
	install -o root -g root -m 644 man/killall5.8 $(tmp)/usr/share/man/man8
	install -o root -g root -m 644 man/sulogin.8 $(tmp)/usr/share/man/man8
	install -o root -g root -m 644 man/bootlogd.8 $(tmp)/usr/share/man/man8
	install -o root -g root -m 644 man/last.1 $(tmp)/usr/share/man/man1
	install -o root -g root -m 644 man/lastb.1 $(tmp)/usr/share/man/man1
	install -o root -g root -m 644 man/mesg.1 $(tmp)/usr/share/man/man1
	install -o root -g root -m 644 debian/service/service.8 $(tmp)/usr/share/man/man8
	install -o root -g root -m 644 man/pidof.8 $(tmp)/usr/share/man/man8
	install -o root -g root -m 644 man/fstab-decode.8 $(tmp)/usr/share/man/man8
	gzip -9f $(tmp)/usr/share/man/man*/*.[0-9]
	install -o root -g root -m 644 debian/copyright \
	  $(tmp)$(doc)/sysvinit-utils/copyright
	cat COPYRIGHT >> $(tmp)$(doc)/sysvinit-utils/copyright
	dpkg-shlibdeps src/init debian/startpar/startpar
	dpkg-gencontrol -psysvinit-utils -P$(tmp) -isp
	dh_md5sums -Ninitscripts -Nsysv-rc
	dpkg --build $(tmp) ..
	rm -rf $(tmp)
	#
	# initscripts package
	#
	-rm -rf $(tmp)
	install -d -o root -g root -m 755 $(tmp)
	install -d -o root -g root -m 755 $(tmp)/DEBIAN
	install -d -o root -g root -m 755 $(tmp)/sbin
	install -d -o root -g root -m 755 $(tmp)/lib
	install -d -o root -g root -m 755 $(tmp)/etc
ifneq (,$(findstring $(DEB_HOST_ARCH_OS),linux kfreebsd))
	install -d -o root -g root -m 755 $(tmp)/sys
endif
	install -d -o root -g root -m 755 $(tmp)/lib/init
	install -d -o root -g root -m 755 $(tmp)/lib/init/rw
	install -d -o root -g root -m 755 $(tmp)/usr/share/initscripts
	install -d -o root -g root -m 755 $(tmp)/var/lib/initscripts
	install -d -o root -g root -m 755 $(tmp)/var/log/fsck
	install -d -o root -g root -m 755 $(tmp)$(doc)/initscripts
	install -o root -g root -m 644 debian/initscripts/doc/* $(tmp)$(doc)/initscripts
	sed -ne '/sysvinit (2.84-3)/q' -e p < debian/changelog \
		> $(tmp)$(doc)/initscripts/changelog.Debian
	chmod 644 $(tmp)$(doc)/initscripts/changelog.Debian
	gzip -9f $(tmp)$(doc)/initscripts/*
	cp -afv debian/initscripts/share/* $(tmp)/usr/share/initscripts
	cp -afv debian/initscripts/etc/* $(tmp)/etc
	cp -afv debian/initscripts/lib/* $(tmp)/lib
	chmod 755 $(tmp)/etc/init.d/[a-z]*
	chmod 644 $(tmp)/lib/init/*.sh
	chmod 644 $(tmp)/etc/init.d/skeleton
	chmod -R g-w $(tmp)
	chown -R root:root $(tmp)
	install -d -o root -g root -m 755 $(tmp)/bin
	install -d -o root -g root -m 755 $(tmp)/etc/default
	install -d -o root -g root -m 755 $(tmp)/usr/share/man/man1
	install -d -o root -g root -m 755 $(tmp)/usr/share/man/man5
	install -d -o root -g root -m 755 $(tmp)/usr/share/man/man8
	install -d -o root -g root -m 755 $(tmp)/var/lib/urandom
	install -o root -g root -m 755 debian/initscripts/sbin/fsck.nfs \
		$(tmp)/sbin/fsck.nfs
	install -o root -g root -m 644 debian/initscripts/man/fsck.nfs.8 \
		$(tmp)/usr/share/man/man8/fsck.nfs.8
	install -o root -g root -m 644 debian/initscripts/man/halt.5 \
		$(tmp)/usr/share/man/man5/halt.5
	install -o root -g root -m 644 debian/initscripts/rcS.5 \
		$(tmp)/usr/share/man/man5
	install -o root -g root -m 755 src/mountpoint $(tmp)/bin
	strip -s -R .comment $(tmp)/bin/mountpoint
	install -o root -g root -m 644 man/mountpoint.1 $(tmp)/usr/share/man/man1
	install -o root -g root -m 755 debian/readlink $(tmp)/lib/init
	strip -s -R .comment $(tmp)/lib/init/readlink
	gzip -9f $(tmp)/usr/share/man/man*/*.[0-9]
	install -d -o root -g root -m 755 $(tmp)/usr/share/lintian/overrides
	install -o root -g root -m 644 \
		debian/initscripts.lintian.overrides \
		$(tmp)/usr/share/lintian/overrides/initscripts
	install -o root -g root -m 644 debian/initscripts.copyright \
	  $(tmp)$(doc)/initscripts/copyright
	install -o root -g root -m 755 debian/initscripts.preinst  $(tmp)/DEBIAN/preinst
	install -o root -g root -m 755 debian/initscripts.postinst $(tmp)/DEBIAN/postinst
	install -o root -g root -m 755 debian/initscripts.postrm $(tmp)/DEBIAN/postrm
	install -o root -g root -m 644 debian/initscripts.conffiles \
	  $(tmp)/DEBIAN/conffiles
	dpkg-shlibdeps debian/readlink
	sh debian/deps-mount >> debian/substvars
	sh debian/deps-glibc >> debian/substvars
	dpkg-gencontrol -pinitscripts -P$(tmp) -isp
	dh_md5sums -Ninitscripts -Nsysv-rc
	dpkg --build $(tmp) ..
	rm -rf $(tmp)

# Architecture independant files.
binary-indep: build
	#
	# sysv-rc package
	#
	-rm -rf $(tmp)
	install -d -o root -g root -m 755 $(tmp)/DEBIAN
	install -d -o root -g root -m 755 $(tmp)/etc
	install -d -o root -g root -m 755 $(tmp)/usr/share
	install -d -o root -g root -m 755 $(tmp)$(doc)/sysv-rc
	install -d -o root -g root -m 755 $(tmp)/usr/share/sysv-rc
	install -d -o root -g root -m 755 $(tmp)/var/lib/insserv
	install -d -o root -g root -m 755 $(tmp)/var/lib/update-rc.d
	install -o root -g root -m 644 debian/sysv-rc/doc/* $(tmp)$(doc)/sysv-rc
	sed -ne '/sysvinit (2.84-3)/q' -e p < debian/changelog \
		> $(tmp)$(doc)/sysv-rc/changelog.Debian
	chmod 644 $(tmp)$(doc)/sysv-rc/changelog.Debian
	gzip -9f $(tmp)$(doc)/sysv-rc/*
	cp -af debian/sysv-rc/etc/* $(tmp)/etc
	for N in 2 3 4 5 ; do \
		install -d -o root -g root -m 755 $(tmp)/etc/rc$${N}.d ; \
		install -o root -g root -m 644 debian/sysv-rc/rc2-5.d-README \
			$(tmp)/etc/rc$${N}.d/README ; \
	done
	chmod 755 $(tmp)/etc/init.d/[a-z]*
	chmod 644 $(tmp)/etc/init.d/README
	chmod -R go=u-w $(tmp)/etc
	chown -R root:root $(tmp)/etc
	install -d -o root -g root -m 755 $(tmp)/usr/sbin
	install -d -o root -g root -m 755 $(tmp)/usr/share/man/man8
#	install -d -o root -g root -m 755 $(tmp)/usr/share/man/ja/man8
#	install -d -o root -g root -m 755 $(tmp)/usr/share/man/fr.UTF-8/man8
#	install -d -o root -g root -m 755 $(tmp)/usr/share/man/es/man8
	install -o root -g root -m 644 debian/sysv-rc/man8/*.8 \
		$(tmp)/usr/share/man/man8
#	install -o root -g root -m 644 debian/sysv-rc/man8/ja/*.8 \
#		$(tmp)/usr/share/man/ja/man8
#	install -o root -g root -m 644 debian/sysv-rc/man8/fr.UTF-8/*.8 \
#		$(tmp)/usr/share/man/fr.UTF-8/man8
#	install -o root -g root -m 644 debian/sysv-rc/man8/es/*.8 \
#		$(tmp)/usr/share/man/es/man8
	install -o root -g root -m 755 debian/sysv-rc/sbin/invoke-rc.d $(tmp)/usr/sbin
	install -o root -g root -m 755 debian/sysv-rc/sbin/update-rc.d $(tmp)/usr/sbin
	gzip -9f $(tmp)/usr/share/man/man*/*.[0-9]
#	gzip -9f $(tmp)/usr/share/man/??/man*/*.[0-9]
#	gzip -9f $(tmp)/usr/share/man/??.*/man*/*.[0-9]
	install -d -o root -g root -m 755 $(tmp)/usr/share/lintian/overrides
	install -o root -g root -m 644 \
		debian/sysv-rc.lintian.overrides \
		$(tmp)/usr/share/lintian/overrides/sysv-rc
	install -o root -g root -m 644 debian/sysv-rc.copyright \
	  $(tmp)$(doc)/sysv-rc/copyright
	po2debconf debian/sysv-rc.templates > $(tmp)/DEBIAN/templates
	for f in preinst postinst prerm postrm ; do \
		install -o root -g root -m 755 debian/sysv-rc.$$f $(tmp)/DEBIAN/$$f ; \
	done
	install -o root -g root -m 755 debian/sysv-rc/saveconfig \
		$(tmp)/usr/share/sysv-rc/saveconfig
	dpkg-gencontrol -psysv-rc -P$(tmp) -isp
	dh_md5sums -Ninitscripts -Nsysv-rc
	dpkg --build $(tmp) ..
	rm -rf $(tmp)

	# sysvutils transitional package
	# keep until after next LTS
	install -d $(tmp)/DEBIAN
	dpkg-gencontrol -psysvutils -P$(tmp) -isp
	dpkg --build $(tmp) ..
	rm -rf $(tmp)

clean:
	dh_testdir
	$(MAKE) -C src clobber
	$(MAKE) -C debian/startpar clean
	$(MAKE) -f debian/rules unpatch
	dh_clean -Xdebian/initscripts -Xdebian/sysv-rc \
		build-stamp debian/readlink

binary: binary-arch binary-indep

.PHONY: binary binary-arch binary-indep clean
