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

include /usr/share/dpkg/default.mk

Q3ARCH             := $(shell $(CURDIR)/debian/q3arch arch     HOST)
Q3PLATFORM         := $(shell $(CURDIR)/debian/q3arch platform HOST)
Q3COMPILE_ARCH     := $(shell $(CURDIR)/debian/q3arch arch     BUILD)
Q3COMPILE_PLATFORM := $(shell $(CURDIR)/debian/q3arch platform BUILD)
distro             := $(shell lsb_release -is)

CFLAGS += -fsigned-char -DWITH_OPENARENA $(CPPFLAGS)

%:
	dh $@ --parallel

ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
TARGET = release
else
TARGET = debug
endif

override_dh_auto_build:
	dh_auto_build \
		-- \
		BR=build \
		BD=build \
		V=1 \
		USE_CODEC_OPUS=1 \
		USE_CODEC_VORBIS=1 \
		USE_CURL=1 \
		USE_CURL_DLOPEN=0 \
		USE_INTERNAL_LIBS=0 \
		USE_LOCAL_HEADERS=0 \
		USE_OPENAL=1 \
		USE_OPENAL_DLOPEN=0 \
		USE_VOIP=1 \
		ARCH=$(Q3ARCH) \
		PLATFORM=$(Q3PLATFORM) \
		COMPILE_ARCH=$(Q3COMPILE_ARCH) \
		COMPILE_PLATFORM=$(Q3COMPILE_PLATFORM) \
		BUILD_CLIENT_SMP=1 \
		DEFAULT_BASEDIR=/usr/lib/ioquake3 \
		BUILD_GAME_SO=1 \
		BUILD_GAME_QVM=0 \
		VERSION=$(DEB_VERSION)/$(distro) \
		CFLAGS="$(CFLAGS)" \
		$(TARGET)
	install build/ioq3ded.* build/ioq3ded
	install build/ioquake3.* build/ioquake3
	install README.md build/README

override_dh_strip:
	dh_strip --dbg-package=ioquake3-dbg

# ---------------------------------------------------------------------------

ORIG_REPO ?= https://github.com/ioquake/ioq3/
ORIG_REL = 1.36
# Empty if upstream ever get round to tagging a release
ORIG_REV = ca9eebb
# Use the date of the ORIG_REV, or 20130426.1 if you snapshot twice in a day,
# or empty if ORIG_REV is
ORIG_DATE = 20140802
# Non-empty for re-repacks (+dfsg2 or something)
ORIG_SUFFIX =

ifeq (${ORIG_REV},)
	# I have no idea what tags are going to look like, because there
	# aren't any yet. For now, assume they'll be like "1.37".
	ORIG_VER := ${ORIG_REL}${ORIG_SUFFIX}
	ORIG_EXPORT := ${ORIG_REL}
else
	# I'd use "+gitsomething", but that sorts earlier than
	# "+svn1788", and "++gitsomething" is really ugly. +u sorts later,
	# and can be justified as short for "upstream".
	# "+g" because "+u20130426.gfffffff" sorts later than
	# "+u20130426.1.gfffffff", whereas "+u20130426+gfffffff"
	# sorts earlier than "+u20130426.1+gfffffff" as desired.
	ORIG_VER := ${ORIG_REL}+u${ORIG_DATE}+g${ORIG_REV}${ORIG_SUFFIX}
	ORIG_EXPORT := ${ORIG_REV}
endif

get-orig-source:
	git clone --bare --branch master ${ORIG_REPO} ioquake3-${ORIG_VER}.git
	GIT_DIR=ioquake3-${ORIG_VER}.git git archive \
		--format=tar \
		--prefix=ioquake3-${ORIG_VER}.orig/ \
		${ORIG_EXPORT} | tar -xvf-
	rm -rf ioquake3-${ORIG_VER}.orig/code/AL
	rm -rf ioquake3-${ORIG_VER}.orig/code/SDL12
	rm -rf ioquake3-${ORIG_VER}.orig/code/tools/lcc
	rm -rf ioquake3-${ORIG_VER}.orig/code/jpeg-8c
	rm -rf ioquake3-${ORIG_VER}.orig/code/libcurl*
	rm -rf ioquake3-${ORIG_VER}.orig/code/libogg-*
	rm -rf ioquake3-${ORIG_VER}.orig/code/libs
	rm -rf ioquake3-${ORIG_VER}.orig/code/libspeex
	rm -rf ioquake3-${ORIG_VER}.orig/code/libvorbis-*
	rm -rf ioquake3-${ORIG_VER}.orig/code/opus-*
	rm -rf ioquake3-${ORIG_VER}.orig/code/opusfile-*
	rm -rf ioquake3-${ORIG_VER}.orig/code/zlib
	tar --xz -cvf ioquake3_${ORIG_VER}.orig.tar.xz ioquake3-${ORIG_VER}.orig
	rm -rf ioquake3-${ORIG_VER}.orig
	rm -rf ioquake3-${ORIG_VER}.git
