#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

DEB_BUILD_ARCH_CPU ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU)
DEB_BUILD_ARCH_OS ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS)
DEB_BUILD_GNU_SYSTEM ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_SYSTEM)
DEB_BUILD_GNU_CPU ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

cpus := $(shell grep -v ^\# /usr/share/dpkg/cputable | (while read debian gnu regex ; do echo $${debian} ; echo $${gnu}; done) | grep -vx x86_64 | sort -u)
systems := $(shell grep -v ^\# /usr/share/dpkg/ostable | (while read debian gnu regex ; do echo $${debian} ; echo $${gnu}; done) | sort -u)

not_cpus := $(shell echo $(cpus) | tr " " "\n" | grep -vx $(DEB_BUILD_ARCH_CPU) | grep -vx $(DEB_BUILD_GNU_CPU))
not_systems := $(shell echo $(systems) | tr " " "\n" | grep -vx $(DEB_BUILD_ARCH_OS) | grep -vx $(DEB_BUILD_GNU_SYSTEM))

not_cpus := $(shell for cpu in $(not_cpus) ; do echo -n , not+$$cpu ; done)
not_systems := $(shell for system in $(not_systems) ; do echo -n , not+$$system ; done)

ifneq (hurd,$(DEB_HOST_ARCH_OS))
provides = $(DEB_HOST_ARCH_OS),
endif

provides += $(DEB_BUILD_ARCH_CPU), $(DEB_BUILD_GNU_SYSTEM), $(DEB_BUILD_GNU_TYPE), $(not_cpus), $(not_systems)

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp 
	rm -f "*:*"
	dh_clean

build: build-stamp
build-stamp:
	dh_testdir
	sed -i -e "s%^Provides: .*%Provides: $(provides)%g" -e "s/_/-/g" debian/control

	TYPE_HANDLING_BOOTSTRAP=yes ./type-handling any linux	> "any:linux"
	TYPE_HANDLING_BOOTSTRAP=yes ./type-handling i386 any	> "i386:any"
	TYPE_HANDLING_BOOTSTRAP=yes ./type-handling amd64 any	> "amd64:any"
	
	touch build-stamp

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	install -o root -g root -m 755 type-handling $(CURDIR)/debian/type-handling/usr/bin
	install -o root -g root -m 644 *:* $(CURDIR)/debian/type-handling/usr/share/type-handling

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

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdebconf
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime	
#	dh_installinit
#	dh_installcron
	dh_installman type-handling.1
	dh_installinfo
	dh_installchangelogs 
	dh_link
#	dh_strip
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install 
