#!/usr/bin/make -f

# make pgloader depend on the libssl package cl-plus-ssl depends on
LIBSSL := $(shell dpkg-query --showformat='$${Depends}' --show cl-plus-ssl | grep -o 'libssl[^ ]*')

BITS = $(shell dpkg-architecture -qDEB_BUILD_ARCH_BITS)
ifeq ($(BITS),32)
SIZE=1024
else
SIZE=4096
endif

# buildd provides a build environment where $HOME is not writable, but the
# CL compilers here will need to fill-in a per-user cache
export HOME = $(CURDIR)/debian/home

override_dh_auto_clean:
	dh_auto_clean
	rm -rf debian/home

override_dh_auto_build:
	mkdir -p build/bin
	mkdir -p $(HOME)
	buildapp --require sb-posix                         \
                 --require sb-bsd-sockets                   \
                 --load /usr/share/common-lisp/source/cl-asdf/build/asdf.lisp \
                 --asdf-path .                              \
                 --asdf-tree /usr/share/common-lisp/systems \
                 --load-system asdf-finalizers         	    \
                 --load-system asdf-system-connections 	    \
                 --load-system pgloader               	    \
                 --load src/hooks.lisp                      \
                 --entry pgloader:main                      \
                 --dynamic-space-size $(SIZE)               \
                 --compress-core                    	    \
                 --output build/bin/pgloader
	$(MAKE) -C docs html

override_dh_auto_test:
	# no nothing

override_dh_strip:
	# do nothing

override_dh_gencontrol:
	dh_gencontrol -- -V"ssl:Depends=$(LIBSSL)"

%:
	dh $@
