#!/usr/bin/make -f
# -*- makefile -*-

# no thanks dpkg-buildpackage, we will handle our own C++ optimizations
DEB_BUILD_OPTIONS= noopt

# Build flags are passed in from the SConscript.
MIXXX_SCONS_FLAGS = ""

# If ARCH is set to a different architecture when running pbuilder, pbuilder
# will be set to create an environment to build packages for the architecture
# specified in ARCH.
ifneq (,$(findstring i386,$(ARCH)))
       MIXXX_SCONS_FLAGS += machine=x86
endif
ifneq (,$(findstring amd64,$(ARCH)))
       MIXXX_SCONS_FLAGS += machine=x86_64
endif

# parallel builds for scons
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
MIXXX_SCONS_FLAGS += -j$(NUMJOBS)
endif

MIXXX_SCONS_FLAGS += prefix=/usr
MIXXX_SCONS_FLAGS += install_root=$(CURDIR)/debian/tmp/usr

%:
	dh $@ --parallel

override_dh_auto_build:
	scons $(MIXXX_SCONS_FLAGS) w=1
	docbook-to-man debian/mixxx.sgml > mixxx.1

override_dh_auto_clean:
	scons $(MIXXX_SCONS_FLAGS) -c || true
	rm -rf .sconf_temp/ cache/ linux_build/
	dh_clean .sconsign.dblite cachecustom.py \
		config.log src/build.h build/*.pyc mixxx.1
	dh_auto_clean

override_dh_auto_install:
	scons $(MIXXX_SCONS_FLAGS) install
	find $(CURDIR)/debian/tmp \( -name COPYING -o -name Thumbs.db \
		-o -name shifter.sh -o -name '*.xsl' -o -name '*.php' \) \
		-exec rm -f {} \;
	find $(CURDIR)/debian/tmp -name '*.js' -exec chmod -x {} \;
	find $(CURDIR)/debian/tmp/usr/share/mixxx/ -type f -exec chmod -x {} \;

override_dh_gencontrol:
	dh_gencontrol -- $(SUBSTVARS)
