#!/usr/bin/make -f
export SHELL := /bin/bash
.SHELLFLAGS := -c
export DH_VERBOSE = 1
export DH_OPTIONS = -v

DEB_BUILD_MAINT_OPTIONS := hardening=+all
export DEB_BUILD_MAINT_OPTIONS

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

node_major_ver = $(shell awk '/^\#define NODE_MAJOR_VERSION/ {print $$3}' src/node_version.h)
node_minor_ver = $(shell awk '/^\#define NODE_MINOR_VERSION/ {print $$3}' src/node_version.h)
node_patch_ver = $(shell awk '/^\#define NODE_PATCH_VERSION/ {print $$3}' src/node_version.h)
nodejs_version = $(node_major_ver).$(node_minor_ver).$(node_patch_ver)

export SHELL = /bin/bash
export HOME=$(CURDIR)/build
export BUILD_DIR=$(shell pwd)




vafilt					= $(subst $(2)=,,$(filter $(2)=%,$(1)))
ARCHS					:= amd64 arm64 i386
DPKG_VARS				:= $(shell dpkg-architecture)
DEB_BUILD_ARCH			?= $(call vafilt,$(DPKG_VARS),DEB_BUILD_ARCH)
DEB_BUILD_GNU_TYPE		?= $(call vafilt,$(DPKG_VARS),DEB_BUILD_GNU_TYPE)
DEB_BUILD_MULTIARCH		?= $(call vafilt,$(DPKG_VARS),DEB_BUILD_MULTIARCH)
DEB_HOST_ARCH			?= $(call vafilt,$(DPKG_VARS),DEB_HOST_ARCH)
DEB_HOST_ARCH_ENDIAN	?= $(call vafilt,$(DPKG_VARS),DEB_HOST_ARCH_ENDIAN)
DEB_HOST_ARCH_OS		?= $(call vafilt,$(DPKG_VARS),DEB_HOST_ARCH_OS)
DEB_HOST_GNU_TYPE		?= $(call vafilt,$(DPKG_VARS),DEB_HOST_GNU_TYPE)
DEB_HOST_MULTIARCH		?= $(call vafilt,$(DPKG_VARS),DEB_HOST_MULTIARCH)

_sourcedir 		:= debian/source
_prefix 		:= /opt/alt/nodejs16
_bindir 		:= $(_prefix)/bin
_libdir 		:= $(_prefix)/lib
_localstatedir 	:= /var
_includedir 	:= $(_prefix)/include
_datadir		:= $(_prefix)/share
_mandir 		:= $(_datadir)/man
_docdir 		:= ${_datadir}/doc
_metainfodir	:= /usr/share/metainfo
uname_m 		:= $(shell uname -m)
buildroot 		:= $(CURDIR)/debian/tmp

DPKG_CFLAGS := $(shell dpkg-buildflags --get CFLAGS)
DPKG_CFLAGS += -fno-semantic-interposition
DPKG_LDFLAGS := $(shell dpkg-buildflags --get LDFLAGS)

export CFLAGS := $(DPKG_CFLAGS) -g -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
export CXXFLAGS := $(DPKG_CFLAGS) -g -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I$(_includedir)
export LDFLAGS := $(DPKG_LDFLAGS) -L$(_libdir) -Wl,-rpath=$(_libdir)

export CFLAGS += -DZLIB_CONST
export CXXFLAGS += -DZLIB_CONST
export LDFLAGS += -lrt

NJOBS := -j$(shell getconf _NPROCESSORS_ONLN)

LANG=C
export LANG
DEB_CONFIGURE_NORMAL_ARGS =
DEB_CONFIGURE_EXTRA_FLAGS = \
--verbose \
--shared-zlib \
--with-intl=system-icu \
--prefix=$(_prefix)

DEB_CONFIGURE_EXTRA_FLAGS += \
--shared-openssl \
--openssl-system-ca-path=$(_sysconfdir)/pki/tls


