#!/usr/bin/make -f

ifneq (,$(findstring parallel=,$(DEB_BUILD_OPTIONS)))
	NCPUS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
	NCPUS := $(shell getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
	ifeq ($(NCPUS),-1)
		NCPUS:=1
	endif
	ifeq ($(NCPUS),0)
		NCPUS:=1
	endif
endif

ifeq "$(DEB_BUILD_ARCH)" "powerpc"
	CFLAGS+=-flax-vector-conversions
endif
ifeq "$(DEB_BUILD_ARCH)" "mips"
	confflags=--disable-asm
endif
ifeq "$(DEB_BUILD_ARCH)" "mipsel"
	confflags=--disable-asm
endif

%:
	dh $@

override_dh_auto_build:
	CC="ccache $(CC)" dh_auto_configure -- \
        --enable-debug --enable-static --disable-lavf --enable-shared \
        --disable-avs --enable-pic --system-libx264 --bit-depth=10 \
	--libdir=/usr/lib/$(DEB_HOST_MULTIARCH)/x264-10bit \
        --extra-ldflags="-Wl,-z,defs -Wl,--as-needed" $(confflags)

	$(MAKE) -j $(NCPUS)
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp AM_UPDATE_INFO_DIR=no

	sed -e 's,@DEB_HOST_MULTIARCH@,$(DEB_HOST_MULTIARCH),' \
	debian/x264-10bit.in > $(CURDIR)/debian/tmp/usr/bin/x264-10bit
	chmod 755 $(CURDIR)/debian/tmp/usr/bin/x264-10bit

	$(MAKE) distclean

	CC="ccache $(CC)" dh_auto_configure -- \
	--enable-debug --enable-static --disable-lavf --enable-shared \
	--disable-avs --enable-pic --system-libx264 \
	--extra-ldflags="-Wl,-z,defs -Wl,--as-needed" $(confflags)

	$(MAKE) -j $(NCPUS)

	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp AM_UPDATE_INFO_DIR=n

	rm -rf debian/tmp/usr/lib/i386-linux-gnu/x264-10bit/pkgconfig

#	exit 1

ifneq "$(DEB_BUILD_ARCH)" "armel"
	LD_LIBRARY_PATH=$(CURDIR):$(LD_LIBRARY_PATH) help2man -N --help-option=--fullhelp --name="video encoder for the H.264/MPEG-4 AVC standard" ./x264  > debian/x264.1
endif

override_dh_auto_clean:
	touch config.mak
	dh_auto_clean

override_dh_prep override_dh_auto_install override_dh_auto_configure:
