#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

export TZ=UTC

include /usr/share/dpkg/default.mk

%:
	dh $@

paks := $(shell cat debian/$(DEB_SOURCE).paks)

# We cannot apply dh_strip_nondeterminism to the PK3 files because
# it would sort the files inside the archive, and the idTech3 engine
# is sensitive to order: an archive with re-ordered files is not
# considered network-compatible. The order of files is arbitrary but
# deterministic, and is chosen to match the arbitrary order that
# is present in upstream binary releases.
override_dh_strip_nondeterminism:
	dh_strip_nondeterminism -X.pk3

override_dh_auto_build: $(patsubst %,build/%,$(paks))
	:

debian/%.pk3.contents.raw: debian/%.pk3.contents
	sed -n -e 's/#.*//; /./p;' < $< > $@

debian/%.pk3.filenames: debian/%.pk3.contents
	sed -n -e 's/#.*//; s/.*\t//p;' < $< > $@

debian/%.pk3.qvms: debian/%.pk3.contents
	sed -n -e 's/#.*//; s/\.qvm$$//p;' < $< > $@

build/%: debian/%.pk3.qvms debian/%.pk3.filenames debian/%.pk3.contents.raw
	set -e; while read crc filename; do \
			test -f $*-$$crc.qvm || \
			$(MAKE) -f debian/rules QVMPREFIX=$(*F) QVMCRC=$$crc \
				debian/$(*F)-$$crc.qvm; \
			install -d $*/vm; \
			install -v -m644 debian/$(*F)-$$crc.qvm $*/$$filename.qvm; \
			touch --date='@$(SOURCE_DATE_EPOCH)' $*/$$filename.qvm; \
			touch --date='@$(SOURCE_DATE_EPOCH)' $*/vm; \
		done < debian/$*.pk3.qvms
	rm -f $*.pk3
	( set -e; cd $*; while read crc filename; do \
		if test $$crc = directory; then \
			chmod 0755 "$$filename"; \
		else \
			chmod 0644 "$$filename"; \
		fi; \
	done ) < debian/$*.pk3.contents.raw
	( cd $* && zip -X -o -q -9 $(CURDIR)/$*.pk3 -@ ) < \
		debian/$*.pk3.filenames

override_dh_auto_test: $(patsubst %,check/%,$(paks))
	:

check/pak0: build/pak0
	grep "NTVEpak0" pak0/vm/cgame.qvm
	grep "NTVEpak0" pak0/vm/qagame.qvm
	grep "NTVEpak0" pak0/vm/ui.qvm

check/mp-pak0: build/mp-pak0
	grep "NTVEmp-pak0" mp-pak0/vm/cgame.qvm
	grep "NTVEmp-pak0" mp-pak0/vm/qagame.qvm
	grep "NTVEmp-pak0" mp-pak0/vm/ui.qvm

check/pak6-patch085: build/pak6-patch085
	grep "NTVEpak6-patch085" pak6-patch085/vm/cgame.qvm
	grep "NTVEpak6-patch085" pak6-patch085/vm/qagame.qvm
	grep "NTVEpak6-patch085" pak6-patch085/vm/ui.qvm

check/pak6-patch088: build/pak6-patch088
	grep "NTVEpak6-patch088" pak6-patch088/vm/cgame.qvm
	grep "NTVEpak6-patch088" pak6-patch088/vm/qagame.qvm
	grep "NTVEpak6-patch088" pak6-patch088/vm/ui.qvm

# otherwise there's nothing to check
check/%: build/%
	:

# check that the PK3s are still compatible (do this as late as
# possible so that they aren't going to be modified further)
override_dh_builddeb: $(patsubst %,check-compat/%,$(paks))
	dh_builddeb

check-compat/%: debian/%.pk3.contents.raw
	perl debian/list-pk3.pl \
		debian/*/usr/share/games/openarena/*/$*.pk3 | \
		sed -n -e 's/#.*//; /./p' | \
		diff -u debian/$*.pk3.contents.raw -

