#!/usr/bin/make -f

.NOTPARALLEL:

package = speech-dispatcher
libpackage = libspeechd2
libpackagedev = libspeechd-dev

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

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE)
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

CFLAGS = -Wall -g
INSTALL = install
INSTALL_FILE    = $(INSTALL) -p    -o root -g root  -m  644
INSTALL_PROGRAM = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_SCRIPT  = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_DIR     = $(INSTALL) -p -d -o root -g root  -m  755
ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -O0
else
    CFLAGS += -O2
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif

config.status: configure.in
	dh_testdir
	dh_autoreconf
	./configure $(confflags) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --sysconfdir=/etc pidpath=/var/run/speech-dispatcher/

build: build-stamp

build-stamp: config.status
	dh_testdir
	$(MAKE) MAKEFLAGS="$(MAKEFLAGS)" CFLAGS="$(CFLAGS)"
	(cd src/python; make)
	(cd src/python/speechd; make; make paths.py)
	(cd src/python/speechd_config; make; make paths.py)
	(cd doc; $(MAKE) MAKEFLAGS="$(MAKEFLAGS)" info)
	touch build-stamp

clean:
	dh_testdir
	rm -f build-stamp
	[ ! -f Makefile ] || $(MAKE) MAKEFLAGS="$(MAKEFLAGS)" distclean
	find -name '.#*' -exec rm {} \;
	dh_autoreconf_clean
	dh_clean

install: install-arch install-indep

install-arch: build
	dh_testdir
	dh_testroot
	dh_prep -A
	dh_installdirs -A
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp CFLAGS="$(CFLAGS)" \
		INSTALL="$(INSTALL)" INSTALL_FILE="$(INSTALL_FILE)" INSTALL_PROGRAM="$(INSTALL_PROGRAM)" \
		INSTALL_SCRIPT="$(INSTALL_SCRIPT)" INSTALL_DIR="$(INSTALL_DIR)"

install-indep: build
	dh_testdir
	dh_testroot
	dh_prep -A
	dh_installdirs -A
# Install python-speechd files
	( cd src/python; make install prefix=$(CURDIR)/debian/tmp/usr )

# Build architecture-independent files here.
binary-indep: build install-indep
	dh_testdir
	dh_testroot
	dh_install -i
	dh_installdocs -i
	dh_installman -i
	dh_installchangelogs -i ChangeLog
	dh_installinfo -i
	dh_python2 -ppython-speechd
	chmod a+x $(CURDIR)/debian/python-speechd/usr/share/pyshared/speechd/_test.py
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install-arch
	dh_testdir
	dh_testroot
	dh_install -a
	dh_installdocs -a
	dh_installinit -a --error-handler=true
	dh_installman -a
	dh_installinfo -a
	dh_installchangelogs -a ChangeLog
	dh_installlogrotate -a
	dh_lintian -a
	dh_link -a
	dh_strip -a --dbg-package=$(package)-dbg
	dh_compress -a
	dh_fixperms -a
	chmod 700 $(CURDIR)/debian/$(package)/var/log/speech-dispatcher
	dh_makeshlibs -p$(libpackage)
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

patch: patch-stamp
patch-stamp:
	dpatch apply-all
	dpatch cat-all >patch-stamp
	touch patch-stamp

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