#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
# output every command that modifies files on the build system.
#export DH_VERBOSE = 1

include /usr/share/dpkg/architecture.mk

N_CPUS=$(shell grep -c "^processor" /proc/cpuinfo 2>/dev/null)

make_options += release=1
make_options += verbose=1
make_options += progress=1
make_options += threads=$(N_CPUS)
make_options += CRYSTAL_CONFIG_PATH="lib:/usr/lib/crystal/lib"
make_options += CRYSTAL_CACHE_DIR="/tmp/crystal"
make_options += interpreter=1

%:
	dh $@

override_dh_auto_build:
	dh_auto_build -- $(make_options)

override_dh_auto_install: DESTDIR=$(CURDIR)/debian/tmp
override_dh_auto_install:
	dh_auto_install -- PREFIX=/usr

	#
	# clean lib dir, but without deleting (needed by extensions loading):
	#   - ext/libcrystal.a
	#   - llvm/ext/{find-llvm-config, llvm_ext.o)
	#
	find "$(DESTDIR)/usr/share/crystal/src" \
		-iname "sigfault.o" -or \
		-iname "*.c" -or \
		-iname "*.cc" \
		| xargs rm
		
	make install_docs DATADIR=$(DESTDIR)/usr/share/doc/crystal
	
	# fix privacy-breach-generic links on html docs
	sed -i  '/<p><a href=/{:a;N;/alt="Crystal - Born and raised at Manas" \/><\/a><\/p>/!ba};//d' \
		$(DESTDIR)/usr/share/doc/crystal/docs/index.html
		
	# remove vcs file in examples
	rm -f $(DESTDIR)/usr/share/doc/crystal/examples/.gitignore

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	make $(make_options) compiler_spec
	make $(make_options) std_spec
	make $(make_options) primitives_spec
endif

override_dh_clean:
	dh_clean
	rm -f man/*.gz
