#!/usr/bin/make -f

PACKAGE=$(shell dh_listpackages | grep -- -udeb$$)
VERSION=$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
ARCH=$(shell dpkg --print-architecture)
UFILENAME=$(PACKAGE)_$(VERSION)_$(ARCH).udeb

buildtype := $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
hosttype := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

ifneq ($(buildtype),$(hosttype))
	crossflags := --build=$(buildtype) --host=$(hosttype)
endif

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

configure: configure-stamp
configure-stamp:
	dh_testdir

	#sh ./autogen.sh
	CFLAGS='$(optimize) -g -Wall' ./configure $(crossflags) --prefix=/usr \
		--sbindir=/sbin \
		--sysconfdir=/etc \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info
	#	--with-kernel-source=/usr/src/kernel-headers-2.2.19

	touch configure-stamp

build: configure-stamp build-stamp
build-stamp:
	dh_testdir

	$(MAKE)

	# This builds the pic library
	ar cqv lib/libdiscover_pic.a lib/.libs/*.o

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	-$(MAKE) distclean
	rm -f po/*.gmo lib/libdiscover_pic.a

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -v

	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

# FIXME: Not quite sure what to do about this.  Clearly something
# better needs to be done in Makefile.am, but I'm not sure what.
# In the meantime, we'll just fix it here and let other packages
# and those installing from source worry about it themselves.
	install -m 755 $(CURDIR)/debian/tmp/etc/init.d/discover.init \
		$(CURDIR)/debian/tmp/etc/init.d/discover

	mkdir -p debian/libdiscover1-pic/usr/lib
	cp lib/libdiscover_pic.a debian/libdiscover1-pic/usr/lib/

	# reportbug helper script
	install -m 755 -d debian/discover1/usr/share/bug
	install -m 755 debian/discover1.bug debian/discover1/usr/share/bug/discover1

# discover1-udeb
	cp -a lib/.libs/*.so* $(CURDIR)/debian/discover1-udeb/lib
	rm -f $(CURDIR)/debian/discover1-udeb/usr/lib/*so
	cp -a $(CURDIR)/debian/tmp/sbin/discover \
		$(CURDIR)/debian/discover1-udeb/sbin
#	$(MAKE) -C discover install DESTDIR=$(CURDIR)/debian/discover1-udeb

# 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 non-discover1-udeb discover1-udeb

non-discover1-udeb:
	dh_testdir
	dh_testroot
	dh_movefiles

	dh_installdocs
# we are native now
	dh_installchangelogs
# XXX: dh_installinit appears to be busted :-P (see Debian #140881)
	#dh_installinit -v --no-restart-on-upgrade --update-rcd-params="start 36 S ."
	dh_strip -Ndiscover1-udeb
	dh_compress  -Ndiscover1-udeb
	dh_fixperms -Ndiscover1-udeb
#	dh_makeshlibs
	dh_installdebconf -Ndiscover1-udeb
	dh_installdeb -Ndiscover1-udeb
	dh_shlibdeps -ldebian/libdiscover1/usr/lib -Ndiscover1-udeb
	dh_gencontrol -Ndiscover1-udeb
	dh_md5sums -Ndiscover1-udeb
	dh_builddeb -Ndiscover1-udeb

discover1-udeb: build install
	dh_testdir
	dh_testroot

	dh_strip -pdiscover1-udeb
	dh_compress -pdiscover1-udeb
	dh_fixperms -pdiscover1-udeb
#	dh_makeshlibs
	dh_installdeb -pdiscover1-udeb  --filename=discover1-udeb.udeb
	dh_shlibdeps -ldebian/libdiscover1/usr/lib -pdiscover1-udeb
	# Don't write your stupid guesses to debian/files.
	dh_gencontrol -pdiscover1-udeb -- -fdebian/files~
	# Register file manually.
	dpkg-distaddfile $(UFILENAME) debian-installer extra
	dh_builddeb -pdiscover1-udeb --filename=$(UFILENAME)

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