#!/usr/bin/make -f
# vim: nowrap ts=8

# Build locations
BUILD		:= $(shell pwd)/build
TMP		:= $(shell pwd)/debian/tmp
TMP_DPKG	:= $(TMP)-dpkg
TMP_DPKG_DEV	:= $(TMP)-dpkg-dev
TMP_DPKG_DOC	:= $(TMP)-dpkg-doc
DIR		:= $(shell pwd)

# We should really get this in a different way...
DEB_BUILD_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE 2> /dev/null || true)
DEB_HOST_GNU_TYPE	:= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE 2> /dev/null || true)
arch			:= $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifneq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  config_arg	:= --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
else
  config_arg	:=
endif

# Setup the buildlocation
$(BUILD)/config.status:
	$(checkdir)
	install -d $(BUILD)
	cd $(BUILD) && $(DIR)/configure \
		--prefix=/usr \
		--datadir=/usr/share \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--sysconfdir=/etc \
		--sharedstatedir=/var/lib \
		--localstatedir=/var/lib \
		--with-admindir=/var/lib/dpkg \
		--with-zlib=static \
		$(config_arg)

clean:
	$(checkdir)
	rm -f debian/files debian/substvars
	rm -f debian/dpkg.substvars
	rm -fr $(BUILD) $(TMP) $(TMP_DPKG) $(TMP_DPKG_DEV) $(TMP_DPKG_DOC)
	rm -f po/{cat-id-tbl.c,stamp-cat-id,*.gmo}
	rm -f stamp-build stamp-binary

build: stamp-build

stamp-build: $(BUILD)/config.status
	$(MAKE) $(MFLAGS) -C $(BUILD)
	touch stamp-build

binary: binary-arch binary-indep

stamp-binary: stamp-build
	$(checkdir)
	test "`id -u`" -eq 0
	rm -rf $(TMP)
	$(MAKE) $(MFLAGS) -C $(BUILD) \
		admindir=$(TMP)/var/lib/dpkg \
		prefix=$(TMP)/usr \
		datadir=$(TMP)/usr/share \
		mandir=$(TMP)/usr/share/man \
		infodir=$(TMP)/usr/share/info \
		sysconfdir=$(TMP)/etc \
		sharedstatedir=$(TMP)/var/lib \
		localstatedir=$(TMP)/var/lib \
		install
	find $(TMP) -type d | xargs chmod 755
	touch stamp-binary

binary-dpkg: stamp-binary
	rm -rf $(TMP_DPKG)
	install -d -m 755 -o root -g root $(TMP_DPKG)
	chmod g-s $(TMP_DPKG)
	install -d -m 755 -o root -g root $(TMP_DPKG)/usr/share/doc/dpkg
	install -d -m 755 -o root -g root $(TMP_DPKG)/usr/lib/dpkg
	install -d -m 755 -o root -g root $(TMP_DPKG)/usr/bin
	install -d -m 755 -o root -g root $(TMP_DPKG)/sbin
	-test -d $(TMP)/etc && mv $(TMP)/etc $(TMP_DPKG)/
	mv $(TMP)/var $(TMP_DPKG)/
	mv $(TMP)/usr/sbin/start-stop-daemon $(TMP_DPKG)/sbin/
	mv $(TMP)/usr/sbin $(TMP_DPKG)/usr/
	mv $(TMP)/usr/lib/dpkg/mksplit $(TMP_DPKG)/usr/lib/dpkg/
	mv $(TMP)/usr/lib/dpkg/methods $(TMP_DPKG)/usr/lib/dpkg/
	mv $(TMP)/usr/share/locale $(TMP_DPKG)/usr/share/
	set -e ; for i in dpkg dpkg-deb dpkg-split md5sum dselect ; do \
		mv $(TMP)/usr/bin/$$i $(TMP_DPKG)/usr/bin/ ; \
	done
	set -e ; for i in ChangeLog THANKS TODO copyright ; do \
		mv $(TMP)/usr/share/doc/dpkg/$$i $(TMP_DPKG)/usr/share/doc/dpkg/ ; \
	done
	set -e ; for i in "" ja sv ; do \
		install -d -m 755 -o root -g root $(TMP_DPKG)/usr/share/man/$$i/man1 ; \
		for m in md5sum.1 dpkg-deb.1 ; do \
			if [ -f $(TMP)/usr/share/man/$$i/man1/$$m ] ; then \
				mv $(TMP)/usr/share/man/$$i/man1/$$m $(TMP_DPKG)/usr/share/man/$$i/man1/$$m ; \
			fi ; \
		done ; \
		install -d -m 755 -o root -g root $(TMP_DPKG)/usr/share/man/$$i/man8 ; \
		for m in dpkg.8 dselect.8 dpkg-split.8 start-stop-daemon.8 \
		    	cleanup-info.8 dpkg-divert.8 dpkg-statoverride.8 \
			install-info.8 update-alternatives.8 update-rc.d.8 ; do \
			if [ -f $(TMP)/usr/share/man/$$i/man8/$$m ] ; then \
				mv $(TMP)/usr/share/man/$$i/man8/$$m $(TMP_DPKG)/usr/share/man/$$i/man8/$$m ; \
			fi ; \
		done ; \
	done

	install -d -m 755 -o root -g root $(TMP_DPKG)/etc/dpkg
	install -p -m 644 -o root -g root debian/dpkg.cfg \
		$(TMP_DPKG)/etc/dpkg/

