#!/usr/bin/make -f

build clean install binary-arch binary-indep binary:
	+dh $(opt_no_act) $@

override_dh_auto_clean:
	dh_auto_clean --builddirectory debian/xzdec-build
	dh_auto_clean --builddirectory debian/normal-build
	sh debian/clean.sh

override_dh_auto_configure:
	autoreconf -fi
	dh_auto_configure --builddirectory debian/normal-build -- \
		$(opt_optimize) $(opt_quiet)
	dh_auto_configure --builddirectory debian/xzdec-build -- \
		--disable-shared --disable-nls --disable-encoders \
		--enable-small --disable-threads \
		$(opt_optimize_small) $(opt_quiet)

override_dh_auto_build:
	dh_auto_build --builddirectory debian/normal-build
	cd debian/normal-build && doxygen Doxyfile
	$(MAKE) -C debian/xzdec-build/src/liblzma
	$(MAKE) -C debian/xzdec-build/src/xzdec

override_dh_auto_test:
	$(MAKE) -C debian/normal-build check

override_dh_auto_install:
	dh_auto_install --builddirectory debian/normal-build
	$(MAKE) -C debian/xzdec-build/src/xzdec install \
		DESTDIR=$$(pwd)/debian/tmp

override_dh_installchangelogs:
	dh_installchangelogs debian/changelog.upstream

opt_optimize = CFLAGS="-g -O2"
opt_optimize_small = CFLAGS="-g -Os"
opt_no_act =
opt_quiet =

comma=,
space_sep_build_opts := $(subst $(comma), ,$(DEB_BUILD_OPTIONS))
build_opt_parallel := $(filter parallel=%,$(space_sep_build_opts))

# The Hurd is misdetected as Mac OS X in the x86 assembler code,
# resulting in errors like
#
# 	crc32_x86.S:280: Error: character following name is not '#'
#
# Use C code in its place on those machines for now.
ifeq (hurd,$(shell dpkg-architecture -qDEB_HOST_ARCH_OS))
    opt_optimize += --disable-assembler
    opt_optimize_small += --disable-assembler
endif

ifneq (,$(filter noopt,$(space_sep_build_opts)))
    opt_optimize = --disable-assembler CFLAGS="-g -O0"
    opt_optimize_small = --disable-assembler CFLAGS="-g -O0"
endif

ifndef in_submake
ifneq (,$(build_opt_parallel))
    MAKEFLAGS += -j $(subst parallel=,,$(build_opt_parallel))
endif
endif

ifneq (,$(findstring n,$(MAKEFLAGS)))
    opt_no_act = --no-act
endif

ifneq (,$(filter quiet,$(space_sep_build_opts)))
    opt_quiet = --quiet
    MAKEFLAGS += --quiet
endif

in_submake = yes
export in_submake

# Build a tarball with the latest upstream version.
# This is made complicated by the need to choose a nice version number.
REPO = git://ctrl.tukaani.org/xz.git
BRANCH = master
get-orig-source:
	mkdir debian-orig-source
	-set -e; cd debian-orig-source; \
		: fetch latest upstream version; \
		git init -q; \
		git fetch -q --tags $(REPO); \
		git fetch -q $(REPO) $(BRANCH); \
		: determine upstream version number; \
		commit_name=$$(git describe FETCH_HEAD); \
		release=$${commit_name%%-*}; \
		date=$$(date --utc --date="$$( \
			git log -1 --pretty=format:%cD "$$commit_name" \
			)" "+%Y%m%d"); \
		if test "$$commit_name" = "$$release"; \
		then upstream_version=$${commit_name#v}; \
		else upstream_version="$${release#v}+$${date}"; \
		fi; \
		: generate tarball; \
		echo "packaging $$commit_name"; \
		git archive --format=tar "$$commit_name" \
			--prefix="xz-utils-$$upstream_version/" | \
		gzip -9 > "../xz-utils_$$upstream_version.orig.tar.gz"
	rm -fr debian-orig-source
