#!/usr/bin/make -f

export DH_VERBOSE = 1
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export LC_ALL = C.UTF-8

%:
	dh $@

override_dh_auto_configure:
	# Restore pre-built deps from postgrest-deps package.
	if [ -d /usr/lib/postgrest-deps/dist-newstyle ]; then \
		cp -a /usr/lib/postgrest-deps/dist-newstyle .; \
	fi
	# Pre-seed cabal's config so it never tries to hit the network.
	mkdir -p local-hackage
	tar -cf local-hackage/01-index.tar --files-from /dev/null
	mkdir -p $$HOME/.config/cabal $$HOME/.cabal
	printf 'repository hackage.haskell.org\n  url: file://%s/local-hackage/\n  secure: False\nremote-repo-cache: %s/.cabal/packages\n' "$$(pwd)" "$$HOME" > $$HOME/.config/cabal/config
	printf 'repository hackage.haskell.org\n  url: file://%s/local-hackage/\n  secure: False\nremote-repo-cache: %s/.cabal/packages\n' "$$(pwd)" "$$HOME" > $$HOME/.cabal/config
	printf 'packages: postgrest.cabal\n          vendor/*/*.cabal\ntests: false\nactive-repositories: hackage.haskell.org\n\nrepository hackage.haskell.org\n  url: file://%s/local-hackage/\n  secure: False\n' "$$(pwd)" > cabal.project

override_dh_auto_build:
	cabal v2-build --offline --disable-tests --disable-benchmarks exe:postgrest -j"$(shell nproc)"

override_dh_auto_test:
	@echo "Skipping upstream test suite; it needs a configured PostgreSQL instance."

override_dh_auto_install:
	install -d debian/postgrest/usr/bin
	cp dist-newstyle/build/x86_64-linux/ghc-*/postgrest-*/x/postgrest/build/postgrest/postgrest debian/postgrest/usr/bin/postgrest

override_dh_auto_clean:
	rm -rf dist-newstyle local-hackage/01-index.tar