#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

include /usr/share/python/python.mk
PY_INTERPRETER=/usr/bin/python
PYVERS=$(shell pyversions -vr)
PYDEFAULT=$(shell pyversions -vd)

PERL_ARCHLIB := $(shell perl -MConfig -e 'print $$Config{vendorarch}')

# enable hardening flags (for debian/compat<9):
configure_flags += $(shell f=`dpkg-buildflags --export=configure` && echo $$f)

# force verbose build to allow log checkers to work:
configure_flags += --disable-silent-rules

configure: $(PYVERS:%=build-%/configure-stamp)
build-%/configure-stamp:
	dh_testdir
	dh_autoreconf
	mkdir build-$*
	cd build-$* && \
	    PYTHON=/usr/bin/python$* \
	    ../configure \
		$(configure_flags) \
		--prefix=/usr --mandir=\$${prefix}/share/man \
		--with-perl-binding \
		--with-python-binding \
		--enable-tcl-binding \
		--with-tcl=/usr/lib/"$(DEB_HOST_MULTIARCH)"/tcl8.5
	touch $@

build: build-arch build-indep
build-arch: $(PYVERS:%=build-%/build-stamp)
build-indep: build-doc-stamp
build-%/build-stamp: build-%/configure-stamp
	$(MAKE) -C build-$*
	touch $@

build-doc-stamp: build-$(PYDEFAULT)/configure-stamp
	$(MAKE) -C build-$(PYDEFAULT)/doc doc
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -rf build-*
	dh_autoreconf_clean
	dh_clean

install: build install-base $(PYVERS:%=install-%) install-other install-indep
install-base:
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/tmp
	$(MAKE) -C build-$(PYDEFAULT) install DESTDIR=$(CURDIR)/debian/tmp

install-%: build-%/build-stamp
	# Move the appropriate files into the python package
	make -C build-$*/bindings install DESTDIR=$(CURDIR)/debian/tmp
	rm $(CURDIR)/debian/tmp/$(call py_libdir,$*)/Hamlib.py?
	mkdir -p $(CURDIR)/debian/python-libhamlib2/$(call py_libdir,$*)
	mv $(CURDIR)/debian/tmp/$(call py_libdir,$*)/* $(CURDIR)/debian/python-libhamlib2/$(call py_libdir,$*)

install-other:
	# Move the appropriate files into the perl package and do some cleanups
	dh_install -plibhamlib2-perl --sourcedir=$(CURDIR)/debian/tmp $(PERL_ARCHLIB)
	cp $(CURDIR)/bindings/perltest.pl \
		$(CURDIR)/debian/libhamlib2-perl/usr/share/doc/libhamlib2-perl/
	# libhamlib2, libhamlib-dev, libhamlib2++c2, libhamlib++-dev,
	# libhamlib-utils and libhamlib2-tcl files handled by dh_install
	dh_install

install-indep:
	# Move the appropriate files into the documentation package..
	cp -r $(CURDIR)/build-$(PYDEFAULT)/doc/html/* \
		$(CURDIR)/debian/libhamlib-doc/usr/share/doc/hamlib-doc/html

# Build architecture-independent files here.
binary-indep: build-indep install-base install-indep
	dh_testdir
	dh_testroot
	dh_installdocs -X INSTALL -X ChangeLog
	dh_installchangelogs
	dh_compress
	dh_fixperms
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build-arch install-base $(PYVERS:%=install-%) install-other
	dh_testdir
	dh_testroot
	dh_installdocs -X INSTALL -X ChangeLog
	dh_installchangelogs
#	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_perl

	: # Replace all '#!' calls to python with $(PY_INTERPRETER)
	: # and make them executable
	for i in `find debian/python-* -type f`; do \
	  sed '1s,#!.*python[^ ]*\(.*\),#! $(PY_INTERPRETER)\1,' \
		$$i > $$i.temp; \
	  if cmp --quiet $$i $$i.temp; then \
	    rm -f $$i.temp; \
	  else \
	    mv -f $$i.temp $$i; \
	    chmod 755 $$i; \
	    echo "fixed interpreter: $$i"; \
	  fi; \
	done

	dh_python2 -ppython-libhamlib2
	dh_makeshlibs -a -V -Xalinco -Xaor -Xdrake -Xdummy -Xeasycomm -Xflexradio \
	-Xfodtrack -Xgs232a -Xicom -Xjrc -Xkachina -Xkenwood -Xkit -Xlowe \
	-Xmicrotune -Xpcr -Xracal -Xrft -Xrotorez -Xrpc -Xsartek -Xskanti \
	-Xtapr -Xtentec -Xtuner -Xuniden -Xwinradio -Xyaesu -Xkit -Xracal \
	-Xscanti -Xwj -Xtuner -Xrs -Xspid -Xars -Xm2 -Xprm80 -Xamsat -Xts7400 \
	-Xadat -Xcelestron
	dh_installdeb -a
	dh_shlibdeps -a -Xalinco -Xaor -Xdrake -Xdummy -Xeasycomm -Xflexradio \
	-Xfodtrack -Xgs232a -Xicom -Xjrc -Xkachina -Xkenwood -Xkit -Xlowe \
	-Xmicrotune -Xpcr -Xracal -Xrft -Xrotorez -Xrpc -Xsartek -Xskanti \
	-Xtapr -Xtentec -Xtuner -Xuniden -Xwinradio -Xyaesu -Xkit -Xracal \
	-Xscanti -Xwj -Xtuner -Xrs -Xspid -Xars -Xm2 -Xprm80 -Xamsat -Xts7400 \
	-Xadat -Xcelestron
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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