#! /usr/bin/make -f
#export DH_VERBOSE=1

LDFLAGS += -Wl,-z,defs -Wl,-O1 -Wl,--as-needed
LIBS += -lboost_system

ifneq (,$(findstring nomousecapture,$(DEB_BUILD_OPTIONS)))
  CXXFLAGS += -DDISABLE_MOUSE_CAPTURE
endif

debian/funguloids.xpm: bin/funguloids.png
	convert $^ -resize 32x32 $@

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

override_dh_auto_configure:
	LIBS="$(LIBS)" CXXFLAGS="$(CXXFLAGS)" dh_auto_configure -- --with-fmod=no
	
	chmod +x debian/mpak.py
	
	if test ! -e bin/bootstrap.mpk.orig ; then \
		cp bin/bootstrap.mpk bin/bootstrap.mpk.orig ; \
		./debian/mpak.py -e -f bin/bootstrap.mpk  -p _bootstrap ; \
		sed -ri '/^[A-Z]/ s/(.*)/overlay \1/' _bootstrap/*.overlay ; \
		./debian/mpak.py -c -f bin/bootstrap.mpk  _bootstrap/* ; \
		rm -rf _bootstrap ; \
	fi
	
	if test ! -e bin/funguloids.mpk.orig ; then \
		cp bin/funguloids.mpk bin/funguloids.mpk.orig ; \
		./debian/mpak.py -e -f bin/funguloids.mpk -p _gamedata ; \
		sed -ri '/^[A-Z]/ s/(.*)/overlay \1/' _gamedata/*.overlay ; \
		sed -ri '/^[A-Z]/ s/(.*)/particle_system \1/' _gamedata/*.particle ; \
		sed -ri 's/^(\t\t\t)(texture_unit) 1/\1\2\n\1{\n\1}\n\1\2/' _gamedata/materials.material ; \
		./debian/mpak.py -c -f bin/funguloids.mpk _gamedata/* ; \
		rm -rf _gamedata ; \
	fi

override_dh_auto_clean:
	dh_auto_clean
	
	if test -e bin/bootstrap.mpk.orig ; then \
		mv bin/bootstrap.mpk.orig bin/bootstrap.mpk ; \
	fi
	
	if test -e bin/funguloids.mpk.orig ; then \
		mv bin/funguloids.mpk.orig bin/funguloids.mpk ; \
	fi

override_dh_install:
	dh_install -a /usr/share/games/funguloids/plugins.cfg
	dh_install -Xplugins.cfg --list-missing

override_dh_builddeb:
	dh_builddeb -- -Zxz
