#!/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 python2,python3,autoreconf
else
	dh $@ --with autoreconf
endif

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

AM_VERS:=1.15

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
	rm -f debian/python*-newt*.examples 

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)"  \
		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 
	mkdir -p debian/tmp/$(LIBDIR)/.
	mv debian/tmp/$(USRLIBDIR)/*.so.* debian/tmp/$(LIBDIR)/.
	ln -sf $(LIBDIR)/$$(readlink debian/tmp/$(USRLIBDIR)/libnewt.so) \
	       debian/tmp/$(USRLIBDIR)/libnewt.so

	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 ;\
               if echo x$$abitag | grep -q "$(DEB_HOST_MULTIARCH)"; then \
                mv $$v/_snack.so $$v/_snack$$abitag.so; \
                 mv $$v-dbg/_snack.so debian/python3-newt-dbg/$$pylib/_snack$$abitagdbg.so; \
              else \
                 mv $$v/_snack.so $$v/_snack$$abitag-$(DEB_HOST_MULTIARCH).so; \
                 mv $$v-dbg/_snack.so debian/python3-newt-dbg/$$pylib/_snack$$abitagdbg-$(DEB_HOST_MULTIARCH).so; \
              fi; \
               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 $(USRLIBDIR)
	mkdir -p debian/libnewt-pic/$(USRLIBDIR)
	cp newt*.ver debian/libnewt-pic/$(USRLIBDIR)/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
