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

export DEB_BUILD_OPTIONS = optimize=-lto

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

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

export GITVER=$(DEB_VERSION)

ifeq ($(DC),ldc2)
	ALLINST_FLAG=-allinst
	DC_WRAP=ldmd2
else
	ALLINST_FLAG=-fall-instantiations
	EXTRA_DFLAGS=-Wno-error=deprecated
	DC_WRAP=gdmd
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 \
		-g -O -wi -release

	# create manual pages
	cd $(CURDIR)/bin/ && \
		./dub $(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
