
export CXXFLAGS
export LDFLAGS

# shortcut setting some defaults to development style
ifneq ($(DEBUG),)
	 CMAKEOPTS += -DCMAKE_BUILD_TYPE=Debug --debug-trycompile --debug-output
	 MAKEFLAGS += VERBOSE=1
else
	# use release type flags unless user has custom settings already
ifeq ($(CXXFLAGS),)
	 CMAKEOPTS += -DCMAKE_BUILD_TYPE=Release
endif
endif

doc:
	make -C doc

PKGNAME=apt-cacher-ng
VERSION=$(shell cat VERSION)
TAGVERSION=$(subst rc,_rc,$(subst pre,_pre,$(VERSION)))
DISTNAME=$(PKGNAME)-$(VERSION)
DEBSRCNAME=$(PKGNAME)_$(shell echo $(VERSION) | sed -e "s,pre,~pre,;s,rc,~rc,;").orig.tar.xz

tarball: doc notdebianbranch nosametarball
	# diff-index is buggy and reports false positives... trying to work around
	git update-index --refresh || git commit -a
	git diff-index --quiet HEAD || git commit -a
	git archive --prefix $(DISTNAME)/ HEAD | xz -9 > ../$(DISTNAME).tar.xz
	test -e /etc/debian_version && ln -f ../$(DISTNAME).tar.xz ../$(DEBSRCNAME) || true
	test -e ../tarballs && ln -f ../$(DISTNAME).tar.xz ../tarballs/$(DEBSRCNAME) || true
	test -e ../build-area && ln -f ../$(DISTNAME).tar.xz ../build-area/$(DEBSRCNAME) || true

tarball-remove:
	rm -f ../$(DISTNAME).tar.xz ../tarballs/$(DEBSRCNAME) ../$(DEBSRCNAME) ../build-area/$(DEBSRCNAME)

release: checktest noremainingwork tarball
	git tag upstream/$(TAGVERSION)

unrelease: tarball-remove
	-git tag -d upstream/$(TAGVERSION)

pristine-commit:
	pristine-tar commit ../$(DISTNAME).tar.xz $(TAGVERSION)
	pristine-tar commit ../tarballs/$(DEBSRCNAME) $(TAGVERSION)

noremainingwork:
	test ! -e TODO.next # the quick reminder for the next release should be empty

notdebianbranch:
	test ! -f debian/rules # make sure it is not run from the wrong branch

nosametarball:
	test ! -f ../$(DISTNAME).tar.xz # make sure not to overwrite existing tarball
	test ! -f ../tarballs/$(DEBSRCNAME)

gendbs:
	$(MAKE) -C dbgen CONFDIR=../conf

doxy:
	doxygen Doxyfile
	see doc/dev/html/index.html

checktest:
	test -e builddir/testok || (echo Should run the test suite before release... ; exit 1)

test:
	bash -x tests/apptests.sh
	touch builddir/testok

# execute them always and consider done afterwards
.PHONY: gendbs clean distclean config conf/gentoo_mirrors.gz test

# the dependencies in THIS Makefile shall be processed as sequence
.NOTPARALLEL:
