#!/usr/bin/make -f

include /usr/share/dpkg/default.mk

# reproducible documentation, see https://wiki.debian.org/ReproducibleBuilds/TimestampsProposal
export SOURCE_DATE_EPOCH = $(shell date -d "$$(dpkg-parsechangelog --count 1 -SDate)" +%s)

platform_tr = $(shell { grep ^$(1) debian/platforms.table || echo '	unsupported:$(1)'; } | cut -f2)

%:
	dh $@

override_dh_auto_build:
	$(MAKE) -f makefile LDFLAGS="-ldl -lm" \
	  myos=$(call platform_tr,$(DEB_HOST_ARCH_OS)) mycpu=$(call platform_tr,$(DEB_HOST_ARCH_CPU))
	./bin/nim c koch
	# Build until convergence; we must do this here, not dh_auto_test, to pick up
	# any changes in debian/patches. see also https://github.com/nim-lang/csources/issues/12
	PATH=./bin/:$$PATH ./koch boot -d:release \
	  $(addprefix --passC:,$(CPPFLAGS)) $(addprefix --passC:,$(CFLAGS)) $(addprefix --passL:,$(LDFLAGS))
	# Build docs
	PATH=./bin/:$$PATH ./koch web
	# Generate manpage
	help2man --name="Nim Language Compiler" -s1 --version-string="$(DEB_VERSION)" \
		-i debian/nim.h2m -o debian/nim.1 ./bin/nim

override_dh_auto_install:
	PATH=./bin/:$$PATH ./koch install debian/tmp
	cp -a web/upload debian/html
	find debian/html -name '*.idx' \
		-delete
	ln -sf overview.html debian/html/index.html

override_dh_auto_test:
	if [ -f compiler/nim2 ]; then cmp ./bin/nim compiler/nim2; else cmp ./bin/nim compiler/nim1; fi

override_dh_auto_clean:
	rm -f ./koch tools/nimweb tools/niminst/niminst compiler/nim compiler/nim0 compiler/nim1
	rm -rf bin/ web/upload/
	rm -rf ./nimcache tools/nimcache tools/niminst/nimcache compiler/nimcache
	find . -name '*.o' \
		-delete
	find doc \( -name '*.idx' -o -name '*.html' \) \
		-delete

override_dh_installdocs:
	dh_installdocs -Xcopying.txt

override_dh_clean:
	dh_clean
	rm -f debian/nim.1
	rm -rf debian/html
