#!/usr/bin/make -f

# output every command that modifies files on the build system.
#DH_VERBOSE = 1

%:
	dh $@ --sourcedirectory=neo --parallel

# debmake generated override targets
# This is example for Cmake (See http://bugs.debian.org/641051 )
override_dh_auto_configure:
	dh_auto_configure -- -G "Unix Makefiles" \
	 -DSDL2=ON -DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH) \
	 -DUSE_SYSTEM_ZLIB=ON -DUSE_SYSTEM_LIBPNG=ON \
	 -DUSE_SYSTEM_LIBJPEG=ON -DUSE_SYSTEM_LIBGLEW=ON

         #-DCMAKE_BUILD_TYPE=RelWithDebInfo

# For get-orig-source
UURL =  https://github.com/RobertBeckebans/RBDOOM-3-BFG.git
PKD   = $(abspath $(dir $(MAKEFILE_LIST)))
PKG   = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
VER  ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog  -SVersion | sed -e s/^.*git// -e s/+repack// | cut -d- -f1 )
DTYPE = +repack
UPSVER ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog -SVersion | cut -d~ -f1)

## from: https://wiki.debian.org/onlyjob/get-orig-source
.PHONY: get-orig-source
get-orig-source: $(PKG)_$(UPSVER)~git$(VER)$(DTYPE).orig.tar.xz
	#$(info I: $(UPSVER$)~$(PKG)_$(VER)$(DTYPE))
	@

UDATE = $(shell date --rfc-3339=seconds --date='TZ="UTC" \
	$(shell echo ~git$(VER) | perl -ne 'print "$$1-$$2-$$3" if m/\~(?:git|svn|hg)(\d{4})(\d{2})(\d{2})/')')

$(PKG)_$(UPSVER)~git$(VER)$(DTYPE).orig.tar.xz:
	@echo DEBUG: VER=$(VER) UDATE=$(UDATE) UPSVER=$(UPSVER)
	echo "$(PKG)_$(UPSVER)~git$(VER)$(DTYPE).orig.tar.xz"
	#$(info I: UDATE=$(UDATE))
	$(if $(wildcard $(PKG)-$(UPSVER)~git$(VER)),$(error $(PKG)-$(UPSVER)~git$(VER) exist, aborting..))
	@echo "# Downloading..."
	git clone $(UURL) $(PKG)-$(UPSVER)~git$(VER) \
	|| $(RM) -r $(PKG)-$(UPSVER)~git$(VER)
	cd $(PKG)-$(UPSVER)~git$(VER) \
	&& git checkout v$(VER) || git checkout $$(git log -n1 --format=%h --before="$(UDATE)") \
	&& ( echo "# Generating ChangeLog..." \
	&& git log --pretty="format:%ad  %aN  <%aE>%n%n%x09* %s%n" --date=short > ChangeLog \
	&& touch -d "$$(git log -1 --format='%ci')" ChangeLog) \
	&& echo "# Setting times..." \
	&& for F in $$(git ls-tree -r --name-only HEAD); do touch --no-dereference -d "$$(git log -1 --format="%ai" -- $$F)" "$$F"; done \
	&& echo "# Cleaning-up..." \
	&& $(RM) -r -v \
	  neo/libs/openal-soft/win32/OpenAL32.dll neo/*.exe \
	  neo/libs/ffmpeg-win*/ neo/sys/win32 neo/libs/mingw-hacks \
	  neo/libs/zlib/*.c neo/libs/zlib/*.h neo/libs/zlib/README \
	  neo/libs/zlib/Makefile* neo/libs/zlib/zconf.h.included \
	  neo/libs/zlib/INDEX neo/libs/zlib/ChangeLog neo/libs/zlib/configure \
	  neo/libs/zlib/FAQ neo/libs/zlib/algorithm.txt \
	  neo/libs/zlib/zconf.h.in neo/libs/zlib/zlib.3 \
	  neo/libs/png \
	  neo/libs/jpeg-6 \
	  neo/libs/glew \
	  neo/libs/openal-soft \
	  neo/astyle.x86 neo/astyle.x86_64 \
	&& $(RM) -r .git .git*
	@echo "# Packing..."
	find -L "$(PKG)-$(UPSVER)~git$(VER)" -xdev -type f -print | sort \
	| XZ_OPT="-6v" tar -caf "$(PKG)_$(UPSVER)~git$(VER)$(DTYPE).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
	&& $(RM) -r "$(PKG)-$(UPSVER)~git$(VER)"
