#!/usr/bin/make -f

#export DH_VERBOSE=1

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

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

configure: configure-stamp
configure-stamp:
	dh_testdir

	cp -f /usr/share/misc/config.sub .
	cp -f /usr/share/misc/config.guess .

	./configure $(CROSS) --prefix=/usr --with-fileio=sndfile \
	--disable-analyzer-hooks --with-pic --mandir=/usr/share/man \
	--enable-decode-layer1 --enable-nasm --enable-expopt=full CC="ccache $(CC)" \
	CXX="ccache $(CXX)" LDFLAGS="-Wl,--as-needed" CFLAGS="$(CFLAGS)"

	touch configure-stamp

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

# -j doesn't work at all (06/07/2008)
	$(MAKE)

	touch build-stamp

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

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

	dh_clean confcache libmp3lame/i386/choose_table.nas.lst \
	libmp3lame/i386/cpu_feat.nas.lst libmp3lame/i386/scalar.nas.lst \
	config.guess config.sub

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install DESTDIR=`pwd`/debian/tmp

	dh_install --fail-missing

# Build architecture-independent files here.
binary-indep: build install

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs TODO README USAGE ChangeLog Changes.txt
	dh_installman
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	maemo-optify
	dh_md5sums
	dh_builddeb

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