#!/usr/bin/make -f
#
# Created 20 December 1997 by Enrique Zanardi <ezanardi@ull.es>
# Modified 2002-2011 by Alastair McKinstry, <mckinstry@debian.org>
#
# Copying and modification is unlimited, provided that the modified
# version is marked as being modified.

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

# Magic debhelper rule.
%:
ifeq ($(filter stage1,$(DEB_BUILD_PROFILES)),)
	dh $@ --with python3
else
	dh $@ 
endif

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
LIBDIR:=/usr/lib/$(DEB_HOST_MULTIARCH)
DEB_HOST_ARCH_OS  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
PY3VERS:=$(shell py3versions --supported)

AM_VERS:=$(strip $(shell dpkg-query -f '$${source:Upstream-Version}' -W automake | egrep -o '^[0-9]+\.[0-9]+'))

ifeq ($(DEB_HOST_ARCH_OS),hurd)
GPMSUPPORT=
else
GPMSUPPORT= --with-gpm-support
endif

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
NOSTRIP=--with-nostrip=no
else
NOSTRIP=--with-nostrip=yes
endif

COLORSFILE = --with-colorsfile=/etc/newt/palette

override_dh_auto_clean:
	dh_auto_clean
	# remove autogenerated files
	rm -rf Makefile config.h* config.log config.status .depend libnewt.pc \
	   configure install-sh po/*.mo python* tutorial libnewt_pic.a po/newt.pot
	rm -f debian/shlibs.local

override_dh_auto_configure:
	# Nasty hack. why is it necessary?
	cp /usr/share/automake-$(AM_VERS)/install-sh ./install-sh
	dh_auto_configure  -- $(GPMSUPPORT) $(NOSTRIP) $(COLORSFILE) CFLAGS='-I/usr/include/tcl8.6 $(CFLAGS) -DMARCH=\"$(DEB_HOST_MULTIARCH)\" '  \
		CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" FFLAGS="$(FFLAGS)" 

override_dh_auto_build:
	dh_auto_build
	mkdir -p tutorial
	(cd tutorial && docbook2html ../tutorial.sgml )
	ar cqv libnewt_pic.a shared/*.o

override_dh_auto_install:
	dh_auto_install 
ifeq ($(filter stage1,$(DEB_BUILD_PROFILES)),)
	for v in $(PY3VERS); do \
               pylib=usr/lib/python3/dist-packages ; \
               abitag=.$$($$v -c "import sysconfig; print(sysconfig.get_config_var('SOABI'))"); \
               mkdir -p debian/python3-newt/$$pylib ; \
               if echo x$$abitag | grep -q "$(DEB_HOST_MULTIARCH)"; then \
                mv $$v/_snack.so $$v/_snack$$abitag.so; \
              else \
                 mv $$v/_snack.so $$v/_snack$$abitag-$(DEB_HOST_MULTIARCH).so; \
              fi; \
               cp snack.py $$v/* debian/python3-newt/$$pylib; \
        done
	-find debian/python-* debian/python3-* -name '*.o' | xargs rm -f
endif
	dh_install -p libnewt-dev libnewt.a $(LIBDIR)
	mkdir -p debian/libnewt-pic/$(LIBDIR)
	cp newt*.ver debian/libnewt-pic/$(LIBDIR)/libnewt_pic.map

override_dh_makeshlibs:
	dh_makeshlibs -a -V 
