#! /usr/bin/make -f

SHELL = bash
package = util-linux

ifndef DEB_HOST_ARCH
DEB_BUILD_ARCH := $(shell dpkg --print-installation-architecture)
DEB_HOST_ARCH = $(DEB_BUILD_ARCH)
endif

export arch = $(DEB_HOST_ARCH)
version := $(shell sed -e '1{;s|^util-linux (\(.*\))\ .*|\1|;q;}' debian/changelog)

fdisk_arch = $(findstring $(arch),alpha i386 powerpc arm mips mipsel hppa ia64)
sparc = $(findstring $(arch),sparc)
nokernel24headers = $(findstring $(arch),m68k ia64)

build:
	$(checkdir)
	./configure
	$(MAKE) all arch=$(arch)
	-$(MAKE) disk-utils/raw
	touch build

clean:
	$(checkdir)
	rm -f build sys-utils/rdev
	touch make_include
	$(MAKE) distclean
	find . -name '*~' -print0 | xargs -0r rm
	rm -rf debian/tmp* debian/files* debian/substvars po/messages

# Architecture independant stuff

SUIDFILES = debian/tmp-mount/bin/{u,}mount

INITFILES = debian/hwclock.sh
BINFILES  = sys-utils/{arch,dmesg} \
	    text-utils/more
SBINFILES = disk-utils/{mkswap,blockdev} login-utils/agetty \
            hwclock/hwclock
ifneq ($(arch),$(nokernel24headers))
SBINFILES += disk-utils/raw mount/pivot_root
endif
UBINFILES = sys-utils/{ipcs,ipcrm,setsid} \
	    misc-utils/{namei,setterm,mcookie,whereis,ddate} \
	    getopt-1.1.2/getopt text-utils/rev disk-utils/fdformat
UBINFILES2= misc-utils/chkdupexe # debian/fdformat # don't strip these
ULIBFILES = text-utils/more.help
USBINFILES= sys-utils/readprofile disk-utils/elvtune # disk-utils/setfdprm

MOUNTBINFILES  = mount/mount mount/umount
MOUNTSBINFILES = mount/swapon mount/losetup

#BSDBINFILES = # misc-utils/kill
BSDUBINFILES = misc-utils/script misc-utils/logger sys-utils/renice login-utils/wall
BSDMAN1FILES = login-utils/wall.1 misc-utils/script.1 misc-utils/logger.1 # misc-utils/kill.1
BSDMAN8FILES = sys-utils/renice.8
BSDDOCFILES = debian/README.script

# Architecture independant docs

MAN1FILES = text-utils/{more,rev}.1 misc-utils/{namei.1,mcookie.1} \
	    sys-utils/{arch,readprofile}.1 \
            misc-utils/{chkdupexe.1,setterm.1,whereis.1,ddate.1} \
	    getopt-1.1.2/getopt.1
MAN8FILES = sys-utils/{dmesg.8,ipcrm.8,ipcs.8,setsid.8} login-utils/agetty.8 \
	      hwclock/hwclock.8 \
	      disk-utils/{mkswap.8,blockdev.8} disk-utils/elvtune.8 \
	      disk-utils/fdformat.8 # disk-utils/setfdprm.8
ifneq ($(arch),$(nokernel24headers))
MAN8FILES += disk-utils/raw.8 mount/pivot_root.8
endif

EXAMPLES = debian/fstab.example2 fdisk/sfdisk.examples \
	getopt-1.1.2/{test,parse}.{ba,tc}sh


INFOFILES = sys-utils/ipc.info
DOCFILES  = login-utils/{README.getty,README.modems-with-agetty,README.poeigl} debian/README.Debian.hwclock

MOUNTMAN5FILES = mount/fstab.5 mount/nfs.5
MOUNTMAN8FILES = mount/losetup.8 mount/swapoff.8 mount/umount.8 \
		 mount/mount.8 mount/swapon.8
MOUNTDOCFILES  = mount/README.mount

ifneq ($(arch),$(sparc))
SBINFILES := $(SBINFILES) disk-utils/{fsck.minix,mkfs,mkfs.minix}
MAN8FILES := $(MAN8FILES) disk-utils/{{fsck,mkfs}.minix.8,mkfs.8}
endif
ifeq ($(arch),$(fdisk_arch))
USBINFILES:= $(USBINFILES) sys-utils/{tunelp,cytune}
SBINFILES := $(SBINFILES) fdisk/{cfdisk,fdisk,sfdisk}
MAN8FILES := $(MAN8FILES) fdisk/{cfdisk.8,fdisk.8,sfdisk.8} \
	    sys-utils/{tunelp.8,cytune.8}
DOCFILES  := $(DOCFILES) fdisk/README.{c,}fdisk
endif
ifeq ($(arch),$(sparc))
SBINFILES := $(SBINFILES) fdisk/fdisk
MAN8FILES := $(MAN8FILES) fdisk/fdisk.8
DOCFILES  := $(DOCFILES) fdisk/README.fdisk
endif
ifeq ($(arch),i386)
USBINFILES:= $(USBINFILES) sys-utils/rdev
MAN8FILES := $(MAN8FILES) sys-utils/{rdev.8,vidmode.8} \
		sys-utils/{ramsize.8,rootflags.8}
endif

install:

binary-indep:	checkroot build binary-arch
	$(checkdir)
	dpkg-gencontrol -isp -putil-linux-locales -Pdebian/tmp-util-linux-locales
	dpkg --build debian/tmp-util-linux-locales ..

binary-arch:	checkroot build
	rm -rf debian/tmp*
	install -d debian/tmp/{DEBIAN,bin,sbin,etc/init.d,usr/{share/locale/{fr,cs,da,de,es,it,ja,nl,pt_BR,tr}/LC_MESSAGES,lib/mime/packages,sbin,bin,share/man/{man1,man8},share/info,lib,share/util-linux,share/doc/$(package)/examples,share/lintian/overrides}}

	install -s $(BINFILES)  debian/tmp/bin
	install -s $(SBINFILES) debian/tmp/sbin
	install -s $(UBINFILES) debian/tmp/usr/bin
	install    $(UBINFILES2) debian/tmp/usr/bin
ifneq ($(USBINFILES),)
	install -s $(USBINFILES) debian/tmp/usr/sbin
ifeq ($(arch), i386)
	ln -s rdev debian/tmp/usr/sbin/ramsize
	ln -s rdev debian/tmp/usr/sbin/vidmode
	ln -s rdev debian/tmp/usr/sbin/rootflags
endif
endif
	install -m 755 $(INITFILES) debian/tmp/etc/init.d
	install -m 644 $(ULIBFILES) debian/tmp/usr/share/util-linux
	install -m 644 $(MAN1FILES) debian/tmp/usr/share/man/man1
	install -m 644 $(MAN8FILES) debian/tmp/usr/share/man/man8
	install -m 644 $(INFOFILES) debian/tmp/usr/share/info
	install -m 644 debian/lintian-override debian/tmp/usr/share/lintian/overrides/util-linux
	cd po && make install DESTDIR=../debian/tmp
	install -m 644 $(DOCFILES)  debian/tmp/usr/share/doc/$(package)
	install -m 644 $(EXAMPLES)  debian/tmp/usr/share/doc/$(package)/examples
ifeq ($(arch), powerpc)
	mv -f debian/tmp/sbin/fdisk debian/tmp/sbin/ddisk
	mv -f debian/tmp/usr/share/man/man8/fdisk.8 debian/tmp/usr/share/man/man8/ddisk.8
endif
	(cd debian/tmp/sbin ; mv agetty getty)
	(cd debian/tmp/usr/share/man/man8 ; mv agetty.8 getty.8)
ifneq ($(arch), powerpc)
	(cd debian/tmp/usr/share/man/man8 && ln -s hwclock.8.gz clock.8.gz)
