#! /usr/bin/make -f
include /usr/share/dpatch/dpatch.make

SHELL = bash
PACKAGE = util-linux

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

ifndef DEB_HOST_GNU_SYSTEM
DEB_HOST_GNU_SYSTEM := $(shell dpkg-architecture -qDEB_HOST_GNU_SYSTEM)
endif

ifndef DEB_HOST_ARCH_OS
DEB_HOST_ARCH_OS := $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
endif


ifeq ($(DEB_HOST_GNU_SYSTEM),linux)
# Take account of old dpkg-architecture output.
DEB_HOST_GNU_SYSTEM := linux-gnu
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 ppc64 arm armeb armel mips mipsel hppa ia64 hurd-i386 amd64 s390)
rdev_arch = $(findstring $(arch),i386 amd64)
sparc = $(findstring $(arch),sparc sparc64)
nohwclock = $(findstring $(arch),s390)

SUBDIRS=po lib getopt disk-utils login-utils misc-utils mount sys-utils text-utils
ifeq ($(arch),$(fdisk_arch))
SUBDIRS += fdisk
endif
ifeq ($(arch),$(sparc))
SUBDIRS += fdisk
endif
ifeq ($(DEB_HOST_ARCH_OS),linux)
ifneq ($(arch),$(nohwclock))
SUBDIRS += hwclock 
endif
endif

ifneq ($(DEB_HOST_ARCH_OS),linux)
util-linux_Conflicts = getty
endif

CFDISK_PO_DIR=cfdisk-po
CFDISK_POT=$(CFDISK_PO_DIR)/cfdisk.pot

build: patch
	$(checkdir)
	./configure
	$(MAKE) all CPU=$(arch) arch=$(arch) SUBDIRS="${SUBDIRS}"
	# $(MAKE) disk-utils/raw - this is done above if linux/raw.h exists
	touch build

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

# Architecture independant stuff

SUIDFILES = debian/tmp-mount/bin/{u,}mount
BINFILES  = sys-utils/arch text-utils/more
UBINFILES = sys-utils/{ipcs,ipcrm,setsid} \
	misc-utils/{namei,setterm,mcookie,whereis,ddate} \
	getopt/getopt text-utils/{rev,line,pg}
SBINFILES = disk-utils/mkswap

ifeq ($(DEB_HOST_ARCH_OS),linux)
ifneq ($(arch),$(nohwclock))
INITFILES = debian/hwclock.sh
SBINFILES += hwclock/hwclock
endif
BINFILES  += sys-utils/dmesg
SBINFILES += disk-utils/{blockdev,raw,isosize} mount/pivot_root login-utils/agetty
UBINFILES += disk-utils/fdformat
USBINFILES = sys-utils/readprofile disk-utils/elvtune # disk-utils/setfdprm
endif

UBINFILES2= misc-utils/chkdupexe # debian/fdformat # don't strip these

ifeq ($(DEB_HOST_ARCH_OS),linux)
MOUNTBINFILES  = mount/mount mount/umount
MOUNTSBINFILES = mount/swapon mount/losetup
endif

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

# Architecture independant docs

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


EXAMPLES = fdisk/sfdisk.examples getopt/getopt-{test,parse}.{ba,tc}sh
ifeq ($(DEB_HOST_ARCH_OS),linux)
EXAMPLES += debian/fstab.example2
endif

INFOFILES = sys-utils/ipc.info
ifeq ($(DEB_HOST_ARCH_OS),linux)
DOCFILES  = login-utils/{README.getty,README.modems-with-agetty,README.poeigl}
ifneq ($(arch),$(nohwclock))
DOCFILES += debian/README.Debian.hwclock
endif
endif

ifeq ($(DEB_HOST_ARCH_OS),linux)
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
endif

