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

include /usr/share/dh-dlang/dlang-flags.mk

INSTALL_DIR = $(CURDIR)/debian/dub/
DEB_VERSION := $(shell dpkg-parsechangelog | awk '/^Version: / { print $$2 }')

export DEB_BUILD_MAINT_OPTIONS=optimize=-lto
export GITVER=$(DEB_VERSION)

# to make build reproducible
export DIFFABLE=1

ifeq ($(DC),ldc2)
	ALLINST_FLAG=-allinst
	DC_WRAP=ldmd2
else
	ALLINST_FLAG=-fall-instantiations
	EXTRA_DFLAGS=-wi -d
	DC_WRAP=gdmd
# Translate/Filter out options that are accepted by GDC but not accepted by GDMD
	DFLAGS := $(patsubst -O%,-O,$(DFLAGS))
	DFLAGS := $(patsubst -W%,,$(DFLAGS))
endif
DFLAGS += $(EXTRA_DFLAGS) $(ALLINST_FLAG)
export DFLAGS

%:
	dh $@

override_dh_auto_build:
	DMD=$(DC_WRAP) DFLAGS="$(ALLINST_FLAG)" $(DC_WRAP) \
		-run $(CURDIR)/build.d \
		$(DFLAGS)

	# create manual pages
	cd $(CURDIR)/bin/ && \
		HOME=$(CURDIR)/debian ./dub run -v --single --combined --compiler=$(DC_WRAP) $(CURDIR)/scripts/man/gen_man.d

override_dh_install:
	dh_install

	# install shell completions
	mkdir -p $(INSTALL_DIR)/usr/share/bash-completion/completions/
	cp $(CURDIR)/scripts/bash-completion/dub.bash $(INSTALL_DIR)/usr/share/bash-completion/completions/dub
	mkdir -p $(INSTALL_DIR)/usr/share/fish/completions/
	cp $(CURDIR)/scripts/fish-completion/dub.fish $(INSTALL_DIR)/usr/share/fish/completions/

override_dh_auto_clean:
	dh_auto_clean
	rm -f $(CURDIR)/scripts/man/*.1
