#!/usr/bin/make -f

DEB_BUILD_GNU_TYPE = $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
STRIP = strip
else
CONFARGS = --host=$(DEB_HOST_GNU_TYPE)
STRIP = $(DEB_HOST_GNU_TYPE)-strip
endif

CFLAGS = -W -Wall -g
INSTALL = install
INSTALL_FILE    = $(INSTALL) -p    -o root -g root  -m  644
INSTALL_PROGRAM = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_SCRIPT  = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_DIR     = $(INSTALL) -p -d -o root -g root  -m  755

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
INSTALL_PROGRAM += -s
endif

LIB_DIR=usr/lib/$(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

obj-sysv/config.status:
	$(checkdir)
	mkdir obj-sysv
	cd obj-sysv && CFLAGS="$(CFLAGS)" ../configure --prefix=/usr --mandir=/usr/share/man --libdir=/$(LIB_DIR)/libfakeroot --program-suffix=-sysv $(CONFARGS)

obj-tcp/config.status:
	$(checkdir)
	mkdir obj-tcp
	cd obj-tcp && CFLAGS="$(CFLAGS)" ../configure --prefix=/usr --mandir=/usr/share/man --libdir=/$(LIB_DIR)/libfakeroot --with-ipc=tcp --program-suffix=-tcp $(CONFARGS)

build: build-arch build-indep
build-arch: build-stamp
build-stamp: obj-sysv/config.status obj-tcp/config.status
	$(checkdir)

	cd obj-sysv && $(MAKE)
	cd obj-tcp && $(MAKE)

ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
ifeq (,$(findstring hurd, $(DEB_HOST_ARCH_OS)))
	cd obj-sysv && $(MAKE) check
endif
	cd obj-tcp && $(MAKE) check
endif
endif

	touch build-stamp

clean: checkroot
	$(checkdir)
	rm -rf obj-sysv obj64-sysv obj-tcp obj64-tcp
	rm -f build-stamp
#	 Add here commands to clean up after the build process.
	-cd obj-sysv && $(MAKE) distclean
	-cd obj-tcp && $(MAKE) distclean
ifneq (,$(findstring $(DEB_HOST_GNU_TYPE), sparc-linux-gnu s390-linux-gnu x86_64-linux-gnu i486-linux-gnu powerpc-linux-gnu ppc64-linux-gnu x86_64-kfreebsd-gnu))
	-cd obj64-sysv && $(MAKE) distclean
	-cd obj64-tcp && $(MAKE) distclean
endif
	rm -rf .deps
	rm -f debian/*.substvars debian/files
	rm -rf debian/tmp debian/fakeroot debian/libfakeroot obj obj64 autom4te.cache
	find . -type f -a \( -name \#\*\# -o -name .\*\~ -o -name \*\~ -o -name DEADJOE -o -name \*.orig -o -name \*.rej -o -name \*.bak -o -name .\*.orig -o -name .\*.rej -o -name .SUMS -o -name TAGS -o -name core -o \( -path \*/.deps/\* -a -name \*.P \) \) -exec rm -f {} \;

# Build architecture-independent files here.
binary-indep: build
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build checkroot
	$(checkdir)

	cd obj-tcp && $(MAKE) DESTDIR="$(CURDIR)/debian/tmp" install

	chmod 644 debian/tmp/$(LIB_DIR)/libfakeroot/libfakeroot-0.so
	mv debian/tmp/$(LIB_DIR)/libfakeroot/libfakeroot-0.so debian/tmp/$(LIB_DIR)/libfakeroot/libfakeroot-tcp.so
	rm debian/tmp/$(LIB_DIR)/libfakeroot/libfakeroot.so

	cd obj-sysv && $(MAKE) DESTDIR="$(CURDIR)/debian/tmp" install

	chmod 644 debian/tmp/$(LIB_DIR)/libfakeroot/libfakeroot-0.so
	mv debian/tmp/$(LIB_DIR)/libfakeroot/libfakeroot-0.so debian/tmp/$(LIB_DIR)/libfakeroot/libfakeroot-sysv.so
	rm debian/tmp/$(LIB_DIR)/libfakeroot/libfakeroot.so

	$(INSTALL_DIR) debian/tmp/usr/share/doc/fakeroot \
		           debian/tmp/$(LIB_DIR)/libfakeroot \
		           debian/libfakeroot/etc/ld.so.conf.d
	echo /usr/lib/$(DEB_HOST_MULTIARCH)/libfakeroot > debian/libfakeroot/etc/ld.so.conf.d/fakeroot-$(DEB_HOST_MULTIARCH).conf
	$(INSTALL_FILE) README doc/README.saving DEBUG debian/tmp/usr/share/doc/fakeroot/
	( echo "fakeroot was written originally by"; \
	  echo "joost witteveen"                   ; \
	  echo "------------------------------------------------"; \
	  cat COPYING) > debian/tmp/usr/share/doc/fakeroot/copyright
#	cd obj-sysv && ./libtool --finish $(CURDIR)/debian/tmp/$(LIB_DIR)/fakeroot
	rm -f debian/tmp/$(LIB_DIR)/libfakeroot/libfakeroot.*a*
	rm -f debian/tmp/usr/bin/simple
	$(INSTALL_FILE) debian/copyright debian/tmp/usr/share/doc/fakeroot/copyright
	$(INSTALL_FILE) debian/changelog debian/tmp/usr/share/doc/fakeroot/changelog.Debian
	find debian/tmp -name '*.1' -or -name changelog.Debian | \
		xargs gzip -9fn
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	$(STRIP) --remove-section=.comment --remove-section=.note --strip-unneeded debian/tmp/$(LIB_DIR)/libfakeroot/libfakeroot-sysv.so
	$(STRIP) --remove-section=.comment --remove-section=.note --strip-unneeded debian/tmp/$(LIB_DIR)/libfakeroot/libfakeroot-tcp.so
	$(STRIP) --remove-section=.comment --remove-section=.note debian/tmp/usr/bin/faked-sysv
	$(STRIP) --remove-section=.comment --remove-section=.note debian/tmp/usr/bin/faked-tcp
endif
	$(INSTALL_DIR) debian/fakeroot/DEBIAN debian/libfakeroot/DEBIAN

	# split
	(cd debian/tmp && find usr/bin usr/share -type f | xargs tar -cf - ) |\
		(cd debian/fakeroot && tar -xf - )
	(cd debian/tmp && find usr/lib | xargs tar -cf - ) |\
		(cd debian/libfakeroot && tar -xf - )
	$(INSTALL_DIR) debian/libfakeroot/usr/share/doc/libfakeroot
	cp -r debian/tmp/usr/share/doc/fakeroot/. \
		debian/libfakeroot/usr/share/doc/libfakeroot/.

	$(INSTALL_SCRIPT) debian/fakeroot.postrm debian/fakeroot/DEBIAN/postrm
	$(INSTALL_SCRIPT) debian/fakeroot.prerm debian/fakeroot/DEBIAN/prerm
	echo /etc/ld.so.conf.d/fakeroot-$(DEB_HOST_MULTIARCH).conf >debian/libfakeroot/DEBIAN/conffiles
ifneq ($(DEB_HOST_GNU_SYSTEM),gnu)
	sed 's/@FAKEROOT_SYSV_ALTPRIO@/50/;s/@FAKEROOT_TCP_ALTPRIO@/30/;' \
		debian/fakeroot.postinst.in > debian/fakeroot.postinst
else
	sed 's/@FAKEROOT_SYSV_ALTPRIO@/30/;s/@FAKEROOT_TCP_ALTPRIO@/50/;' \
		debian/fakeroot.postinst.in > debian/fakeroot.postinst
endif
	$(INSTALL_SCRIPT) debian/fakeroot.postinst debian/fakeroot/DEBIAN/postinst
	rm -f debian/fakeroot.postinst
	# We can just use the deps for faked.
	dpkg-shlibdeps -Tdebian/libfakeroot.substvars \
		debian/libfakeroot/$(LIB_DIR)/libfakeroot/*.so
	dpkg-shlibdeps -Tdebian/fakeroot.substvars \
		debian/fakeroot/usr/bin/faked-sysv \
		debian/fakeroot/usr/bin/faked-tcp
	dpkg-gencontrol -pfakeroot -ldebian/changelog -isp -Tdebian/fakeroot.substvars -Pdebian/fakeroot
	dpkg-gencontrol -plibfakeroot -ldebian/changelog -isp -Tdebian/libfakeroot.substvars -Pdebian/libfakeroot
	chmod 644 debian/fakeroot/DEBIAN/control debian/libfakeroot/DEBIAN/control
	chown root:root debian/fakeroot/DEBIAN/control debian/libfakeroot/DEBIAN/control
	cd debian/fakeroot && find * -type f ! -regex '^DEBIAN/.*' -print0 | xargs -r0 md5sum > DEBIAN/md5sums
	cd debian/libfakeroot && find * -type f ! -regex '^DEBIAN/.*' -print0 | xargs -r0 md5sum > DEBIAN/md5sums
	dpkg-deb --build debian/fakeroot ..
	dpkg-deb --build debian/libfakeroot ..

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

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

prebuild:
	@echo prebuild handled differently now

define checkdir
	test -f debian/rules
endef

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