ifneq ($(arch),$(sparc))
SBINFILES := $(SBINFILES) disk-utils/{{fsck,mkfs}.{minix,cramfs},mkfs}
MAN8FILES := $(MAN8FILES) disk-utils/{{fsck,mkfs}.minix.8,mkfs.8}	# no cramfs man
endif
ifeq ($(arch),$(fdisk_arch))
SBINFILES := $(SBINFILES) fdisk/{cfdisk,fdisk,sfdisk}
MAN8FILES := $(MAN8FILES) fdisk/{cfdisk.8,fdisk.8,sfdisk.8}
DOCFILES  := $(DOCFILES) fdisk/README.{c,}fdisk
ifeq ($(DEB_HOST_ARCH_OS),linux)
USBINFILES:= $(USBINFILES) sys-utils/{tunelp,cytune}
MAN8FILES := $(MAN8FILES) sys-utils/{tunelp.8,cytune.8}
endif
endif
ifeq ($(arch),$(sparc))
SBINFILES := $(SBINFILES) fdisk/fdisk
MAN8FILES := $(MAN8FILES) fdisk/fdisk.8
DOCFILES  := $(DOCFILES) fdisk/README.fdisk
endif
ifeq ($(arch),$(rdev_arch))
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)
	# This assumes non-native, and only one hyphen in the version number.
	dpkg-gencontrol -isp -putil-linux-locales -Pdebian/tmp-util-linux-locales -VUpstream=$$(sed 's/^.*(\(.*\)-.*).*/\1/; q' debian/changelog)
	dpkg --build debian/tmp-util-linux-locales ..

