#!/usr/bin/make -f

# $Progeny: rules 4786 2005-02-06 20:52:04Z licquia $

# Copyright 2002 Hewlett-Packard Company
# Copyright 2000, 2001, 2002, 2004 Progeny Linux Systems, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

export DH_COMPAT=3

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

# if $DEB_BUILD_OPTIONS *doesn't* contain "noopt"
ifeq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
OPTIMIZE:=-O2
endif

# Figure out where the kernel PCMCIA headers are, searching the
# directories in order of modification time (most recent
# first).
#PCMCIA_HEADERS:=$(shell \
#	for KDIR in $$(ls -dt /usr/src/kernel* /usr/src/linux* 2>/dev/null); \
#	  do \
#	    if [ -d $$KDIR ]; then \
#		for HFILE in $$(find $$KDIR -name version.h); do \
#		    if expr "$$HFILE" : ".*/pcmcia/version\.h" > \
#		      /dev/null 2>&1; then \
#			echo "$${HFILE%/pcmcia/version.h}"; \
#			break 2; \
#		    fi \
#		done \
#	    fi \
#	done)
# if we found some, pass them to configure

ifneq ($(PCMCIA_HEADERS),)
CONFIGURE_PCMCIA:=--with-pcmcia-headers=$(PCMCIA_HEADERS)
endif

CFLAGS_DEB=CFLAGS="-g $(OPTIMIZE)"
CFLAGS_UDEB=CFLAGS="-g -Os" 
RUN_CONFIGURE=  ../configure $(CONFIGURE_PCMCIA) \
		--prefix=/usr \
		--sbindir=/sbin \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--with-default-url=file:///lib/discover/list.xml \
		--disable-curl

configure: configure-deb configure-udeb

configure-deb: configure-deb-stamp
configure-deb-stamp:
	dh_testdir

	mkdir builddeb
	cd builddeb && $(CFLAGS_DEB) $(RUN_CONFIGURE)

	touch $@

configure-udeb: configure-udeb-stamp
configure-udeb-stamp:
	dh_testdir

	mkdir buildudeb
	cd buildudeb && $(CFLAGS_UDEB) $(RUN_CONFIGURE) --disable-shared

	touch $@

build: build-deb build-udeb
#only build udeb for now
#build: build-udeb

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

	cd builddeb && $(MAKE)

	touch $@

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

	cd buildudeb && $(MAKE)

	touch $@

clean:
	dh_testdir
	dh_testroot

	rm -rf builddeb buildudeb build-deb-stamp build-udeb-stamp \
	  configure-deb-stamp configure-udeb-stamp

	dh_clean

install: install-deb install-udeb

install-deb: build-deb
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -v

	cd builddeb && $(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

	mv $(CURDIR)/debian/tmp/usr/bin/discover-static \
		$(CURDIR)/debian/tmp/sbin/discover
	rm $(CURDIR)/debian/tmp/usr/bin/discover

#	XXX: dh_strip is reporting weird "Permission denied" errors.
	for strip_file in debian/tmp/sbin/discover debian/tmp/usr/lib/libdiscover.so.*; do \
		strip $$strip_file; \
	done

	rm -f $(CURDIR)/debian/tmp/etc/discover.conf.example
	rm -f $(CURDIR)/debian/tmp/etc/discover-modprobe.conf.example

install-udeb: build-udeb
#	discover-udeb
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs -v -p$(UPACKAGE)

	cp buildudeb/discover/discover-static debian/$(UPACKAGE)/sbin/discover
	cp etc/discover.conf $(CURDIR)/debian/$(UPACKAGE)/etc/discover.conf.d/00discover


# Build architecture-dependent files here.
binary-arch: discover-deb discover-udeb
#Only build the udeb for now
#binary-arch: build-udeb install-udeb discover-udeb

discover-deb: install-deb
	dh_testdir
	dh_testroot
	dh_movefiles

	dh_installdocs -N$(UPACKAGE)
	dh_installchangelogs -N$(UPACKAGE)
#	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 -N$(UPACKAGE)
	dh_compress -N$(UPACKAGE)
	dh_fixperms -N$(UPACKAGE)
	dh_makeshlibs -N$(UPACKAGE)
#	dh_installdebconf
	dh_installdeb -N$(UPACKAGE)
	dh_shlibdeps -ldebian/libdiscover2/usr/lib -N$(UPACKAGE)
	dh_gencontrol -N$(UPACKAGE)
	dh_md5sums -N$(UPACKAGE)
	dh_builddeb -N$(UPACKAGE)

discover-udeb: install-udeb
	dh_testdir
	dh_testroot

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

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