#!/usr/bin/make -f
#export DH_VERBOSE = 1

# we use the upstream packaged tarball, we don't build the package.
# We need to download the packaged tarball for the the current nodejs
# version in Kali. We must change the debian/watch

# Version of nodejs in Kali
NODE_MAJOR_VERSION := $(shell nodejs -v | sed -e 's/v*//' | cut -d. -f1)
# Version of nodejs used by upstream to build juice-shop
NODE_VERSION_JUICE := $(shell grep '"node":' package.json | sed -e 's/"//g' | sed -e 's/ //g' | cut -d':' -f2)

CURRENT_VERSION := $(shell dpkg -s nodejs | grep '^Version:' | cut -d' ' -f2)
MAX_MAJOR_VERSION := $(shell echo $$(( $(NODE_MAJOR_VERSION) + 1 )) )

# Check if the versions are identical
ifneq ($(NODE_MAJOR_VERSION), $(NODE_VERSION_JUICE))
    $(error Please download the upstream tarball for node version $(NODE_MAJOR_VERSION))
endif

%:
	dh $@

override_dh_installsystemd:
	dh_installsystemd --no-enable --no-start

override_dh_gencontrol:
ifneq ($(shell dpkg -s nodejs | grep '^Version:' | grep really),)
	dh_gencontrol -- -Vnode:Depends="nodejs (= $(CURRENT_VERSION))"

else
	dh_gencontrol -- -Vnode:Depends="nodejs (>= $(NODE_MAJOR_VERSION)), nodejs (<< $(MAX_MAJOR_VERSION))"
endif