binary-arch:	checkroot build $(INITFILES)
	rm -rf debian/tmp*
	install -d debian/tmp/{DEBIAN,bin,sbin,etc/init.d,usr/{bin,sbin,lib/mime/packages,share/{locale/{ca,cs,da,de,es,et,fi,fr,it,ja,nl,pt_BR,sl,sv,tr}/LC_MESSAGES,man/{man1,man8},info,$(PACKAGE),doc/$(PACKAGE)/examples,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),$(rdev_arch))
	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
ifneq ($(INITFILES),)
	install -m 755 $(INITFILES) debian/tmp/etc/init.d
endif
	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/$(PACKAGE)
	cd po && make install DESTDIR=../debian/tmp
ifneq ($(DOCFILES),)
	install -m 644 $(DOCFILES)  debian/tmp/usr/share/doc/$(PACKAGE)
endif
	install -m 644 $(EXAMPLES)  debian/tmp/usr/share/doc/$(PACKAGE)/examples
ifeq ($(arch),$(findstring $(arch),powerpc ppc64))
	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
ifeq ($(DEB_HOST_GNU_SYSTEM),gnu)
	(cd debian/tmp/sbin ; mv mkswap mkswap.linux)
	(cd debian/tmp/usr/share/man/man8 ; mv mkswap.8 mkswap.linux.8)
endif
ifeq ($(DEB_HOST_ARCH_OS),linux)
	(cd debian/tmp/sbin ; mv agetty getty)
	(cd debian/tmp/usr/share/man/man8 ; mv agetty.8 getty.8)
ifneq ($(arch), $(nohwclock))
ifneq ($(arch),$(findstring $(arch),powerpc ppc64))
	(cd debian/tmp/usr/share/man/man8 && ln -s hwclock.8.gz clock.8.gz)
endif
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`
endif
	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.$(PACKAGE) debian/tmp/usr/lib/mime/packages/$(PACKAGE)
	install -m 644 debian/copyright debian/tmp/usr/share/doc/$(PACKAGE)/copyright
	install debian/{preinst,postinst,prerm,postrm} debian/tmp/DEBIAN/
ifeq ($(DEB_HOST_ARCH_OS),linux)
ifneq ($(arch),$(nohwclock))
	install -m 644 debian/conffiles debian/tmp/DEBIAN/
endif
endif
	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 -Vutil-linux:Conflicts="$(util-linux_Conflicts)"
	dpkg --build debian/tmp ..

ifeq ($(arch),$(fdisk_arch))
# Do the udeb
	install -d debian/tmp-{c,}fdisk-udeb/usr/sbin
	install -d debian/tmp-{c,}fdisk-udeb/DEBIAN
	install -m 755 fdisk/cfdisk debian/tmp-cfdisk-udeb/usr/sbin
	install -m 755 fdisk/fdisk fdisk/sfdisk debian/tmp-fdisk-udeb/usr/sbin
	strip --remove-section=.comment --remove-section=.note \
			debian/tmp-{c,}fdisk-udeb/usr/sbin/*

	# Generate a po translations needed for cfdisk
	mkdir $(CFDISK_PO_DIR)
	xgettext --default-domain=util-linux -C --keyword=_ --keyword=N_ -o $(CFDISK_POT) fdisk/*.c
	for po in po/*.po ; do  \
		LANG=`basename $$po | cut -f1 -d.` ; \
		msgmerge $$po $(CFDISK_POT)  > $(CFDISK_PO_DIR)/$$LANG.po ; \
		msgfmt $(CFDISK_PO_DIR)/$$LANG.po -o $(CFDISK_PO_DIR)/$$LANG.gmo ; \
		mkdir -p debian/tmp-cfdisk-udeb/usr/share/locale/$$LANG/LC_MESSAGES ; \
		cp $(CFDISK_PO_DIR)/$$LANG.gmo debian/tmp-cfdisk-udeb/usr/share/locale/$$LANG/LC_MESSAGES/util-linux.mo ; \
	done

	ls debian/tmp-fdisk-udeb/usr/sbin/* | xargs dpkg-shlibdeps -pfdisk-udeb -tudeb
	dpkg-gencontrol -isp -pfdisk-udeb -Pdebian/tmp-fdisk-udeb -fdebian/files~
	dpkg-distaddfile fdisk-udeb_$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)_$(arch).udeb debian-installer extra
	dpkg --build debian/tmp-fdisk-udeb ../fdisk-udeb_$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)_$(arch).udeb

	ls debian/tmp-cfdisk-udeb/usr/sbin/* | xargs dpkg-shlibdeps -pcfdisk-udeb -tudeb
	dpkg-gencontrol -isp -pcfdisk-udeb -Pdebian/tmp-cfdisk-udeb -fdebian/files~
	dpkg-distaddfile cfdisk-udeb_$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)_$(arch).udeb debian-installer extra
	dpkg --build debian/tmp-cfdisk-udeb ../cfdisk-udeb_$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)_$(arch).udeb
endif
ifeq ($(arch),$(sparc))
# Do the udeb
	install -d debian/tmp-fdisk-udeb/usr/sbin
	install -d debian/tmp-fdisk-udeb/DEBIAN
	install -m 755 fdisk/fdisk debian/tmp-fdisk-udeb/usr/sbin
	strip --remove-section=.comment --remove-section=.note \
			debian/tmp-fdisk-udeb/usr/sbin/*

	ls debian/tmp-fdisk-udeb/usr/sbin/* | xargs dpkg-shlibdeps -pfdisk-udeb
	dpkg-gencontrol -isp -pfdisk-udeb -Pdebian/tmp-fdisk-udeb -fdebian/files~
	dpkg-distaddfile fdisk-udeb_$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)_$(arch).udeb debian-installer extra
	dpkg --build debian/tmp-fdisk-udeb ../fdisk-udeb_$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)_$(arch).udeb
endif

ifeq ($(DEB_HOST_ARCH_OS),linux)
	# 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-mount/usr/share/doc/mount/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 ..
endif

	# 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/.
	cp misc-utils/scriptreplay.pl debian/tmp-bsd/usr/bin/scriptreplay
	chmod 755 debian/tmp-bsd/usr/bin/scriptreplay
	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-bsd/usr/share/doc/bsdutils/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:
