#!/usr/bin/make -f

export CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
# The build system ignores CPPFLAGS, pass them to CFLAGS/CXXFLAGS to enable
# the missing (hardening) flags.
export CFLAGS:=$(shell dpkg-buildflags --get CFLAGS) $(CPPFLAGS)
export CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS) $(CPPFLAGS)
export LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS) -Wl,--as-needed

export DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
export DEB_HOST_ARCH_OS   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
export DEB_HOST_ARCH_CPU  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)

export QT_SELECT=qt5

ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
	SUBSTVARS = -Vdist:Depends="pulseaudio-utils"
else
	SUBSTVARS = -Vdist:Depends=""
endif

SCONS_OPTS  = qt5=1
SCONS_OPTS += install_root=$(CURDIR)/debian/tmp/usr
SCONS_OPTS += qtdir=/usr/share/qt5 prefix=/usr
SCONS_OPTS += machine=$(DEB_HOST_ARCH)
SCONS_OPTS += buildtime=0

%:
	dh $@ --parallel --with=scour,autotools_dev

override_dh_auto_build:
	# create icons
	convert res/images/mixxx-icon.png -geometry 32x32 mixxx.xpm
	scons $(SCONS_OPTS) w=1
	docbook-to-man debian/mixxx.sgml > mixxx.1 

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

override_dh_auto_install:
	scons $(SCONS_OPTS) 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 {} \;
	chrpath -d debian/tmp/usr/bin/mixxx \
		debian/tmp/usr/lib/mixxx/plugins/vamp/libmixxxminimal.so

override_dh_gencontrol:
	dh_gencontrol -- $(SUBSTVARS)