# Now that dpkg has been installed, Debianize it
# Policy stuff
	find $(TMP_DPKG)/usr/share/man -type f | xargs gzip -9f
	strip $(TMP_DPKG)/usr/bin/* $(TMP_DPKG)/sbin/*
	mv $(TMP_DPKG)/usr/share/doc/dpkg/ChangeLog \
		$(TMP_DPKG)/usr/share/doc/dpkg/changelog

	install -p -m 644 -o root -g root debian/changelog \
		$(TMP_DPKG)/usr/share/doc/dpkg/changelog.Debian
	rm -f $(TMP_DPKG)/usr/share/doc/dpkg/copyright
	gzip -9 $(TMP_DPKG)/usr/share/doc/dpkg/*
	install -p -m 644 -o root -g root debian/copyright \
		$(TMP_DPKG)/usr/share/doc/dpkg/

# Final package creation
	install -d -m 755 -o root -g root $(TMP_DPKG)/DEBIAN
	install -p -m 644 -o root -g root debian/dpkg.conffiles $(TMP_DPKG)/DEBIAN/conffiles
	install -p -m 755 -o root -g root debian/dpkg.preinst $(TMP_DPKG)/DEBIAN/preinst
	install -p -m 755 -o root -g root debian/dpkg.prerm $(TMP_DPKG)/DEBIAN/prerm
	install -p -m 755 -o root -g root debian/dpkg.postinst $(TMP_DPKG)/DEBIAN/postinst

	dpkg-shlibdeps -dPre-Depends -Tdebian/dpkg.substvars \
		$(TMP_DPKG)/usr/bin/dpkg $(TMP_DPKG)/usr/bin/dselect \
		$(TMP_DPKG)/sbin/start-stop-daemon
	perl -I `pwd`/scripts scripts/dpkg-gencontrol.pl -isp \
		-Tdebian/dpkg.substvars -pdpkg -P$(TMP_DPKG)
	dpkg --build $(TMP_DPKG) ..

binary-dpkg-dev: stamp-binary
	rm -rf $(TMP_DPKG_DEV)
	install -d -m 755 -o root -g root $(TMP_DPKG_DEV)
	chmod g-s $(TMP_DPKG_DEV)
	install -d -m 755 -o root -g root $(TMP_DPKG_DEV)/etc/dpkg
	install -p -m 644 -o root -g root debian/shlibs.default \
		$(TMP_DPKG_DEV)/etc/dpkg/
	install -p -m 644 -o root -g root debian/shlibs.override \
		$(TMP_DPKG_DEV)/etc/dpkg/
	install -d -m 755 -o root -g root $(TMP_DPKG_DEV)/usr/lib/dpkg
	install -d -m 755 -o root -g root $(TMP_DPKG_DEV)/usr/share
	install -d -m 755 -o root -g root $(TMP_DPKG_DEV)/usr/bin
	mv $(TMP)/usr/lib/dpkg/parsechangelog $(TMP_DPKG_DEV)/usr/lib/dpkg/
	mv $(TMP)/usr/lib/dpkg/controllib.pl $(TMP_DPKG_DEV)/usr/lib/dpkg/
	set -e ; for i in dpkg-name dpkg-source dpkg-genchanges dpkg-gencontrol \
		dpkg-shlibdeps dpkg-buildpackage dpkg-distaddfile 822-date \
		dpkg-scanpackages dpkg-scansources dpkg-architecture \
		dpkg-parsechangelog dpkg-checkbuilddeps ; do \
		mv $(TMP)/usr/bin/$$i $(TMP_DPKG_DEV)/usr/bin/ ; \
	done
	set -e ; for i in "" ja sv ; do \
		install -d -m 755 -o root -g root $(TMP_DPKG_DEV)/usr/share/man/$$i/man1 ; \
		for m in dpkg-name.1 dpkg-source.1 822-date.1 dpkg-architecture.1 \
			dpkg-buildpackage.1 dpkg-distaddfile.1 dpkg-genchanges.1 \
			dpkg-gencontrol.1 dpkg-parsechangelog.1 dpkg-shlibdeps.1 \
			dpkg-checkbuilddeps.1 ; do \
			if [ -e $(TMP)/usr/share/man/$$i/man1/$$m ] ; then \
				mv $(TMP)/usr/share/man/$$i/man1/$$m $(TMP_DPKG_DEV)/usr/share/man/$$i/man1/$$m ; \
			fi ; \
		done ; \
		install -d -m 755 -o root -g root $(TMP_DPKG_DEV)/usr/share/man/$$i/man5 ; \
		for m in deb.5 deb-control.5 deb-old.5 ; do \
			if [ -e $(TMP)/usr/share/man/$$i/man5/$$m ] ; then \
				mv $(TMP)/usr/share/man/$$i/man5/$$m $(TMP_DPKG_DEV)/usr/share/man/$$i/man5/$$m ; \
			fi ; \
		done ; \
		install -d -m 755 -o root -g root $(TMP_DPKG_DEV)/usr/share/man/$$i/man8 ; \
		for m in dpkg-scanpackages.8 dpkg-scansources.8 ; do \
			if [ -e $(TMP)/usr/share/man/$$i/man8/$$m ] ; then \
				mv $(TMP)/usr/share/man/$$i/man8/$$m $(TMP_DPKG_DEV)/usr/share/man/$$i/man8/$$m ; \
			fi ; \
		done ; \
	done
# Now that dpkg-dev has been installed, Debianize it
# Policy stuff
	find $(TMP_DPKG_DEV)/usr/share/man -type f | xargs gzip -9f
	install -d -m 755 -o root -g root $(TMP_DPKG_DEV)/usr/share/doc
	ln -s dpkg $(TMP_DPKG_DEV)/usr/share/doc/dpkg-dev

# Final package creation
	install -d -m 755 -o root -g root $(TMP_DPKG_DEV)/DEBIAN
	install -p -m 644 -o root -g root debian/dpkg-dev.conffiles $(TMP_DPKG_DEV)/DEBIAN/conffiles
	install -p -m 755 -o root -g root debian/dpkg-dev.prerm $(TMP_DPKG_DEV)/DEBIAN/prerm
	install -p -m 755 -o root -g root debian/dpkg-dev.postinst $(TMP_DPKG_DEV)/DEBIAN/postinst

	perl -I `pwd`/scripts scripts/dpkg-gencontrol.pl -isp \
		-Tdebian/dpkg-dev.substvars -pdpkg-dev -P$(TMP_DPKG_DEV)
	dpkg --build $(TMP_DPKG_DEV) ..

binary-dpkg-doc: stamp-binary
	rm -rf $(TMP_DPKG_DOC)
	install -d -m 755 -o root -g root $(TMP_DPKG_DOC)
	chmod g-s $(TMP_DPKG_DOC)
	install -d -m 755 -o root -g root $(TMP_DPKG_DOC)/usr/share/doc/dpkg
	mv $(TMP)/usr/share/doc/dpkg/internals $(TMP_DPKG_DOC)/usr/share/doc/dpkg/
	mv $(TMP)/usr/share/doc/dpkg/ChangeLog.manuals \
		$(TMP_DPKG_DOC)/usr/share/doc/dpkg/changelog.manuals

# Now that dpkg-dev has been installed, Debianize it
# Policy stuff
	gzip -9f $(TMP_DPKG_DOC)/usr/share/doc/dpkg/[!i]*
	ln -s dpkg $(TMP_DPKG_DOC)/usr/share/doc/dpkg-doc

# Register documentation with doc-base
	install -d -m 755 -o root -g root $(TMP_DPKG_DOC)/usr/share/doc-base
	install -p -m 644 -o root -g root debian/dpkg-doc.doc-base \
		$(TMP_DPKG_DOC)/usr/share/doc-base/dpkg-doc
	
# Final package creation
	install -d -m 755 -o root -g root $(TMP_DPKG_DOC)/DEBIAN
	install -p -m 755 -o root -g root debian/dpkg-doc.prerm $(TMP_DPKG_DOC)/DEBIAN/prerm
	install -p -m 755 -o root -g root debian/dpkg-doc.postinst $(TMP_DPKG_DOC)/DEBIAN/postinst

	perl -I `pwd`/scripts scripts/dpkg-gencontrol.pl -isp \
		-Tdebian/dpkg-dev.substvars -pdpkg-doc -P$(TMP_DPKG_DOC)
	dpkg --build $(TMP_DPKG_DOC) ..

binary-arch: binary-dpkg
	set -e ; \
	v=`sed -n 's/^Version: //p' $(TMP_DPKG)/DEBIAN/control`; \
	f=dpkg-$${v}_$(arch).nondebbin.tar ; \
	tar -C $(TMP_DPKG) -cf $(DIR)/../$$f usr var ; \
	gzip -9f ../$$f ; \
	dpkg-distaddfile -fdebian/files $$f.gz byhand -

binary-indep: binary-dpkg-dev binary-dpkg-doc
	set -e ; set -x ; \
	v=`sed -n 's/^Version: //p' $(TMP_DPKG_DEV)/DEBIAN/control`; \
	f=dpkg-$${v}.tar.gz ; \
	if [ -f ../dpkg_$${v}.tar.gz ] ; then \
	    	cp ../dpkg_$${v}.tar.gz ../$$f ; \
		dpkg-distaddfile -fdebian/files  $$f byhand - ; \
	fi

define checkdir
	test -f include/dpkg.h.in
endef

.PHONY: clean build binary binary-arch binary-indep binary-dpkg binary-dpkg-dev
.PHONY: binary-dpkg-doc binary-nondebbin
