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

CFLAGS = -Wall -g

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

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
	./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --sysconfdir=/etc --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info CFLAGS="$(CFLAGS)" --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
	$(MAKE) SUBDIRS="$(SUBDIRS_ARCH)"
	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_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 \
	  LD_LIBRARY_PATH=$(CURDIR)/debian/tmp/usr/lib help2man --no-info --include=$${f} --output=$(CURDIR)/debian/tmp/usr/share/man/man1/`basename $${f} .h2m`.1 $(CURDIR)/debian/tmp/usr/bin/`basename $${f} .h2m`; \
	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
