#!/usr/bin/make -f
# -*- makefile -*-

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
dpkg_buildflags = DEB_BUILD_MAINT_OPTIONS=$(DEB_BUILD_MAINT_OPTIONS) dpkg-buildflags
CFLAGS=$(shell $(dpkg_buildflags) --get CFLAGS)
CPPFLAGS=$(shell $(dpkg_buildflags) --get CPPFLAGS)
LDFLAGS=$(shell $(dpkg_buildflags) --get LDFLAGS)

SUBDIRS_INDEP = doc
SUBDIRS_ARCH = eb libebutils ebappendix ebfont ebinfo ebrefile ebstopcode ebzip po-eb po-ebutils m4
DIST_SUBDIRS = doc eb libebutils ebappendix ebfont ebinfo ebrefile ebstopcode ebzip m4 samples

config.status: configure
	dh_testdir
	dh_autotools-dev_updateconfig
	CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --sysconfdir=/etc --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) --with-pkgdocdir=/usr/share/doc/eb-doc

build: build-indep build-arch

build-indep: build-indep-stamp
build-indep-stamp: config.status
	$(MAKE) SUBDIRS="$(SUBDIRS_INDEP)"
	touch $@

build-arch: build-arch-stamp
build-arch-stamp: config.status
	rm -f eb.conf
	$(MAKE) SUBDIRS="$(SUBDIRS_ARCH)"
	perl -i -pe 's!/usr/lib/'`dpkg-architecture -qDEB_HOST_MULTIARCH`'!/usr/lib/`dpkg-architecture -qDEB_HOST_MULTIARCH`!g' eb.conf
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-indep-stamp build-arch-stamp
	rm -f po-eb/Makefile po-ebutils/Makefile
	[ ! -f Makefile ] || $(MAKE) DIST_SUBDIRS="$(DIST_SUBDIRS)" distclean
	dh_autotools-dev_restoreconfig
	dh_clean

install: install-indep install-arch

install-indep:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -i

#	for d in $(SUBDIRS_INDEP); do \
#	  (cd $${d} && make DESTDIR=$(CURDIR)/debian/tmp install); \
#	done
	install -d $(CURDIR)/debian/tmp/usr/share/doc/eb-doc/html
	for f in doc/*.html doc/*.css; do \
	  nkf -w $${f} | LC_ALL=C sed -r 's/.*content-type.*charset=.*//i' > $(CURDIR)/debian/tmp/usr/share/doc/eb-doc/html/`basename $${f}`; \
	done
	for f in README NEWS; do \
	  nkf -w $${f} > $(CURDIR)/debian/tmp/usr/share/doc/eb-doc/`basename $${f}`; \
	done
	install debian/README.Debian $(CURDIR)/debian/tmp/usr/share/doc/eb-doc/

	dh_movefiles -i
	dh_install -i

install-arch:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -s

	$(MAKE) SUBDIRS="$(SUBDIRS_ARCH)" DESTDIR=$(CURDIR)/debian/tmp install

	install -d $(CURDIR)/debian/tmp/usr/share/man/man1
	for f in $(CURDIR)/debian/*.h2m; do \
	  b=`basename $${f} .h2m`; \
	  m=$(CURDIR)/debian/tmp/usr/share/man/man1/$${b}.1; \
	  LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH) help2man --no-info --include=$${f} --output=$${m} $(CURDIR)/debian/tmp/usr/bin/$${b}; \
	  sed 's/"'$${b}' (EB Library) version/"EB Library/' $${m} > $${m}.tmp && mv -f $${m}.tmp $${m}; \
	  rm -f $${m}.tmp; \
	done
	install -d $(CURDIR)/debian/tmp/usr/share/eb
	for f in README; do \
	  nkf -w $${f} > $(CURDIR)/debian/tmp/usr/share/eb/`basename $${f}`; \
	done
	install debian/README.Debian $(CURDIR)/debian/tmp/usr/share/eb/

	dh_movefiles -s
	dh_install -s
	rm -f $(CURDIR)/debian/eb-utils/usr/bin/ebunzip
	rm -f $(CURDIR)/debian/eb-utils/usr/bin/ebzipinfo

binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
#	dh_installmenu
#	dh_installdebconf
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

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