build-indep: debian/openarena-088-data.README.Debian
debian/openarena-088-data.README.Debian: debian/qvms.README.Debian
	install -m644 $< $@

build-indep: debian/openarena-085-data.README.Debian
debian/openarena-085-data.README.Debian: debian/qvms.README.Debian
	install -m644 $< $@

build-indep: debian/openarena-data.README.Debian
debian/openarena-data.README.Debian: debian/qvms.README.Debian
	install -m644 $< $@

ifeq ($(filter-out openarena-oacmp1,$(DEB_SOURCE)),)
build-indep: oacmp-volume1-v3/docs/README
oacmp-volume1-v3/docs/README: oacmp-volume1-v3/docs/OACMP-Readme.txt
	install -m644 $< $@
endif

# order-only dependency: once we've generated it, no need to waste time
# generating it again, it'll come out the same
debian/$(QVMPREFIX)-$(QVMCRC).qvm: | debian/qvmbrute
	./debian/qvmbrute 0x$(QVMCRC) $@ $(QVMPREFIX)

debian/qvmbrute: | debian/qvmbrute.c
	$(DEB_BUILD_GNU_TYPE)-gcc $(CPPFLAGS) $(CFLAGS) -Wall -o debian/qvmbrute debian/qvmbrute.c $(LDFLAGS) -lz

tarball := $(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.xz
tardir := $(DEB_SOURCE)-$(DEB_VERSION_UPSTREAM).orig

ifeq ($(filter-out openarena-oacmp1,$(DEB_SOURCE)),)
sources = oacmp-volume1-v3
import-orig: pre-import-orig $(patsubst %,import-orig-zip/%,$(sources))
	:
else
sources = source
import-orig: pre-import-orig $(patsubst %,import-orig-pk3/%,$(paks))
	:
endif

pre-import-orig:
	rm -rf $(sources)

import-orig-pk3/%: pre-import-orig
	@if ! test -f $*.pk3; then \
		echo "Copy upstream pk3 file to $*.pk3";\
		exit 1;\
	fi
	rm -f debian/$*.pk3.contents
	rm -rf ./$*
	install -d $*
	perl debian/list-pk3.pl $*.pk3 > debian/$*.pk3.contents
	unzip -q $*.pk3 -x '*.qvm' '../*' -d $*
	set -e && if test mp-pak0 = $*; then \
		unzip -o -j mp-pak0.pk3 ../COPYING ../CREDITS; \
	fi
	# r901 is 0.8.1; r931 is 0.8.5 (despite the commit message)
	# r947 is 0.8.8
	# source/assets/textures/trak/ are not GPL-compatible
	set -e; for r in 901 931 947; do \
		if ! test -e debian/$*.svn$$r; then \
			continue; \
		fi; \
		install -d debian/svn-r$$r/source; \
		test -d debian/svn-r$$r/source/assets || svn export -r$$r \
			https://openarena.ws/svn/source/assets \
			debian/svn-r$$r/source/assets; \
		rm -rf debian/svn-r$$r/source/assets/textures/trak/; \
		while read s; do \
			rsync --link-dest=debian/svn-r$$r \
				--files-from=debian/$*.svn$$r -r \
				debian/svn-r$$r/ ./; \
		done < debian/$*.svn$$r; \
	done

import-orig-zip/%:
	@if ! test -f $*.zip; then \
		echo "Copy upstream download to $*.zip";\
		exit 1;\
	fi
	unzip $*.zip -d $*
	mv $*/*/*.pk3 .

export-orig:
	rm -rf $(tardir)
	install -d $(tardir)
	set -e && for x in $(paks) $(sources); do \
		cp -al $$x $(tardir)/$$x; \
	done
ifneq ($(filter-out openarena-oacmp1,$(DEB_SOURCE)),)
	install -m644 CHANGES COPYING CREDITS CREDITS-0.8.5 CREDITS-0.8.8 LINUXNOTES README WENEED readme_085.txt readme_088.txt $(tardir)
endif
	tar -Jvcf ../$(tarball) $(tardir)
	rm -rf $(tardir)