#!/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

# This is the debhelper compatability version to use.
export DH_COMPAT=2

topdir=$(shell pwd)
udebbuild=$(topdir)/debian/build-udeb
debbuild=$(topdir)/debian/build

PACKAGE=cdebconf-udeb
VERSION=$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
DEB_HOST_ARCH=$(shell dpkg-architecture -qDEB_HOST_ARCH)
FILENAME=$(PACKAGE)_$(VERSION)_$(DEB_HOST_ARCH).udeb
PRIORITY=$(shell grep ^Priority: debian/control | cut -d ' ' -f 2)
CONFFILE=/etc/cdebconf.conf

FRONTENDS=text slang
ifeq (,$(findstring hurd-,$(DEB_HOST_ARCH)))
FRONTENDS += bogl
endif

clean:
	dh_testdir
	dh_testroot
	rm -f *-stamp
	-$(MAKE) distclean
	-rm -rf $(udebbuild)
	-rm -rf $(debbuild)
	dh_clean

build-udeb: build-udeb-stamp
build-udeb-stamp:
	dh_testdir
	[ -d $(udebbuild) ] || mkdir -p $(udebbuild)
	(cd $(udebbuild); CFLAGS="-Os -fomit-frame-pointer" \
		$(topdir)/configure --prefix=/usr --sysconfdir=/etc \
		--without-rpath --with-db="textdb" \
		--with-frontend="text" \
		--with-conffile=$(CONFFILE))
	(cd $(udebbuild); $(MAKE) clean; $(MAKE))
	touch $@

build: build-stamp
build-stamp:
	dh_testdir
	[ -d $(debbuild) ] || mkdir -p $(debbuild)
	(cd $(debbuild); CFLAGS="-O2" \
		$(topdir)/configure --prefix=/usr --sysconfdir=/etc \
		--without-rpath \
		--with-db="textdb perldb" \
		--with-frontend="$(FRONTENDS)" \
		--with-conffile=$(CONFFILE))
	(cd $(debbuild); $(MAKE) clean; $(MAKE))
	touch $@

install-udeb: build-udeb
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	(cd $(udebbuild); \
		$(MAKE) install prefix=$(shell pwd)/debian/cdebconf-udeb/usr \
		etcdir=$(shell pwd)/debian/cdebconf-udeb/etc )
	rm -rf $(shell pwd)/debian/cdebconf-udeb/usr/include

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs 
	(cd $(debbuild); \
		$(MAKE) install prefix=$(shell pwd)/debian/cdebconf/usr \
		etcdir=$(shell pwd)/debian/cdebconf/etc )

	# TODO: REmove this temporary hack when we make debconf/cdebconf
	# coexist happily
	for i in usr/sbin/dpkg-reconfigure usr/bin/debconf-loadtemplate \
		usr/share/debconf/confmodule usr/share/debconf/frontend; do \
		mv $(shell pwd)/debian/cdebconf/$$i \
			$(shell pwd)/debian/cdebconf/$$i.cdebconf; \
	done

	dh_movefiles -pcdebconf-dev --sourcedir=debian/cdebconf \
		usr/lib/*.so usr/include/cdebconf
	rm -rf debian/cdebconf/usr/include

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

# Build architecture-dependent files here.
#
# Note that this builds a .udeb, which is not policy compliant or anything.
binary-arch: cdebconf cdebconf-udeb

cdebconf: install
	@echo "Building $@"
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_installdocs		-p$@ doc/*
	dh_installchangelogs	-p$@
	dh_strip		-p$@
	dh_compress		-p$@
	dh_fixperms		-p$@
	dh_makeshlibs		-p$@
	dh_installdeb		-p$@
	dh_shlibdeps		-p$@
	dh_gencontrol		-p$@
	dh_md5sums		-p$@
	dh_builddeb		-p$@

	dh_installdocs		-p$@-dev
	dh_installchangelogs	-p$@-dev
	#dh_strip		-p$@-dev
	dh_compress		-p$@-dev
	dh_fixperms		-p$@-dev
	dh_installdeb		-p$@-dev
	#dh_shlibdeps		-p$@-dev
	dh_gencontrol		-p$@-dev
	dh_md5sums		-p$@-dev
	dh_builddeb		-p$@-dev

cdebconf-udeb: install-udeb
	@echo "Building $@"
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_strip		-p$@
	dh_compress		-p$@
	dh_fixperms		-p$@
	dh_installdeb		-p$@
	dh_shlibdeps		-p$@
	# Don't write your stupid guesses to debian/files.
	dh_gencontrol 		-p$@ -- -fdebian/files~
	# Register file manually.
	dpkg-distaddfile	$(FILENAME) debian-installer $(PRIORITY)
	dh_md5sums		-p$@
	dh_builddeb		-p$@ --filename=$(FILENAME)

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