#!/usr/bin/make -f

export NOCOLOR=1

PYVERSIONS := $(shell pyversions -s)
VENDOR ?= $(shell if dpkg-vendor --is Ubuntu || dpkg-vendor --derives-from Ubuntu; then echo "Ubuntu"; else echo "Debian"; fi)

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
else
	NUMJOBS := 1
endif

WAFFLAGS := --nocache --prefix=/usr -j $(NUMJOBS)
ifeq ($(VENDOR),Ubuntu)
additional_wafflags := --with-default-output-plugin=pulse
endif

%:
	dh $@

override_dh_auto_clean:
	rm -rf _build_python_ $(PYVERSIONS:%=_build_%_)
	./waf distclean
	# Clean up after waf
	find wafadmin waftools -name '*.pyc' -delete
	rm -f waf-lightc

override_dh_auto_configure:
	./waf configure $(WAFFLAGS) \
		--with-perl-archdir=$(shell /usr/bin/perl -MConfig -le'print $$Config{vendorarch}') \
		--with-perl-binary=/usr/bin/perl \
		--with-ruby-archdir=$(shell ruby1.8 -rrbconfig -e'print Config::CONFIG["archdir"]') \
		--with-ruby-libdir=$(shell ruby1.8 -rrbconfig -e'print Config::CONFIG["rubylibdir"]') \
		--without-optionals=python $(additional_wafflags)
	mv _build_ _build_python_
	for pyversion in $(PYVERSIONS); do \
		$${pyversion} ./waf configure $(WAFFLAGS) --without-xmms2d --with-optionals=python && \
		mv _build_ _build_$${pyversion}_; \
	done

override_dh_auto_build:
	for pyversion in python $(PYVERSIONS); do \
		mv _build_$${pyversion}_ _build_ && \
		$${pyversion} ./waf -v && \
		mv _build_ _build_$${pyversion}_; \
	done

override_dh_auto_install:
	for pyversion in python $(PYVERSIONS); do \
		echo installing $${pyversion}; \
		mv _build_$${pyversion}_ _build_ && \
		$${pyversion} ./waf install --destdir=$(CURDIR)/debian/tmp --without-ldconfig && \
		mv _build_ _build_$${pyversion}_; \
	done
ifeq ($(VENDOR),Ubuntu)
	echo "misc:Depends=xmms2-plugin-pulse" >> debian/xmms2.substvars
endif
