#!/usr/bin/make -f
PKD   = $(abspath $(dir $(MAKEFILE_LIST)))
PKG   = $(shell dpkg-parsechangelog -l$(PKD)/changelog -SSource)
VER  = $(shell dpkg-parsechangelog -l$(PKD)/changelog -SVersion | cut -d- -f1)
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# Needed for cross-compiling.
DEB_HOST_GNU_TYPE=$(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

# Needed for deleting a directory when running rules clean after a build
DPKG_ARCH=$(shell dpkg-architecture -qDEB_HOST_ARCH)

# nostrip option implies noopt as well for this package.
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
   DEBUG = NO
else
   DEBUG = YES
endif

# Needed for deleting directory created when running rules clean twice on
# x86_64 and other architectures.
UNAME_ARCH=$(shell uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc/ -e s/sparc64/sparc/ -e s/arm.*/arm/ -e s/sa110/arm/ -e s/alpha/axp/)

# This is used convert files to be patched from dos to unix EOL convention.
from-dos:
	dh_testdir
	#fromdos -e source/source/gameshared/q_shared.h
	#fromdos -e warsow_0.50_src/source/client/cl_main.c
	touch fromdos-stamp

patch: from-dos

build: patch build-stamp
build-stamp:
	dh_testdir
	#cd source/source/ && $(MAKE) CFLAGS="-O0" LOCALBASE=/usr DEBUG_BUILD=$(DEBUG) \
	#	CC=$(DEB_HOST_GNU_TYPE)-gcc LD=$(DEB_HOST_GNU_TYPE)-gcc
	mkdir -p source/source/cmake_build
	cd source/source/cmake_build && cmake -DQFUSION_GAME="Warsow" .. && $(MAKE)
	touch build-stamp

clean: clean-patched to-dos
clean-patched:
	dh_testdir
	dh_testroot
	#cd source/source && $(MAKE) clean
	rm -rf source/source/cmake_build
	cd source/libsrcs/angelscript/sdk/angelscript/projects/gnuc && \
	  $(MAKE) clean
	rm -rf source/source/build warsow-0.6-src/source/release \
		source/source/build$(UNAME_ARCH) \
		source/source/build$(DPKG_ARCH) \
		source/source/release/
	[ ! -f warsow128x128.png ] || rm warsow128x128.png
	[ ! -f 0.4_renderer_changes.png ] || rm 0.4_renderer_changes.png
	dh_clean
	[ ! -f build-stamp ] || rm build-stamp

# Reverses from-dos target
to-dos:
	dh_testdir
	#todos -e source/source/gameshared/q_shared.h
	#todos -e warsow_0.50_src/source/client/cl_main.c
	[ ! -f fromdos-stamp ] || rm fromdos-stamp

install: build
	dh_testdir
	dh_testroot
	dh_prep
	uudecode debian/warsow128x128.png.uu
	uudecode debian/documentation/0.4_renderer_changes.png.uu
	dh_install

binary-indep:
#Nothing to be done for binary-indep

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs debian/documentation/changelog.txt
	dh_installdocs
	dh_link -pwarsow usr/lib/games/warsow/basewsw/libcgame_$(UNAME_ARCH).so usr/share/games/warsow/basewsw/libcgame_$(UNAME_ARCH).so
	dh_link -pwarsow usr/lib/games/warsow/basewsw/libgame_$(UNAME_ARCH).so usr/share/games/warsow/basewsw/libgame_$(UNAME_ARCH).so
	#dh_link -pwarsow usr/lib/games/warsow/basewsw/libui_$(UNAME_ARCH).so usr/share/games/warsow/basewsw/libui_$(UNAME_ARCH).so
	dh_link -pwarsow usr/lib/games/warsow/libs usr/share/games/warsow/libs
	dh_installmenu
	dh_installman -pwarsow debian/warsow.6
	dh_installman -pwarsow-server debian/warsow-server.6 \
		debian/warsowtv-server.6
#ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	dh_strip --dbg-package=warsow-dbg
#endif
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-arch
.PHONY: build binary-indep binary-arch binary install clean get-orig-source

## http://wiki.debian.org/onlyjob/get-orig-source
get-orig-source: $(PKG)_$(VER).orig.tar.xz $(info I: $(PKG)_$(VER))
	@

$(PKG)_$(VER).orig.tar.xz:
	@echo "# Downloading..."
	uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-version $(VER) $(PKD)
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	@echo "# Extracting..."
	mkdir $(PKG)-$(VER) \
	&& tar -xf $(PKG)_$(VER).orig.tar.* --directory $(PKG)-$(VER) --strip-components 1 \
	|| $(RM) -r $(PKG)-$(VER)
	@echo "# Cleaning-up..."
	cd $(PKG)-$(VER) \
	&& find . -type f -iname *.bat -exec $(RM) -r {} \; -printf 'removed %p\n' \
	&& $(RM) -r -v \
	    ./source/libsrcs/SDL2/premake \
            ./source/libsrcs/SDL2/test \
            ./source/libsrcs/libRocket/libRocket/Samples \
	    ./source/source/win32 \
	    ./source/libsrcs/libfreetype/docs/VERSION.DLL \
	    ./ktx_sources \
	    ./mapping \
	    ./maps \
	    ./modelling \
	    ./models \
	    ./normalmaps \
	    ./oldprogs \
	    ./textures
	#$(RM) -v $(PKG)_$(VER).orig.tar.*
	@echo "# Packing..."
	find -L "$(PKG)-$(VER)" -xdev -type f -print | LC_ALL=C sort \
	| XZ_OPT="-6v" tar -caf "$(PKG)_$(VER)$(DTYPE).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
	&& $(RM) -r "$(PKG)-$(VER)"