ICU_PREFIX := /opt/alt/libicu71/usr
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:$(ICU_PREFIX)/lib/$(DEB_HOST_MULTIARCH)/pkgconfig
export LDFLAGS += -Wl,-rpath=$(ICU_PREFIX)/lib/$(DEB_HOST_MULTIARCH)

CFLAGS += -fPIC
CPPFLAGS += -fPIC
CXXFLAGS += $(CPPFLAGS)
DH_FLAGS += --max-parallel=1

DEB_DESTDIR = $(CURDIR)/debian/tmp
DEB_MAKE_CLEAN_TARGET = clean
DEB_MAKE_BUILD_TARGET = build

export CPPFLAGS
export CXXFLAGS
export CFLAGS
export LDFLAGS

#  relax regression tests when targeted experimental suite
export HOME = $(CURDIR)/tmp
export FLAKY_TESTS = dontcare
export TEST_CI_ARGS = --arch=$(destCpu) --timeout=3000
export CI_JS_SUITES = message parallel sequential
export NODE_TEST_DIR = $(CURDIR)/tmp
export NODE_TEST_NO_INTERNET = 1
DEBIAN_DOC_DEPS = $(CURDIR)/debian/doc-generator/node_modules
# needed for tests to avoid "ee key too small" errors
export OPENSSL_CONF = ./deps/openssl/openssl/apps/openssl.cnf
export


%:
	dh $@ $(DH_FLAGS)

override_dh_auto_configure:
	# The CVE-2023-39333 patch includes binary files that dpkg-source can't handle,
	# so we apply it manually using git apply instead of quilt.
	git apply -p1 debian/patches/CVE-2023-39333.patch
	export
	./configure $(DEB_CONFIGURE_EXTRA_FLAGS)
	grep -r "ffat-lto-objects" .
	find ./ -type f -exec sed -i -e "1s@#!/usr/bin/env node@#!$(_bindir)/node@" {} \;

override_dh_auto_build:
	ulimit -n 8192
	ulimit -Sa
	export
	rm -rf node-v$(node_major_ver).$(node_minor_ver).$(node_patch_ver)/deps/openssl/
	make BUILDTYPE=Release $(NJOBS)

