#! /usr/bin/make -f

export DH_OPTIONS

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)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)


HARD_CPPFLAGS = -D_FORTIFY_SOURCE=2
HARD_CFLAGS   = -Wformat=2
HARD_LDFLAGS  = -Wl,-z,now

ifneq (,$(filter-out $(DEB_HOST_ARCH), alpha hppa arm))
	HARD_CFLAGS += -fstack-protector --param ssp-buffer-size=4
endif
ifneq (,$(filter-out $(DEB_HOST_ARCH), ia64 hppa avr32))
	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 do append -g for release builds though, for the detached -dbg package,
# and also now the hardening flags too as appropriate.
CPPFLAGS = $(HARD_CPPFLAGS)
CFLAGS   = $(HARD_CFLAGS) -g
CXXFLAGS = $(HARD_CFLAGS) -g
LDFLAGS  = $(HARD_LDFLAGS)

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


objdir = objs


clean:
	dh_testdir
	dh_testroot
	$(RM) build-stamp install-stamp
	$(RM) -r $(objdir)
	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)	\
				     --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
				     --enable-static			\
				     --prefix=/usr			\
				     CPPFLAGS="$(CPPFLAGS)"		\
				     CFLAGS="$(CFLAGS)"			\
				     CXXFLAGS="$(CXXFLAGS)"		\
				     LDFLAGS="$(LDFLAGS)"

build: build-arch
build-arch: build-stamp
build-indep:

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


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


binary: binary-arch

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

binary-indep:


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