#!/usr/bin/make -f
#export DH_VERBOSE=1

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

ifeq ($(DEB_STAGE),stage1)
export DH_OPTIONS += -Nruby-semanage -Npython-semanage
else
PYVERSIONS = $(shell pyversions -rv)
endif

# The build system doesn't use CPPFLAGS, pass them to CFLAGS to enable the
# missing (hardening) flags.
export DEB_CFLAGS_MAINT_APPEND = $(shell dpkg-buildflags --get CPPFLAGS)

%:
	dh $@ --with python2 --with ruby

override_dh_auto_build:
	$(MAKE) all LIBBASE="lib/${DEB_HOST_MULTIARCH}"

ifneq ($(DEB_STAGE),stage1)
	for version in $(PYVERSIONS); do              \
	  $(MAKE) pywrap PYTHON=python$$version  \
	    PYPREFIX=python-$$version;  \
	done;

	$(MAKE) -C src rubywrap RUBY=ruby1.9.1 RUBYLIBVER=1.9.1 \
	  RUBYINC="-I/usr/include/ruby-1.9.1/ -I/usr/include/ruby-1.9.1/\$${RUBYPLATFORM}/"

	$(MAKE) -C src rubywrap RUBY=ruby2.0 RUBYLIBVER=2.0.0 \
	  RUBYINC="-I/usr/include/ruby-2.0.0/ -I/usr/include/\$${RUBYPLATFORM}/ruby-2.0.0/"
endif

override_dh_auto_install:
	$(MAKE) install DESTDIR="${CURDIR}/debian/tmp" \
	  LIBDIR="${CURDIR}/debian/tmp/usr/lib/${DEB_HOST_MULTIARCH}" \
	  SHLIBDIR="${CURDIR}/debian/tmp/usr/lib/${DEB_HOST_MULTIARCH}"

ifneq ($(DEB_STAGE),stage1)
	for version in $(PYVERSIONS); do              \
	  $(MAKE) install-pywrap PYTHON=python$$version  \
	    DESTDIR="${CURDIR}/debian/tmp" \
	    PYPREFIX=python-$$version;  \
	done;

	$(MAKE) -C src install-rubywrap RUBY=ruby1.9.1 RUBYLIBVER=1.9.1 DESTDIR="${CURDIR}/debian/tmp" \
	    RUBYINSTALL="${CURDIR}/debian/tmp/usr/lib/ruby/vendor_ruby/\$${RUBYLIBVER}/\$${RUBYPLATFORM}";

	$(MAKE) -C src install-rubywrap RUBY=ruby2.0 RUBYLIBVER=2.0.0 DESTDIR="${CURDIR}/debian/tmp" \
	    RUBYINSTALL="${CURDIR}/debian/tmp/usr/lib/\$${RUBYPLATFORM}/ruby/vendor_ruby/\$${RUBYLIBVER}";
endif

override_dh_auto_clean:
ifneq ($(DEB_STAGE),stage1)
	for version in $(PYVERSIONS); do              \
	  $(MAKE) clean PYTHON=python$$version;  \
	done;
endif

override_dh_install:
	dh_install --list-missing

override_dh_fixperms:
	dh_fixperms
	-chmod -x ${CURDIR}/debian/python-semanage/usr/share/pyshared/semanage.py

override_dh_auto_test:
	$(MAKE) test || true
