#!/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"

export FLAGS = -Dwithout_playground


%:
	dh $@

override_dh_auto_build:
	dh_auto_build -- $(make_options)
	make $(make_options) docs

override_dh_auto_install:
	dh_auto_install -- PREFIX=/usr

override_dh_install:
	dh_install
	
	#
	# clean lib dir, but without deleting (needed by extensions loading):
	#   - ext/libcrystal.a
	#   - llvm/ext/{find-llvm-config, llvm_ext.o)
	#
	find "debian/crystal/usr/lib/crystal/" \
		-iname "sigfault.o" -or \
		-iname "*.c" -or \
		-iname "*.cc" \
		| xargs rm
	
	# fix privacy-breach-generic links on html docs
	sed -i  '/<p><a href=/{:a;N;/alt="born-and-raised" \/><\/a><\/p>/!ba};//d' \
		debian/crystal-doc/usr/share/doc/crystal/api-docs/index.html

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


override_dh_clean:
	dh_clean
	rm -f man/*.gz
