#!/usr/bin/make -f
# -*- makefile -*-

include /usr/share/dpkg/pkg-info.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export PREFIX = /usr
export LIBDIR = /usr/lib/$(DEB_HOST_MULTIARCH)

sonameVersion = $(shell echo $(DEB_VERSION_UPSTREAM) | cut -d '+' -f 1 | cut -d '.' -f -3)
sonameAlias = $(shell echo $(sonameVersion) | cut -d '.' -f -2)

export SONAME = libllhttp.so.$(sonameVersion)
export SONAMEALIAS = libllhttp.so.$(sonameAlias)

%:
	dh $@

execute_before_dh_auto_configure:
	grep -Fq 'libllhttp-source (>= $(DEB_VERSION_UPSTREAM)~)' debian/control || { echo >&2 "***\nPlease update Build-Depends on libllhttp-source\n***"; exit 1; }

override_dh_auto_clean-indep:
	- dh_auto_clean --buildsystem=nodejs

override_dh_auto_configure-indep:
	dh_auto_configure --buildsystem=nodejs

override_dh_auto_build-indep: release
	WASM_OPT=/usr/bin/wasm-opt WASM_CC=/usr/bin/clang WASM_CFLAGS="--sysroot=/usr -target wasm32-unknown-wasi" node debian/wasm.js

override_dh_auto_install-indep:
	dh_auto_install --buildsystem=nodejs

override_dh_auto_test-indep:
	dh_auto_test --buildsystem=nodejs


override_dh_auto_clean-arch:
	rm -rf release
	- dh_auto_clean --buildsystem=cmake --builddir=build-arch

override_dh_auto_configure-arch: release
	dh_auto_configure --buildsystem=cmake --builddir=build-arch --sourcedir=release -- -DLLHTTP_BUILD_STATIC_LIBS=ON

override_dh_auto_build-arch:
	dh_auto_build --buildsystem=cmake --builddir=build-arch --sourcedir=release

override_dh_auto_install-arch:
	dh_auto_install --buildsystem=cmake --builddir=build-arch --sourcedir=release

release:
	if [ -e /usr/bin/nodejs ]; then \
	 dh_auto_configure --buildsystem=nodejs ; \
	 dh_auto_build --buildsystem=nodejs; \
	 RELEASE=$(sonameVersion) make release; \
	 rm release/LICENSE release/README.md; \
	else \
	 cp -rf /usr/src/llhttp release; \
	fi
