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

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

VERSION=$(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
DEB_HOST_ARCH=$(shell dpkg-architecture -qDEB_HOST_ARCH)
CONFFILE=/etc/cdebconf.conf

udebname=cdebconf-udeb_$(VERSION)_$(DEB_HOST_ARCH).udeb
udebpriority=$(shell grep '^Package: cdebconf-udeb' debian/control -A 10 | grep ^Priority: | cut -d ' ' -f 2)

priorityudebname=cdebconf-priority_$(VERSION)_$(DEB_HOST_ARCH).udeb
priorityudebpriority=$(shell grep '^Package: cdebconf-priority' debian/control -A 10 | grep ^Priority: | cut -d ' ' -f 2)

libudebname=libdebconfclient0-udeb_$(VERSION)_$(DEB_HOST_ARCH).udeb
libudebpriority=$(shell grep '^Package: libdebconfclient0-udeb' debian/control -A 10 | grep ^Priority: | cut -d ' ' -f 2)

slang_udebname=cdebconf-slang-udeb_$(VERSION)_$(DEB_HOST_ARCH).udeb
slang_udebpriority=$(shell grep '^Package: cdebconf-slang-udeb' debian/control -A 10 | grep ^Priority: | cut -d ' ' -f 2)

text_udebname=cdebconf-text-udeb_$(VERSION)_$(DEB_HOST_ARCH).udeb
text_udebpriority=$(shell grep '^Package: cdebconf-text-udeb' debian/control -A 10 | grep ^Priority: | cut -d ' ' -f 2)
	
newt_udebname=cdebconf-newt-udeb_$(VERSION)_$(DEB_HOST_ARCH).udeb
newt_udebpriority=$(shell grep '^Package: cdebconf-newt-udeb' debian/control -A 10 | grep ^Priority: | cut -d ' ' -f 2)


gtk_udebname=cdebconf-gtk-udeb_$(VERSION)_$(DEB_HOST_ARCH).udeb
gtk_udebpriority=$(shell grep '^Package: cdebconf-gtk-udeb' debian/control -A 10 | grep ^Priority: | cut -d ' ' -f 2)

#package names
LIBDEBCONFDEV=libdebconfclient0-dev
LIBDEBCONF=libdebconfclient0

FRONTENDS=text newt
UDEB_FRONTENDS=text newt

SIZEOPTFLAG=-Os
SPEEDOPTFLAG=-O2
DEBUG_CONFIGURE_OPT=
ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
	SIZEOPTFLAG=
	SPEEDOPTFLAG=
	DEBUG_CONFIGURE_OPT=--with-debug=yes
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="$(CFLAGS) $(SIZEOPTFLAG) -fomit-frame-pointer" \
		$(topdir)/configure --prefix=/usr --sysconfdir=/etc \
		--enable-d-i \
		--without-rpath --with-db="rfc822db" \
		--with-frontend="$(UDEB_FRONTENDS)" $(DEBUG_CONFIGURE_OPT) \
		--with-default-frontend=newt \
		--with-syslog-logging \
		--with-textwrap \
		--with-conffile=$(CONFFILE))
	(cd $(udebbuild); $(MAKE) clean; $(MAKE))
	touch $@

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

