#!/usr/bin/make -f
# Ron Lee 2003 - 2016

#export DH_VERBOSE=1
export DH_OPTIONS

SHELL = /bin/bash

NUM_CPUS = $(shell getconf _NPROCESSORS_ONLN 2>/dev/null)
PARALLEL = $(subst parallel=,,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NJOBS    = -j$(or $(PARALLEL),$(NUM_CPUS),1)


DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)


# -fstack-protector-all is added by the default autoconf tests.
HARD_CPPFLAGS = -D_FORTIFY_SOURCE=2
HARD_CFLAGS   = -Wformat=2
HARD_LDFLAGS  = -Wl,-z,now

ifneq (,$(filter-out alpha hppa arm, $(DEB_HOST_ARCH)))
	HARD_CFLAGS += -fstack-protector --param ssp-buffer-size=4
endif
ifneq (,$(filter-out ia64 hppa avr32, $(DEB_HOST_ARCH)))
	HARD_LDFLAGS += -Wl,-z,relro
endif

# Keep dpkg-buildpackage the hell out of messing with our compile flags,
# we should trust upstream to know better than it what to use here.
# We explicitly re-add -g and -O2 here, since by explicitly overriding
# these variables, we otherwise disable autoconf adding them by default.
CPPFLAGS = $(HARD_CPPFLAGS)
CFLAGS   = $(HARD_CFLAGS) -g -O2
CXXFLAGS = $(HARD_CFLAGS) -g -O2
LDFLAGS  = $(HARD_LDFLAGS)


ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS = -g -O0
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif


ifneq (,$(filter arm armel mips mipsel, $(DEB_HOST_ARCH)))
	EXTRA_CONFIG_FLAGS = --enable-fixed-point
endif



objdir := objs


clean:
	dh_testdir
	dh_testroot
	$(RM) -r $(objdir)
	$(RM) *-stamp
	dh_clean


$(objdir)/config.status: configure
	dh_testdir
	mkdir -p $(objdir)
	cd $(objdir) &&	../configure --disable-maintainer-mode		\
				     --host=$(DEB_HOST_GNU_TYPE)	\
				     --build=$(DEB_BUILD_GNU_TYPE)	\
				     --prefix=/usr			\
				     $(EXTRA_CONFIG_FLAGS)		\
				     CPPFLAGS="$(CPPFLAGS)"		\
				     CFLAGS="$(CFLAGS)"			\
				     CXXFLAGS="$(CXXFLAGS)"		\
				     LDFLAGS="$(LDFLAGS)"

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: $(objdir)/config.status
	dh_testdir
	$(MAKE) $(NJOBS) -C $(objdir) opusfile
	touch $@

build-indep: build-indep-stamp
build-indep-stamp: $(objdir)/config.status
	dh_testdir
	$(MAKE) $(NJOBS) -C $(objdir) docs
	touch $@


install: install-arch install-indep

install-arch: install-arch-stamp
install-arch-stamp: build-arch
	dh_testdir
	$(MAKE) -C $(objdir) install-opusfile DESTDIR=$(CURDIR)/debian/tmp
	touch $@

install-indep: install-indep-stamp
install-indep-stamp: build-indep
	dh_testdir
	$(MAKE) -C $(objdir) install-docs DESTDIR=$(CURDIR)/debian/tmp
	touch $@


binary: binary-arch binary-indep

binary-indep: DH_OPTIONS = -i
binary-indep: install-indep
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installchangelogs
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch: DH_OPTIONS = -a
binary-arch: install-arch
	dh_testdir
	dh_testroot
	dh_install --sourcedir=debian/tmp
	dh_installdocs
	dh_installchangelogs
	dh_strip --dbg-package=libopusfile-dbg
	dh_compress
	dh_fixperms
	dh_makeshlibs -- -c0
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb


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

