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

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif

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)
debian_version     := $(shell dpkg-parsechangelog -c1 | sed -ne 's/^Version: //p')

CFLAGS += -fsigned-char


%:
	dh $@

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

override_dh_auto_build:
	$(MAKE) \
		BR=build \
		BD=build \
		V=1 \
		USE_CODEC_VORBIS=1 \
		USE_CURL=1 \
		USE_CURL_DLOPEN=0 \
		USE_OPENAL=1 \
		USE_OPENAL_DLOPEN=0 \
		USE_VOIP=1 \
		USE_INTERNAL_SPEEX=0 \
		USE_INTERNAL_ZLIB=0 \
		USE_INTERNAL_JPEG=0 \
		USE_LOCAL_HEADERS=0 \
		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=$(debian_version)/$(distro) \
		CFLAGS="$(CFLAGS)" \
		$(TARGET)
	install build/ioq3ded.* build/ioq3ded
	install build/ioquake3.* build/ioquake3

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

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

ORIG_REL = 1.36
ORIG_REV = 2224
# SUFFIX can be made non-empty for re-repacks
ORIG_SUFFIX =

ifeq (${ORIG_REV},)
	ORIG_VER := ${ORIG_REL}${ORIG_SUFFIX}
	ORIG_EXPORT := svn://svn.icculus.org/quake3/tags/${ORIG_VER}
else
	ORIG_VER := ${ORIG_REL}+svn${ORIG_REV}${ORIG_SUFFIX}
	ORIG_EXPORT := -r ${ORIG_REV} svn://svn.icculus.org/quake3/trunk
endif

get-orig-source:
	svn export ${ORIG_EXPORT} ioquake3-${ORIG_VER}.orig
	rm -rf ioquake3-${ORIG_VER}.orig/.svnignore
	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/libs
	rm -rf ioquake3-${ORIG_VER}.orig/code/libspeex
	rm -rf ioquake3-${ORIG_VER}.orig/code/zlib
	tar -zcvf ioquake3_${ORIG_VER}.orig.tar.gz ioquake3-${ORIG_VER}.orig
	rm -rf ioquake3-${ORIG_VER}.orig
