#!/usr/bin/make -f

#export DH_VERBOSE=1

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
CXXFLAGS = -Wall -fno-exceptions -DSWIG_GLOBAL -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

configure: configure-stamp
configure-stamp:
	dh_testdir


	-test -r /usr/share/misc/config.sub && \
	  cp -f /usr/share/misc/config.sub config.sub
	-test -r /usr/share/misc/config.guess && \
	  cp -f /usr/share/misc/config.guess config.guess

	./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) -ldl -lpthread -lutil -lSDL -lSDL_ttf -lSDL_mixer" \
                    --with-py-cflags="-I/usr/include/$(PYVERSION)" \
                    $(CFGDEBUG)
	touch configure-stamp


build: configure-stamp build-stamp

build-stamp: 
	dh_testdir

	$(MAKE) CXXFLAGS="$(CXXFLAGS)" INSTALL_PROGRAM="$(INSTALL_PROGRAM)"

	touch build-stamp

clean: 
	dh_testdir
	dh_testroot
	rm -f configure-stamp build-stamp 

	[ ! -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

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(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

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installmenu
	dh_installman debian/adonthell.6
	dh_installchangelogs ChangeLog
	dh_pysupport -V $(PYVERSIONNN) adonthell /usr/share/games/adonthell/modules/
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

