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

DEBIAN_ARCH = $(shell dpkg-architecture -qDEB_BUILD_ARCH)

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

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

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

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

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
	CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
	CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

ifeq "$(DEBIAN_ARCH)" "powerpc"
	EXTRA_OPTIONS=--disable-simd-accel
endif

CFLAGS=

configure: configure-stamp
configure-stamp: debian/stamp-patched
	dh_testdir

	dh_autoreconf

	./configure $(CROSS) --prefix=/usr --mandir=\$${prefix}/share/man \
	--infodir=\$${prefix}/share/info CC="$(CC)" CXX="$(CXX)" $(EXTRA_OPTIONS) \
	CFLAGS="$(CFLAGS)"

	touch configure-stamp

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

	$(MAKE) -j $(NCPUS)

	touch build-stamp

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

	[ ! -f Makefile ] || $(MAKE) distclean

	dh_autoreconf_clean

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

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

	dh_install --fail-missing

#	cp mpeg2enc/mpeg2syntaxcodes.h debian/libmjpegtools-dev/usr/include/mjpegtools/mpeg2enc

# 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_installdocs -A AUTHORS
	dh_installinfo
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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