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

export DEB_BUILD_HARDENING=1
export DEB_BUILD_HARDENING_FORMAT=0

CFLAGS = -Wall -g
INSTALL = install
INSTALL_FILE    = $(INSTALL) -p    -o root -g root  -m  644
INSTALL_PROGRAM = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_SCRIPT  = $(INSTALL) -p    -o root -g root  -m  755
INSTALL_DIR     = $(INSTALL) -p -d -o root -g root  -m  755

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
	else
	CFLAGS += -O2
endif
ifeq (,$(filter nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif
#ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
#	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
#	MAKEFLAGS += -j$(NUMJOBS)
#endif

PYVERS=$(shell pyversions -vr)

TMPDIR=debian/tmp

prefix=$(CURDIR)/debian/tmp/usr

configure: configure-stamp
configure-stamp:
	dh_testdir
	./configure --prefix=/usr --mandir=\$${prefix}/share/man --enable-gtk-doc=no --localstatedir=/var --sysconfdir=/etc --with-perl-installdirs=vendor --enable-easy-bindings
	# force swig to create files
	touch bindings/low-level/libprelude.i
	touch bindings/libpreludecpp.i
	touch configure-stamp

build: build-arch build-indep $(PYVERS:%=build-python%)
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir
	$(MAKE)
	touch build-stamp

build-python%: build-stamp
	cd bindings/python && python$* setup.py build
	cd bindings/low-level/python && python$* setup.py build
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp build-python2.*
	[ ! -d bindings/low-level/python/build ] || rm -rf bindings/low-level/python/build
	[ ! -f bindings/perl/Makefile ] || touch bindings/perl/Makefile
	[ ! -f bindings/perl/Makefile ] || $(MAKE) -C bindings/perl realclean
	[ ! -f bindings/low-level/perl/Makefile ] || $(MAKE) -C bindings/low-level/perl realclean
	# work around makefile bug
	[ ! -f bindings/Makefile ] || make -C bindings perl-makefile
	# force -j1, the distclean target fails in parallel mode
	[ ! -f Makefile ] || $(MAKE) -j1 distclean
	rm -rf bindings/python/build
	rm -f config.log
	dh_clean

install: install-core $(PYVERS:%=install-python%)
	find debian/tmp -type f -name .packlist | xargs rm -f
	find debian/tmp/usr/lib -name "*.la" -exec \
		sed -i -e "s,^dependency_libs=.*,dependency_libs=''," {} +

install-core: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	$(INSTALL_DIR) $(TMPDIR)/etc/prelude/	\
		$(TMPDIR)/etc/prelude/default/	\
		$(TMPDIR)/etc/prelude/profile/

install-python%: build-stamp
	cd bindings/python && python$* setup.py install --install-layout=deb --root $(CURDIR)/debian/python-prelude
	cd bindings/low-level/python && python$* setup.py install --install-layout=deb --root $(CURDIR)/debian/python-prelude

binary-indep: build install

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install
	dh_installdocs README
	dh_installchangelogs ChangeLog
	dh_installman
	dh_perl
	#dh_python
	dh_python2 -ppython-prelude
	dh_link
	dh_strip --dbg-package=libprelude2-dbg
	dh_compress -X.py
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps debian/libprelude2/usr/bin/prelude-admin \
		debian/libprelude-dev/usr/bin/libprelude-config
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install configure
