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

tmp     = $(shell pwd)/debian/tmp
doc	= /usr/share/doc

SHELL	= /bin/bash
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 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

LIBC6	= libc6
ifeq ($(DEB_HOST_ARCH), alpha)
LIBC6	= libc6.1
endif
ifeq ($(DEB_HOST_ARCH), ia64)
LIBC6	= libc6.1
endif

define checkdir
	test -f src/init.c
endef

build: patch-stamp
# Builds the binary package.
	$(checkdir)
ifeq ($(DEB_HOST_ARCH_OS),linux)
	(cd src; make DISTRO=Debian WITH_SELINUX="yes")
else
	(cd src; make DISTRO=Debian)
endif
	make -C debian/startpar
	cc -W -Wall -s -o debian/readlink debian/readlink.c
	touch build

# Make a binary package (.deb file)
binary-arch:	build checkroot
	#
	# sysvinit package
	#
	-rm -rf $(tmp)
	install -d -o root -g root -m 755 $(tmp)
	install -d -o root -g root -m 755 $(tmp)/{DEBIAN,etc}
	install -d -o root -g root -m 755 $(tmp)/usr/share/sysvinit
	install -d -o root -g root -m 755 $(tmp)/usr/include
	install -d -o root -g root -m 755 $(tmp)$(doc)/sysvinit
	install -o root -g root -m 644 doc/Changelog \
		$(tmp)$(doc)/sysvinit/changelog
	install -o root -g root -m 644 debian/changelog \
		$(tmp)$(doc)/sysvinit/changelog.Debian
	install -o root -g root -m 644 debian/NEWS \
		$(tmp)$(doc)/sysvinit/NEWS.Debian
	gzip -9f $(tmp)$(doc)/sysvinit/changelog*
	gzip -9f $(tmp)$(doc)/sysvinit/NEWS.Debian
	if test -e debian/share/inittab.$(DEB_HOST_GNU_TYPE) ; \
	then \
		install -o root -g root -m 644 \
			debian/share/inittab.$(DEB_HOST_GNU_TYPE) \
			$(tmp)/usr/share/sysvinit/inittab ; \
	elif test -e debian/share/inittab.$(DEB_HOST_GNU_SYSTEM) ; \
	then \
		install -o root -g root -m 644 \
			debian/share/inittab.$(DEB_HOST_GNU_SYSTEM) \
			$(tmp)/usr/share/sysvinit/inittab ; \
	else \
		install -o root -g root -m 644 debian/share/inittab \
			$(tmp)/usr/share/sysvinit/inittab ; \
	fi
	install -o root -g root -m 755 debian/share/update-rc.d \
		$(tmp)/usr/share/sysvinit
	install -d -o root -g root -m 755 $(tmp)/{bin,sbin,lib,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/man{1,5,8}
	(cd src; make ROOT=$(tmp) DISTRO=Debian install )
	make -C debian/startpar DESTDIR=$(tmp) install
	strip -s -R comment -R .comment $(tmp)/sbin/startpar
ifeq ($(DEB_HOST_ARCH), hurd-i386)
	# The Hurd has its own halt and reboot commands.
	rm -f $(tmp)/usr/share/man/man8/halt.8
	rm -f $(tmp)/usr/share/man/man8/reboot.8
	rm -f $(tmp)/sbin/halt
	rm -f $(tmp)/sbin/reboot
endif
	rm -f $(tmp)/usr/share/man/man8/killall5.8
	rm -f $(tmp)/usr/share/man/man8/sulogin.8
	rm -f $(tmp)/usr/share/man/man1/last.1
	rm -f $(tmp)/usr/share/man/man1/lastb.1
	rm -f $(tmp)/usr/share/man/man1/mesg.1
	rm -f $(tmp)/usr/share/man/man8/pidof.8
	rm -f $(tmp)/usr/share/man/man1/mountpoint.1
	rm -f $(tmp)/sbin/killall5
	rm -f $(tmp)/sbin/sulogin
	rm -f $(tmp)/usr/bin/last
	rm -f $(tmp)/usr/bin/lastb
	rm -f $(tmp)/usr/bin/mesg
	rm -f $(tmp)/bin/pidof
	rm -f $(tmp)/bin/mountpoint
	rmdir $(tmp)/usr/share/man/man1
	rmdir $(tmp)/usr/sbin
	rmdir $(tmp)/usr/bin
	rmdir $(tmp)/bin
	gzip -9f $(tmp)/usr/share/man/man*/*.[0-9]
	install -o root -g root -m 644 debian/copyright \
	  $(tmp)$(doc)/sysvinit/copyright
	cat COPYRIGHT >> $(tmp)$(doc)/sysvinit/copyright
	install -o root -g root -m 755 debian/postinst $(tmp)/DEBIAN/postinst
	dpkg-shlibdeps src/init debian/startpar/startpar
	dpkg-gencontrol -psysvinit -P$(tmp) -isp
	dpkg --build $(tmp) ..
	rm -rf $(tmp)
	#
	# 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,sbin,lib,usr}
	install -d -o root -g root -m 755 $(tmp)/usr/bin
	install -d -o root -g root -m 755 $(tmp)/usr/share/man
	install -d -o root -g root -m 755 $(tmp)/usr/share/man/man{1,8}
	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/last $(tmp)/usr/bin
	install -o root -g root -m 755 src/mesg $(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/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 man/pidof.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
	dpkg-gencontrol -psysvinit-utils -P$(tmp) -isp
	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,sbin,lib,etc}
ifeq ($(DEB_HOST_ARCH_OS),linux)
	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 755 $(tmp)/etc/network/if-up.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/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 -d -o root -g root -m 755 $(tmp)/usr/share/linda/overrides
	install -o root -g root -m 644 \
		debian/initscripts/linda.overrides \
		$(tmp)/usr/share/linda/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
	install -o root -g root -m 755 debian/initscripts/postinst $(tmp)/DEBIAN
	install -o root -g root -m 755 debian/initscripts/postrm $(tmp)/DEBIAN
	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
	dpkg --build $(tmp) ..
	rm -rf $(tmp)


# Architecture independant files.
binary-indep:   build checkroot
	#
	# sysv-rc package
	#
	-rm -rf $(tmp)
	install -d -o root -g root -m 755 $(tmp)/{DEBIAN,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 -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/*
	install -d -o root -g root -m 755 $(tmp)/etc/rc{2,3,4,5}.d
	cp -af debian/sysv-rc/etc/* $(tmp)/etc
	for N in 2 3 4 5 ; do \
		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/man5
	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/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/*.8 \
		$(tmp)/usr/share/man/fr/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] \
		 $(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 -d -o root -g root -m 755 $(tmp)/usr/share/linda/overrides
	install -o root -g root -m 644 \
		debian/sysv-rc/linda.overrides \
		$(tmp)/usr/share/linda/overrides/sysv-rc
	install -o root -g root -m 644 debian/sysv-rc/copyright \
	  $(tmp)$(doc)/sysv-rc/copyright
	install -o root -g root -m 755 debian/sysv-rc/postrm $(tmp)/DEBIAN/postrm
	dpkg-gencontrol -psysv-rc -P$(tmp) -isp
	dpkg --build $(tmp) ..
	rm -rf $(tmp)

clean:	checkroot unpatch
	(cd src; make clobber )
	-make -C debian/startpar clean
	rm -f build debian/files debian/substvars debian/readlink
	rm -rf $(tmp)
	find . -name '*.bak' -o -name '*~' | xargs -r rm -f --

binary: binary-arch binary-indep

checkroot:
	$(checkdir)
	test root = "`whoami`"

buildfromsvn:
	version=`dpkg-parsechangelog | grep Version: | awk '{print $$2}'`; \
	rm -rf ../sysvinit-$$version; \
	svn export . ../sysvinit-$$version; \
	cp debian/changelog ../sysvinit-$$version/debian/changelog; \
	chmod -R a-s ../sysvinit-$$version; \
	(cd ../sysvinit-$$version && debuild $(DEBUILDFLAGS))

.PHONY: binary binary-arch binary-indep clean checkroot

include /usr/share/dpatch/dpatch.make
