#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+all

%:
	dh $@ --parallel

override_dh_auto_configure:
	chmod u+x game2/configure
	cd game2 && ./configure --linux

override_dh_auto_build:
	convert -type Grayscale -negate ./game2/build/macOSX/icon128_mask.png mask.png
	composite -compose CopyOpacity mask.png ./game2/build/macOSX/icon128_color.png cultivation.png
	mkdir -p 32x32
	convert -scale 32x32 cultivation.png 32x32/cultivation.png
	convert 32x32/cultivation.png 32x32/cultivation.xpm
	sed -i -e 's/-lX11//' game2/gameSource/Makefile
	sed -i -e 's/^DEBUG_FLAG = .*/DEBUG_FLAG = /' game2/gameSource/Makefile
	sed -i -e 's/^OPTIMIZE_FLAG = .*/OPTIMIZE_FLAG = /' game2/gameSource/Makefile
	sed -i -e 's/^COMPILE_FLAGS = /COMPILE_FLAGS = $$(CFLAGS) $$(CPPFLAGS) /' game2/gameSource/Makefile
	sed -i -e 's/^LINK_FLAGS = /LINK_FLAGS = $$(LDFLAGS) /' game2/gameSource/Makefile
	$(MAKE) -C game2/gameSource LDFLAGS="$(LDFLAGS)" CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CFLAGS) -DDATADIR=\\\"/usr/share/games/cultivation\\\""

override_dh_auto_clean:
	[ ! -f game2/gameSource/Makefile ] || $(MAKE) -C game2/gameSource clean
	rm -f game2/gameSource/Makefile
	rm -f game2/gameSource/Makefile.minorGems_dependencies
	rm -rf mask.png cultivation.png 32x32

override_dh_installchangelogs:
	dh_installchangelogs game2/documentation/changeLog.txt

V=9
P=Cultivation
p=cultivation
sfp=cultivation/cultivation

get-orig-source:
	wget -c http://downloads.sourceforge.net/project/$(sfp)/v$(V)/$(P)_$(V)_UnixSource.tar.gz
	tar zxf $(P)_$(V)_UnixSource.tar.gz
	# Embedded code copies
	rm -r $(P)_$(V)_UnixSource/minorGems/sound/portaudio
	# Embedded code copies, unused
	rm -r $(P)_$(V)_UnixSource/minorGems/network/upnp/miniupnpc
	rm $(P)_$(V)_UnixSource/minorGems/graphics/converters/lodepng.*
	# Pre-built PDF, unused
	rm $(P)_$(V)_UnixSource/game2/documentation/futurePlay/poster.pdf
	# Pre-built Windows libraries, unused
	rm $(P)_$(V)_UnixSource/game2/build/win32/*.dll
	# non-free, unused
	rm -r $(P)_$(V)_UnixSource/minorGems/util/development/fortify
	rm $(P)_$(V)_UnixSource/minorGems/graphics/openGL/tga.*
	rm $(P)_$(V)_UnixSource/minorGems/graphics/openGL/texture.*
	mv $(P)_$(V)_UnixSource $(p)-$(V)+dfsg1.orig
	tar zcf ../$(p)_$(V)+dfsg1.orig.tar.gz $(p)-$(V)+dfsg1.orig
	rm -r $(P)_$(V)_UnixSource.tar.gz $(p)-$(V)+dfsg1.orig