#!/usr/bin/make -f

#export DH_VERBOSE=1

#include /usr/share/quilt/quilt.make

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

NCPUS := $(shell getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)

ifeq ($(NCPUS),-1)
	NCPUS:=1
endif

ifeq ($(NCPUS),0)
	NCPUS:=1
endif

CFLAGS = -Wall -g
LDFLAGS= --as-needed
IS_DEB_MAKE=1
export IS_DEB_MAKE

CC:=ccache $(CC)
CXX:=ccache $(CXX)

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

configure: configure-stamp
configure-stamp:
	dh_testdir

	chmod 755 configure
	./configure --host=$(DEB_HOST_GNU_TYPE) --prefix=/usr \
	--build=$(DEB_BUILD_GNU_TYPE) \
	--extra-cflags="$(CFLAGS)" --cc="$(CC)" CXX="$(CXX)"

	touch $@

build: build-stamp
build-stamp: configure-stamp
	dh_testdir

# TODO: we exclude GPAC Proprietary Extension project: libiconv dependency
	$(MAKE) -j

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	-$(MAKE) distclean

#	debian/rules unpatch

	dh_clean bin/gcc/libgpac*

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) install prefix=$(CURDIR)/debian/tmp/usr \
	moddir=gpac

	$(MAKE) install-lib prefix=$(CURDIR)/debian/tmp/usr

	dh_install

# Prune empty directories
	find debian -type d | xargs rmdir -p --ignore-fail-on-non-empty

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs Changelog
	dh_installmenu
#	dh_installman doc/man/*.1
	dh_link
	dh_strip --no-automatic-dbgsym
	dh_compress
	dh_fixperms
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb --destdir=.

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