#!/usr/bin/make -f
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

#export DH_VERBOSE=1

DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
# If you manage to fix the issues mentioned in README.source, you should
# probably replace the current CXXFLAGS variable with the old one again and
# remove src/modules/adonthell.py src/py_adonthell_wrap.cc in override_dh_clean.
# CXXFLAGS += -Wall -fno-exceptions -DSWIG_GLOBAL -DDATA_DIR=\"\\\"/usr/share/games/adonthell\"\\\"
CXXFLAGS += -Wall -fno-exceptions -DDATA_DIR=\"\\\"/usr/share/games/adonthell\"\\\"
CFGDEBUG = ""
INSTALL = /usr/bin/install -c
INSTALL_PROGRAM = ${INSTALL} -p -o root -g root  -m 755

PYVERSIONNN:=$(shell pyversions -d -v)
PYVERSION :=python$(PYVERSIONNN)

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CXXFLAGS += -g
	CFGDEBUG = "--enable-py-debug"
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CXXFLAGS += -O0
else
  CXXFLAGS += -O2
endif

%:
	dh $@ --parallel --with autoreconf,python2

override_dh_auto_configure:
	dh_auto_configure -- \
		--host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--bindir=\$${prefix}/games \
		--datadir=\$${prefix}/share/games \
		--with-py-libs="-Wl,-E -L/usr/lib/$(PYVERSION)/config -l$(PYVERSION) -lpthread -lSDL -lSDL_ttf -lSDL_mixer" \
		--with-py-cflags="-I/usr/include/$(PYVERSION)" \
		$(CFGDEBUG)


override_dh_auto_build:
	$(MAKE) CXXFLAGS="$(CXXFLAGS)" CPPFLAGS="$(CPPFLAGS)" INSTALL_PROGRAM="$(INSTALL_PROGRAM)"


override_dh_auto_install:
	$(MAKE) install DESTDIR="$(CURDIR)/debian/adonthell" INSTALL_PROGRAM="$(INSTALL_PROGRAM)"
	#Remove the symlink and change binary name back to adonthell
	rm -f $(CURDIR)/debian/adonthell/usr/games/adonthell
	mv $(CURDIR)/debian/adonthell/usr/games/adonthell-0.3 \
		$(CURDIR)/debian/adonthell/usr/games/adonthell
	# Remove empty directories
	find . -type d -empty -delete

override_dh_clean:
	[ ! -f Makefile ] || $(MAKE) distclean
	-rm -f `find . -name "*~"`
	-rm -f src/adonthell.pyc
	rm -f config.guess config.sub
	# drop files generated by SWIG
	#rm -f src/modules/adonthell.py src/py_adonthell_wrap.cc
	dh_clean
