#!/usr/bin/make -ef

DEB_SRCDIR = id
DEB_CMAKE_INSTALL_PREFIX = /usr
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_VERSION := $(DEB_VERSION)
NODE_VER ?= $(shell cat ./.nvmrc)
# DEB_HYPHEN_VERSION := $(subst .,-,$(DEB_VERSION))

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

%:
	dh $@ --with systemd

override_dh_auto_configure:
	dh_auto_configure

override_dh_auto_build:
	npm run build
	dh_auto_build -- -j2

override_dh_auto_test:
	npm run test

override_dh_installdeb:
		dh_installdeb
		cat debian/yandex-id-frontend/DEBIAN/conffiles |grep -v '.sh' > debian/yandex-id-frontend/DEBIAN/conffiles_filtered
		mv debian/yandex-id-frontend/DEBIAN/conffiles_filtered debian/yandex-id-frontend/DEBIAN/conffiles

override_dh_installinit:
	dh_installinit --name=yandex-id-frontend
	dh_installinit --name=yandex-id-tvmtool

override_dh_systemd_enable:
	dh_systemd_enable --name=yandex-id-frontend
	dh_systemd_enable --name=yandex-id-tvmtool

override_dh_installmodules:
	mkdir -p debian/tmp
	cp package.json package-lock.json .npmrc debian/tmp
	npm ci --production --prefix debian/tmp

	NAVE_DIR=debian/tmp/nvm npx nave install ${NODE_VER}
	mv debian/tmp/nvm/installed/${NODE_VER} debian/tmp/node

	dh_install debian/tmp/node_modules debian/tmp/node /usr/lib/yandex/id-frontend/

override_dh_auto_clean: clean_build_dirs
	dh_auto_clean

clean_build_dirs:
	-rm -Rf .next build .env
