#!/usr/bin/make -f


CARGO ?= /home/nabijaczleweli/.cargo/bin/cargo

OUTDIR = $(CURDIR)/debian/http/


override_dh_auto_build:
	mkdir -p target/completions target/locales
ifeq "$(filter nodoc,$(DEB_BUILD_PROFILES))" ""
	localedef -i en_GB -c -f UTF-8 -A /usr/share/locale/locale.alias target/locales/en_GB.UTF-8
	LOCPATH="target/locales/" LC_CTYPE="en_GB.UTF-8" ronn -r --organization="http developers" http.md
endif
	RUSTFLAGS=-g HTTP_GENVER="$(shell IFS="$$IFS()" read -r _ v _ < debian/changelog; echo "$$v")" $(CARGO) build --release
	cd target/completions && ../../target/release/http-build-completions

override_dh_auto_install:
	mkdir -p $(OUTDIR)usr/bin $(OUTDIR)usr/share/man/man1 $(OUTDIR)usr/share/doc/http $(OUTDIR)usr/share/zsh/vendor-functions $(OUTDIR)usr/share/fish/vendor_completions.d
	cp target/release/http $(OUTDIR)usr/bin/
	cd $(OUTDIR)usr/bin/ && ln http httplz
	@
ifeq "$(filter nodoc,$(DEB_BUILD_PROFILES))" ""
	gzip -9 < http.1 > $(OUTDIR)usr/share/man/man1/http.1.gz
	cd $(OUTDIR)usr/share/man/man1/ && ln http.1.gz httplz.1.gz
endif
	@
	dh_bash-completion
	# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=921236#17
	cp $(wildcard target/completions/_*) $(OUTDIR)usr/share/zsh/vendor-functions/
	# https://fishshell.com/docs/current/#where-to-put-completions
	cp $(wildcard target/completions/*.fish) $(OUTDIR)usr/share/fish/vendor_completions.d/

override_dh_dwz:
	# https://101010.pl/@nabijaczleweli/110944972179495353

override_dh_clean:
	dh_clean -X.orig  # Would delete vendore Cargo.toml.origs

override_dh_auto_clean:
	rm -rf target/ Cargo.lock http.1

override_dh_installchangelogs:
	dh_installchangelogs debian/changelog.upstream

override_dh_installdocs:
	dh_installdocs README.md

# Can't use dh_installman because the manpages get compressed later,
# and I want a hardlink of the compressed versions so they get hardlinked on the target system

%:
	dh $@