install-udeb: build-udeb install-dirs
	dh_testdir
	dh_testroot
	dh_installdebconf -n -pcdebconf-udeb
	(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
	rm -rf $(shell pwd)/debian/cdebconf-udeb/usr/share/man
	rm -rf $(shell pwd)/debian/cdebconf-udeb/usr/lib/cdebconf/frontend/newt.so
	rm -rf $(shell pwd)/debian/cdebconf-udeb/usr/lib/cdebconf/frontend/text.so
	rm -rf $(shell pwd)/debian/cdebconf-udeb/usr/lib/lib*.a
	rm -rf $(shell pwd)/debian/cdebconf-udeb/usr/lib/libdebconfclient.so
	mv $(shell pwd)/debian/cdebconf-udeb/usr/lib/libdebconfclient.so.* \
		$(shell pwd)/debian/libdebconfclient0-udeb/usr/lib

install-priority-udeb: build-udeb install-dirs
	dh_testdir
	dh_testroot
	dh_installdebconf -n -pcdebconf-priority

install-slang-udeb: build-udeb install-dirs
	dh_testdir
	dh_testroot
	dh_installdebconf -n -pcdebconf-slang-udeb
	(cd $(udebbuild)/src/modules/frontend/slang; \
		$(MAKE) install prefix=$(shell pwd)/debian/cdebconf-slang-udeb/usr )

install-newt-udeb: build-udeb install-dirs
	dh_testdir
	dh_testroot
	dh_installdebconf -n -pcdebconf-newt-udeb
	(cd $(udebbuild)/src/modules/frontend/newt; \
		$(MAKE) install prefix=$(shell pwd)/debian/cdebconf-newt-udeb/usr )

install-text-udeb: build-udeb install-dirs
	dh_testdir
	dh_testroot
	dh_installdebconf -n -pcdebconf-text-udeb
	(cd $(udebbuild)/src/modules/frontend/text; \
		$(MAKE) install prefix=$(shell pwd)/debian/cdebconf-text-udeb/usr )

install-gtk-udeb: build-udeb install-dirs
	dh_testdir
	dh_testroot
	dh_installdebconf -n -pcdebconf-gtk-udeb
	(cd $(udebbuild)/src/modules/frontend/gtk; \
		$(MAKE) install prefix=$(shell pwd)/debian/cdebconf-gtk-udeb/usr )

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

	d-shlibmove --commit --movedev debian/cdebconf/usr/include/cdebconf \
		usr/include debian/cdebconf/usr/lib/lib*.so



	rm -rf debian/cdebconf/usr/include

install-dirs: install-dirs-stamp
install-dirs-stamp:
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs 

# 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-priority libdebconfclient0-udeb \
	cdebconf-newt-udeb cdebconf-text-udeb

$(LIBDEBCONF): install
	dh_testdir
	dh_testroot
	dh_installdirs

	dh_installdocs		-p$@
	dh_installchangelogs	-p$@
	dh_makeshlibs		-p$@
	dh_strip		-p$@
	dh_compress		-p$@
	dh_fixperms		-p$@
	dh_installdeb		-p$@
	dh_shlibdeps		-p$@ -ldebian/$@/usr/lib
	dh_gencontrol		-p$@
	dh_md5sums		-p$@
	dh_builddeb		-p$@

$(LIBDEBCONFDEV): install
	dh_testdir
	dh_testroot
	dh_installdirs

	dh_installdocs		-p$@
	dh_installchangelogs	-p$@
	#dh_strip		-p$@
	dh_compress		-p$@
	dh_fixperms		-p$@
	dh_installdeb		-p$@
	#dh_shlibdeps		-p$@ -ldebian/$@/usr/lib
	dh_gencontrol		-p$@
	dh_md5sums		-p$@
	dh_builddeb		-p$@

cdebconf: $(LIBDEBCONFDEV) $(LIBDEBCONF)
	@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$@ -ldebian/$(LIBDEBCONF)/usr/lib
	dh_gencontrol		-p$@
	dh_md5sums		-p$@
	dh_builddeb		-p$@



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$@ -ldebian/$@/usr/lib
	# Don't write your stupid guesses to debian/files.
	dh_gencontrol 		-p$@ -- -fdebian/files~
	# Register file manually.
	dpkg-distaddfile	$(udebname) debian-installer $(udebpriority)
# udebs shouldn't have md5sums.
#	dh_md5sums		-p$@
	dh_builddeb		-p$@ --filename=$(udebname)

cdebconf-priority: install-priority-udeb
	@echo "Building $@"
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_strip		-p$@
	dh_compress		-p$@
	dh_fixperms		-p$@
	dh_installdeb		-p$@
	dh_shlibdeps -p$@ -ldebian/$@/usr/lib
	# Don't write your stupid guesses to debian/files.
	dh_gencontrol 		-p$@ -- -fdebian/files~
	# Register file manually.
	dpkg-distaddfile	$(priorityudebname) debian-installer $(priorityudebpriority)
# udebs shouldn't have md5sums.
#	dh_md5sums		-p$@
	dh_builddeb		-p$@ --filename=$(priorityudebname)

libdebconfclient0-udeb: install-udeb
	@echo "Building $@"
	dh_testdir
	dh_testroot
	dh_strip		-p$@
	dh_compress		-p$@
	dh_fixperms		-p$@
	dh_installdeb		-p$@
	dh_shlibdeps -p$@ -ldebian/$@/usr/lib
	# Don't write your stupid guesses to debian/files.
	dh_gencontrol 		-p$@ -- -fdebian/files~
	# Register file manually.
	dpkg-distaddfile	$(libudebname) debian-installer $(libudebpriority)
# udebs shouldn't have md5sums.
#	dh_md5sums		-p$@
	dh_builddeb		-p$@ --filename=$(libudebname)

cdebconf-text-udeb: install-text-udeb
	@echo "building $@"
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_strip		-p$@
	dh_compress		-p$@
	dh_fixperms		-p$@
	dh_installdeb		-p$@
	dh_shlibdeps -p$@ -ldebian/$@/usr/lib
	# don't write your stupid guesses to debian/files.
	dh_gencontrol 		-p$@ -- -fdebian/files~
	# register file manually.
	dpkg-distaddfile	$(text_udebname) debian-installer $(text_udebpriority)
# udebs shouldn't have md5sums.
#	dh_md5sums		-p$@
	dh_builddeb		-p$@ --filename=$(text_udebname)

cdebconf-slang-udeb: install-slang-udeb
	@echo "building $@"
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_strip		-p$@
	dh_compress		-p$@
	dh_fixperms		-p$@
	dh_installdeb		-p$@
	dh_shlibdeps -p$@ -ldebian/$@/usr/lib
	# don't write your stupid guesses to debian/files.
	dh_gencontrol 		-p$@ -- -fdebian/files~
	# register file manually.
	dpkg-distaddfile	$(slang_udebname) debian-installer $(slang_udebpriority)
# udebs shouldn't have md5sums.
#	dh_md5sums		-p$@
	dh_builddeb		-p$@ --filename=$(slang_udebname)

cdebconf-newt-udeb: install-newt-udeb
	@echo "Building $@"
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_strip		-p$@
	dh_compress		-p$@
	dh_fixperms		-p$@
	dh_installdeb		-p$@
	dh_shlibdeps -p$@ -ldebian/$@/usr/lib
	# Don't write your stupid guesses to debian/files.
	dh_gencontrol 		-p$@ -- -fdebian/files~
	# Register file manually.
	dpkg-distaddfile	$(newt_udebname) debian-installer $(newt_udebpriority)
# udebs shouldn't have md5sums.
#	dh_md5sums		-p$@
	dh_builddeb		-p$@ --filename=$(newt_udebname)

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

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