#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

%:
	dh $@

distro := $(shell lsb_release -is)
debian_version := $(shell dpkg-parsechangelog -c1 | sed -ne 's/^Version: //p')

DEB_CFLAGS_MAINT_APPEND := \
	-Wall \
	-Wextra \
	-Wstrict-prototypes \
	-Wsign-compare \
	-Wnested-externs \
	-Wpointer-arith \
	-Wformat-security \
	-Winit-self \
	-Wno-missing-field-initializers \
	-Wno-unused-parameter \
	-fno-strict-aliasing \
	$(NULL)

DEB_CPPFLAGS_MAINT_APPEND := \
	-DSVNREVISION=$(debian_version) \
	-DBUILDTYPE=$(distro) \
	$(NULL)

ifeq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
TARGETS = sdl-release sv-release
else
TARGETS = sdl-debug sv-debug
endif

# LDFLAGS_* are used to compile builddate.c, so yes, they do need to include
# the CPPFLAGS.
override_dh_auto_build:
	$(MAKE) \
		CFLAGS_RELEASE="$(CPPFLAGS) $(CFLAGS)" \
		CFLAGS_DEBUG="$(CPPFLAGS) $(CFLAGS)" \
		LDFLAGS_RELEASE="$(CPPFLAGS) $(CFLAGS) $(LDFLAGS)" \
		LDFLAGS_DEBUG="$(CPPFLAGS) $(CFLAGS) $(LDFLAGS)" \
		LINK_TO_CURL=1 \
		LINK_TO_FREETYPE2=1 \
		LINK_TO_LIBJPEG=1 \
		LINK_TO_LIBVORBIS=1 \
		LINK_TO_MODPLUG=1 \
		LINK_TO_ODE=1 \
		LINK_TO_PNG=1 \
		LINK_TO_THEORA=1 \
		LINK_TO_ZLIB=1 \
		DISABLE_OFFSCREEN_GECKO=1 \
		DISABLE_D0_BLIND_ID=1 \
		DISABLE_D0_RIJNDAEL=1 \
		DISABLE_AVW=1 \
		STRIP=": do not strip" \
		$(TARGETS)

override_dh_auto_install:
	install -d debian/darkplaces/usr/games
	install darkplaces-sdl debian/darkplaces/usr/games/darkplaces
	install -d debian/darkplaces-server/usr/games
	install darkplaces-dedicated debian/darkplaces-server/usr/games/darkplaces-server

override_dh_auto_test:
	:

override_dh_strip:
	dh_strip --dbg-package=darkplaces-dbg

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

# The upstream release
ORIG_REL = 0~20130304
# The svn revision corresponding to that release (look in ChangeLog)
ORIG_REL_REV = 11925
# The svn revision we actually want ("+svnXXXX" if different)
ORIG_REV = 11952
# SUFFIX can be made non-empty for re-repacks
ORIG_SUFFIX =

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

get-orig-source:
	svn export ${ORIG_EXPORT} darkplaces-${ORIG_VER}.orig
	rm -rf darkplaces-${ORIG_VER}.orig/snd_3dras*
	tar -zcvf darkplaces_${ORIG_VER}.orig.tar.gz darkplaces-${ORIG_VER}.orig
	rm -rf darkplaces-${ORIG_VER}.orig
