#!/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.
%:
	dh $@ --with python2,python3,autoreconf

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)
PYVERS:=$(shell pyversions --supported)
PY3VERS:=$(shell py3versions --supported)

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-1.*/install-sh ./install-sh
	dh_auto_configure  -- $(GPMSUPPORT) $(NOSTRIP) $(COLORSFILE) CFLAGS="-I/usr/include/tcl8.6 $(CFLAGS)"  \
		CPPFLAGS="$(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" FFLAGS="$(FFLAGS)" 

override_dh_auto_build:
	dh_auto_build
	sgmltools --backend html tutorial.sgml
	ar cqv libnewt_pic.a shared/*.o
	
	
override_dh_auto_install:
	dh_auto_install 
	for v in $(PYVERS); do \
		pylib=usr/lib/$$v/dist-packages ; \
		mkdir -p debian/python-newt/$$pylib ; \
		mkdir -p debian/python-newt-dbg/$$pylib ;\
		mv $$v-dbg/_snack.so debian/python-newt-dbg/$$pylib/_snack_d.so; \
		cp snack.py $$v/* debian/python-newt/$$pylib; \
	done
	for v in $(PY3VERS); do \
               pylib=usr/lib/python3/dist-packages ; \
               abitag=.$$($$v -c "import sysconfig; print(sysconfig.get_config_var('SOABI'))"); \
               abitagdbg=.$$($$v-dbg -c "import sysconfig; print(sysconfig.get_config_var('SOABI'))"); \
               mkdir -p debian/python3-newt/$$pylib ; \
               mkdir -p debian/python3-newt-dbg/$$pylib ;\
	       mv $$v/_snack.so $$v/_snack$$abitag.so; \
               mv $$v-dbg/_snack.so debian/python3-newt-dbg/$$pylib/_snack$$abitagdbg.so; \
               cp snack.py $$v/* debian/python3-newt/$$pylib; \
        done
	-find debian/python-* debian/python3-* -name '*.o' | xargs rm -f
	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_strip:
	dh_strip -Npython-newt -Npython-newt-dbg -Npython3-newt -Npython3-newt-dbg
	dh_strip -ppython-newt --dbg-package=python-newt-dbg
	dh_strip -ppython3-newt --dbg-package=python3-newt-dbg
