#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

DESTDIR := $(CURDIR)/debian/tmp
PYVER=$(shell pyversions -vd)
PYVERS=$(filter-out $(PYVER),$(shell pyversions -vr)) $(PYVER)

configure: configure-stamp
configure-stamp:
	rm -f SCustomize

build: configure $(PYVERS:%=build-python%-stamp)

clean:
	dh_testdir
	dh_testroot
	-scons -c
	rm -f jppy/_jpilot.py
	find . -name \*.pyc -exec rm {} \;
	rm -f config.log
	rm -f scons-signatures-db SCustomize tests_pass .sconsign.dblite
	rm -rf .sconf_temp
	rm -f testhome/.jpilot/*.pc3 testhome/.jpilot/next_id testhome/.jpilot/jpilot.log
	dh_clean

build-python%-stamp:
	# build the binary module for each python we support
	scons prefix=/usr jppyroot=$(DESTDIR) jppyguiroot=$(DESTDIR) jpilotroot=$(DESTDIR) \
	      python_version=$* \
	      python_includes=/usr/include/python$* \
	      mx_datetime_includes=/usr/include/python$*/mx \
	      python_bin_module_prefix=/usr/lib/python$*/site-packages/jppy \
	      python_module_prefix=/usr/lib/python$*/site-packages/jppy \
	      pisock_library=/usr/lib
	scons install
	touch $@

install: build 
	dh_installdirs

	# scons sucks, not only because it dereferences links on install
	for i in address email mobile phone vcard; do \
		ln -sf jp_lookup $(DESTDIR)/usr/bin/jp_$${i}_lookup; \
	done

	install -d -m755 $(DESTDIR)/usr/lib/lbdb
	install -m755 lbdb/m_palm_db6 lbdb/palm_db6_lsaddr \
		$(DESTDIR)/usr/lib/lbdb

binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installmenu -i
	dh_installman -i
	dh_installchangelogs -i
	dh_install -i --sourcedir=$(DESTDIR) --autodest
	dh_pysupport -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir -a
	dh_testroot -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
	dh_installman -a
	dh_installchangelogs -a
	dh_install -a --sourcedir=$(DESTDIR) --autodest
	dh_pysupport -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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