endif
	(cd debian/tmp/usr/share/doc/$(package) ; mv README.modems-with-agetty README.modems-with-getty )
	perl -pi.bak -e 's/agetty/getty/g' debian/tmp/usr/share/man/man8/getty.8 \
	debian/tmp/usr/share/doc/$(package)/README.getty \
	debian/tmp/usr/share/doc/$(package)/README.modems-with-getty
	rm `find debian/tmp/usr -name \*.bak`
	install -m 644 debian/changelog \
	  debian/tmp/usr/share/doc/$(package)/changelog.Debian
	install -m 644 HISTORY debian/tmp/usr/share/doc/$(package)/changelog
	find debian/tmp/usr/share/{info,doc,man} -type f | xargs gzip -9
	install -m 644 debian/mime.util-linux debian/tmp/usr/lib/mime/packages/util-linux
	install -m 644 debian/copyright debian/tmp/usr/share/doc/$(package)/copyright
	install debian/{preinst,postinst,prerm,postrm} debian/tmp/DEBIAN/
	install -m 644 debian/conffiles debian/tmp/DEBIAN/
	ls debian/tmp/{bin,sbin,usr/{bin,sbin}}/* | xargs dpkg-shlibdeps -putil-linux
	install -d debian/tmp-util-linux-locales/usr/share/doc/util-linux-locales
	install -d debian/tmp-util-linux-locales/DEBIAN
	install -m 755 debian/util-linux-locales.postinst debian/tmp-util-linux-locales/DEBIAN/postinst
	install -m 755 debian/util-linux-locales.prerm debian/tmp-util-linux-locales/DEBIAN/prerm
	mv debian/tmp/usr/share/locale debian/tmp-util-linux-locales/usr/share/
	install -m 644 HISTORY debian/tmp-util-linux-locales/usr/share/doc/util-linux-locales/changelog
	install -m 644 debian/changelog debian/tmp-util-linux-locales/usr/share/doc/util-linux-locales/changelog.Debian
	find debian/tmp-util-linux-locales/usr/share/doc -type f | xargs gzip -9
	install -m 644 debian/copyright debian/tmp-util-linux-locales/usr/share/doc/util-linux-locales/copyright
	dpkg-gencontrol -isp -putil-linux
	dpkg --build debian/tmp ..

	# Mount
	install -d debian/tmp-mount/{DEBIAN,bin,sbin,usr/share/{man/{man8,man5},doc/mount/examples}}
	install -m 4755 -o root -s $(MOUNTBINFILES) debian/tmp-mount/bin/.
	install -m 755 -s $(MOUNTSBINFILES) debian/tmp-mount/sbin/.
	install -m 644 $(MOUNTMAN8FILES) debian/tmp-mount/usr/share/man/man8
	install -m 644 $(MOUNTMAN5FILES) debian/tmp-mount/usr/share/man/man5
	install -m 644 $(MOUNTDOCFILES) debian/tmp-mount/usr/share/doc/mount
	(cd debian/tmp-mount/sbin ; ln -s swapon swapoff)
	install -m 644 debian/mount.fstab \
	  debian/tmp-mount/usr/share/doc/mount/examples/fstab
	install -m 644 debian/changelog \
	  debian/tmp-mount/usr/share/doc/mount/changelog.Debian
	install -m 644 debian/changelog.Debian-mount.old \
	  debian/tmp-mount/usr/share/doc/mount/changelog.Debian-mount.old
	install -m 644 HISTORY debian/tmp/usr/share/doc/$(package)/changelog
	find debian/tmp-mount/usr/share/{doc,man} -type f | xargs gzip -9
	install -m 644 debian/copyright debian/tmp-mount/usr/share/doc/mount/copyright
	install debian/mount.postinst debian/tmp-mount/DEBIAN/postinst
	install debian/mount.prerm   debian/tmp-mount/DEBIAN/prerm
	ls debian/tmp-mount/{bin,sbin}/* | xargs dpkg-shlibdeps -pmount
	dpkg-gencontrol -isp -pmount -Pdebian/tmp-mount
	dpkg --build debian/tmp-mount ..

	# Bsdutils
	install -d debian/tmp-bsd/{DEBIAN,bin,usr/{bin,share/man/{man8,man1},share/doc/bsdutils}}
	# install -m 755 -o root -s $(BSDBINFILES) debian/tmp-bsd/bin/.
	install -m 755 -s $(BSDUBINFILES) debian/tmp-bsd/usr/bin/.
	install -m 644 $(BSDMAN8FILES) debian/tmp-bsd/usr/share/man/man8
	mv debian/tmp-bsd/usr/share/man/man8/renice.8 debian/tmp-bsd/usr/share/man/man1/renice.1
	install -m 644 $(BSDMAN1FILES) debian/tmp-bsd/usr/share/man/man1
	install -m 644 $(BSDDOCFILES) debian/tmp-bsd/usr/share/doc/bsdutils
	chown root.tty debian/tmp-bsd/usr/bin/wall
	chmod g+s debian/tmp-bsd/usr/bin/wall
	install -m 644 debian/changelog \
	  debian/tmp-bsd/usr/share/doc/bsdutils/changelog.Debian
	install -m 644 HISTORY debian/tmp/usr/share/doc/$(package)/changelog
	find debian/tmp-bsd/usr/share/{doc,man} -type f | xargs gzip -9
	install -m 644 debian/copyright debian/tmp-bsd/usr/share/doc/bsdutils/copyright
	install debian/bsdutils.postinst debian/tmp-bsd/DEBIAN/postinst
	install debian/bsdutils.prerm   debian/tmp-bsd/DEBIAN/prerm
	ls debian/tmp-bsd/usr/bin/* | xargs dpkg-shlibdeps -pbsdutils
	dpkg-gencontrol -isp -pbsdutils -Pdebian/tmp-bsd -v1:$(version)
	dpkg --build debian/tmp-bsd ..

define checkdir
	test -f fdisk/fdisk.c -a -f debian/rules
endef

binary:		binary-indep binary-arch

source diff:
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

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

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

# Local Variables:
# mode:Makefile
# End:
