#!/usr/bin/make -f


CARGO ?= cargo
GEM ?= gem

OUTDIR = $(CURDIR)/debian/http/
VERSION = $(shell awk '/^version/ {print $$3; exit}' Cargo.toml | sed 's/"//g')


override_dh_auto_build:
	mkdir -p target/completions target/locales
	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
	RUSTFLAGS=-g $(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
	@
	cat http.1 | gzip -9 > $(OUTDIR)usr/share/man/man1/http.1.gz
	cd $(OUTDIR)usr/share/man/man1/ && ln http.1.gz httplz.1.gz
	@
	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_auto_clean:
	rm -rf target/ Cargo.lock http.1

# for tag in $(git tag -l | sort -rV); do git tag -ln1000 "$tag"; echo; echo; done | awk '!/[[:space:]]+\([[:alnum:]]{40}\)$/' | sed -re 's;\([[:alnum:]]{40}\);+;g' -e 's;\(#;(https://github.com/thecoshman/http/issues/;g' -e 's;\(@;(https://github.com/;g' | head -c -2
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 $@