override_dh_auto_install:
	sed -i "1s@#!/usr/bin/env python@#!/usr/bin/python3@" ./tools/install.py
	./tools/install.py install $(buildroot) $(_prefix)

	# own the sitelib directory
	mkdir -p $(buildroot)$(_prefix)/lib/node_modules

	#install documentation
	mkdir -p $(buildroot)$(_docdir)/nodejs-docs-$(nodejs_version)/html
	cp -pr doc/* $(buildroot)$(_docdir)/nodejs-docs-$(nodejs_version)/html
	rm -f $(buildroot)$(_docdir)/nodejs-docs-$(nodejs_version)/html/nodejs.1

	#node-gyp needs common.gypi too
	mkdir -p $(buildroot)$(_datadir)/node
	cp -p common.gypi $(buildroot)$(_datadir)/node

	# install file contained package version in name
	echo $(nodejs_version) > $(buildroot)$(_datadir)/node/node-version-$(nodejs_version)

	# Install the GDB init tool into the documentation directory
	mv $(buildroot)$(_datadir)/doc/node/gdbinit $(buildroot)$(_docdir)/nodejs-docs-$(nodejs_version)/gdbinit

	# Since the old version of NPM was unbundled, there are a lot of symlinks in
	# it's node_modules directory. We need to keep these as symlinks to ensure we
	# can backtrack on this if we decide to.

	# Rename the npm node_modules directory to node_modules.bundled
	mv $(buildroot)/$(_prefix)/lib/node_modules/npm/node_modules \
	   $(buildroot)/$(_prefix)/lib/node_modules/npm/node_modules.bundled

	# Recreate all the symlinks
	mkdir -p $(buildroot)/$(_prefix)/lib/node_modules/npm/node_modules
	FILES=$(buildroot)/$(_prefix)/lib/node_modules/npm/node_modules.bundled/*; \
	for f in $$FILES; do \
		module=`basename $$f`; \
		ln -s ../node_modules.bundled/$${module} $(buildroot)$(_prefix)/lib/node_modules/npm/node_modules/$${module}; \
	done

	# install NPM docs to mandir
	mkdir -p $(buildroot)$(_mandir) \
			 $(buildroot)$(_docdir)/nodejs-docs-$(nodejs_version)/npm

	cp -pr deps/npm/man/* $(buildroot)$(_mandir)/
	rm -rf $(buildroot)$(_prefix)/lib/node_modules/npm/man

	# Install Markdown and HTML documentation to $(_pkgdocdir)
	cp -pr deps/npm/docs $(buildroot)$(_docdir)/nodejs-docs-$(nodejs_version)/npm/
	rm -rf $(buildroot)$(_prefix)/lib/node_modules/npm/docs


	# Node tries to install some python files into a documentation directory
	# (and not the proper one). Remove them for now until we figure out what to
	# do with them.
	rm -f $(buildroot)/$(_defaultdocdir)/node/lldb_commands.py \
		  $(buildroot)/$(_defaultdocdir)/node/lldbinit

# Packaging
	dh_installdirs -p alt-nodejs$(node_major_ver)-nodejs \
	  .$(_datadir)/node \
	  .$(_datadir)/systemtap \
	  .$(_datadir)/systemtap/tapset \
	  .$(_prefix)/lib/node_modules

	dh_movefiles -p alt-nodejs$(node_major_ver)-nodejs \
	  .$(_bindir)/corepack \
	  .$(_bindir)/node \
	  .$(_datadir)/node/node-version-$(nodejs_version) \
	  .$(_datadir)/systemtap/tapset/node.stp \
	  .$(_prefix)/lib/node_modules/corepack

	dh_installdocs -p alt-nodejs$(node_major_ver)-nodejs \
	  AUTHORS CHANGELOG.md CONTRIBUTING.md GOVERNANCE.md README.md LICENSE

	dh_movefiles -p alt-nodejs$(node_major_ver)-nodejs-devel \
	  .$(_datadir)/node/common.gypi \
	  .$(_docdir)/nodejs-docs-$(nodejs_version)/gdbinit \
	  .$(_includedir)/node

	dh_movefiles -p alt-nodejs$(node_major_ver)-npm \
	  .$(_bindir)/npm \
	  .$(_bindir)/npx \
	  .$(_prefix)/lib/node_modules/npm

	dh_installdocs -p alt-nodejs$(node_major_ver)-npm \
	  ./debian/tmp$(_mandir)/man1/npm* \
	  ./debian/tmp$(_mandir)/man1/node* \
	  ./debian/tmp$(_mandir)/man1/npx* \
	  ./debian/tmp$(_mandir)/man5/* \
	  ./debian/tmp$(_mandir)/man7/* \
	  ./debian/tmp$(_docdir)/node*

	dh_movefiles -p alt-nodejs$(node_major_ver)-docs \
	  .$(_docdir)/nodejs-docs-$(nodejs_version)/html* \
	  .$(_docdir)/nodejs-docs-$(nodejs_version)/npm*

override_dh_fixperms:
	dh_fixperms

	chmod 0755 debian/alt-nodejs16-nodejs$(_bindir)/node
	chmod 0755 debian/alt-nodejs16-npm$(_bindir)/npm
	chmod 0755 debian/alt-nodejs16-npm$(_bindir)/npx
	chmod 0755 debian/alt-nodejs16-npm$(_libdir)/node_modules/npm/bin/npm-cli.js
	chmod 0755 debian/alt-nodejs16-npm$(_libdir)/node_modules/npm/bin/npx-cli.js

override_dh_missing:
	dh_missing --fail-missing

override_dh_auto_test:
override_dh_auto_clean:
