#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk
include /usr/share/javahelper/java-vars.mk
include /usr/share/dpkg/architecture.mk

cp += /usr/share/java/css-validator.jar
cp += /usr/share/java/commons-codec.jar
cp += /usr/share/java/commons-fileupload.jar
cp += /usr/share/java/commons-io.jar
cp += /usr/share/java/commons-logging.jar
cp += /usr/share/java/httpclient.jar
cp += /usr/share/java/httpcore.jar
cp += /usr/share/java/icu4j.jar
cp += /usr/share/java/isorelax.jar
cp += /usr/share/java/javax.json.jar
cp += /usr/share/java/javax.json-api.jar
cp_jetty += /usr/share/java/jetty9-http.jar
cp_jetty += /usr/share/java/jetty9-io.jar
cp_jetty += /usr/share/java/jetty9-security.jar
cp_jetty += /usr/share/java/jetty9-server.jar
cp_jetty += /usr/share/java/jetty9-servlet.jar
cp_jetty += /usr/share/java/jetty9-servlets.jar
cp_jetty += /usr/share/java/jetty9-util-ajax.jar
cp_jetty += /usr/share/java/jetty9-util.jar
cp += /usr/share/java/log4j-1.2.jar
cp += /usr/share/java/salvation.jar
cp += /usr/share/java/saxon.jar
cp += /usr/share/java/saxonb.jar
cp += /usr/share/java/servlet-api.jar
cp += /usr/share/java/slf4j-api.jar
cp += /usr/share/java/slf4j-log4j12.jar
cp += /usr/share/java/xalan2.jar

cp += /usr/share/java/htmlparser.jar

# cp += /usr/share/java/jing.jar # Disable for now. vnu's version deviates too much. Debian not usable.

# If we want not to add the the local build of these jars to vnu.jar
#cp += $(PWD)/jing-trang/build/jing.jar
#cp += $(PWD)/css-validator/css-validator.jar

noop=
space = $(noop) $(noop)
export EXTRA_CLASSPATH=$(subst $(space),:,$(cp)):$(subst $(space),:,$(cp_jetty))

export JAVA_HOME
export VNU_MODULES_CLASSPATH = /usr/share/vnu/galimatias.jar /usr/share/vnu/langdetect.jar /usr/share/vnu/jing.jar
export VNU_BUILDDEPS_CLASSPATH = $(cp)
export VNU_SERVER_BUILDDEPS_CLASSPATH = $(cp_jetty)
export DEB_VERSION_UPSTREAM

ifneq ($(filter i386,$(DEB_HOST_ARCH)),)
	# On i386, we have to set stacksize=512 (will result in -Xss=512k)
	# Otherwise we would have this error:
	#StackOverflowError while evaluating HTML schema.
	#The checker requires a java thread stack size of at least 512k.
	#Consider invoking java with the -Xss option. For example:
	#java -Xss512k -jar ~/vnu.jar FILE.html
	# with openjdk-21 512 was sufficient, but with openjdk-25 we need at least 1750
	BUILD_ARGS += --stacksize=2048
	# Using _JAVA_OPTIONS doesn't work as an alternative: See: https://stackoverflow.com/q/49233449/15401262
	#export _JAVA_OPTIONS="-Xss512k"
endif

BUILD_ARGS += --ant-extra-arg="-Dextra_classpath=$(EXTRA_CLASSPATH)"
BUILD_ARGS += --ant-extra-arg="-Djar-without-deps=true"
BUILD_ARGS += --ant-extra-arg="-Duse-system-cssvalidator=true"
BUILD_ARGS += --ant-extra-arg="-Duse-system-htmlparser=true"
BUILD_ARGS += --offline
BUILD_ARGS += --version=$(DEB_VERSION_UPSTREAM)

%:
	dh $@ --with javahelper

execute_after_dh_clean:
	for wrapper in vnu vnu-server; do $(RM) debian/java-wrappers/$${wrapper}; done

override_dh_auto_clean:
	python3 checker.py $(BUILD_ARGS) clean

override_dh_auto_build:
	mkdir -p dependencies # Required by build.py
	python3 checker.py $(BUILD_ARGS) build
	python3 checker.py $(BUILD_ARGS) war
	for page in vnu vnu-client vnu-server; do \
	  pandoc --standalone --from markdown -f markdown-smart --to man \
	  -M title:"$${page}(1) $${page} $(DEB_VERSION_UPSTREAM)" \
	  -M date:"$$(date -d'@$(SOURCE_DATE_EPOCH)' +%Y-%m-%d)" docs/$${page}.1.md > docs/$${page}.1 ; \
	done

override_dh_auto_test:
	python3 ./checker.py --verbose $(BUILD_ARGS) test
	python3 ./checker.py $(BUILD_ARGS) check

execute_after_dh_auto_install:
	# Unpack content of war file to debian/tmp
	mkdir -p debian/tmp/usr/share/vnu/webapp
	cd debian/tmp/usr/share/vnu/webapp && jar -xvf $(CURDIR)/build/dist-war/vnu.war

execute_before_dh_install:
	sed "s#@jars@#$(VNU_MODULES_CLASSPATH) $(VNU_BUILDDEPS_CLASSPATH)#" debian/java-wrappers/vnu.in > debian/java-wrappers/vnu; \
	sed "s#@jars@#$(VNU_MODULES_CLASSPATH) $(VNU_BUILDDEPS_CLASSPATH) $(VNU_SERVER_BUILDDEPS_CLASSPATH)#" debian/java-wrappers/vnu-server.in > debian/java-wrappers/vnu-server; \
	for wrapper in vnu vnu-server; do \
	  chmod +x debian/java-wrappers/$${wrapper} ;\
	done

override_dh_installchangelogs:
	dh_installchangelogs --no-trim CHANGELOG.md

execute_before_dh_link:
	touch debian/vnu-common.links.in
	cp -a debian/vnu-common.links.in debian/vnu-common.links
	for jar in $(cp); do \
	  echo "$${jar} usr/share/vnu/webapp/WEB-INF/lib/$$(basename "$${jar}")" >> debian/vnu-common.links ;\
	done

execute_after_jh_manifest:
	# Set the Class-Path on some jars in a copy in a temporary location for use by jh_depends
	# This is to set the java:Depends automatically on the vnu, vnu-client and vnu-server packages
	# and leave the libvnu-java package clean.
	mkdir -p debian/workaround/vnu/usr/share/java/
	cp -L debian/libvnu-java/usr/share/java/vnu.jar debian/workaround/vnu/usr/share/java/
	jh_manifest -plibvnu-java -Pdebian/workaround/vnu -c "$(VNU_BUILDDEPS_CLASSPATH)"

	mkdir -p debian/workaround/vnu-server/usr/share/java/
	cp -L debian/libvnu-java/usr/share/java/vnu.jar debian/workaround/vnu-server/usr/share/java/
	jh_manifest -plibvnu-java -Pdebian/workaround/vnu-server -c "$(VNU_BUILDDEPS_CLASSPATH) $(VNU_SERVER_BUILDDEPS_CLASSPATH)"

	# Comment/Uncomment the command below if we want to set or keep blank the Class-Path in vnu.jar
	# Override classpath for libvnu-java, otherwise it is filled with the CLASSPATH environment variable.
	# and we don't want to depend on libjetty*-java in this package
	# Note that if we keep the classpath, then the wrapper-script shouldn't add the jars already in the class-path of the manifest.
	# Otherwise we may encounter https://github.com/qos-ch/slf4j/issues/470
	#jh_manifest -plibvnu-java -c "$(VNU_BUILDDEPS_CLASSPATH)"

override_jh_depends:
	# jh_depends reads the Class-Path of the manifest to fill the java:Depends
	# So we have to fill the manifest correctly which is done in execute_after_jh_manifest

	# Then, we have to set the java:Depends to the proper files, because the
	# lib*-java should not depend on other -java packages.
	# This is done by using
	#    jh_depends -p<package_to_create_substvars_file_for> -P<dir_containing_the_jar_to_use>

	# No need to produce java:Depends on
	#  - vnu-tomcat* -> Managed by tomcat & vnu-common
	#  - vnu-jetty* -> Managed by jetty & vnu-common
	#  - libvnu-java -> Managed by vnu & vnu-server
	#  - libvnu-java-common -> Managed by vnu & vnu-server

	# vnu & vnu-common (for vnu-tomcat & vnu-jetty) don't require jetty.
	# Jetty is only required to run the standalone server of vnu which is
	# made accessible through the "vnu-server" package.
	jh_depends -jheadless -pvnu -Pdebian/workaround/vnu
	jh_depends -jheadless -pvnu-common -Pdebian/workaround/vnu
	jh_depends -jheadless -pvnu-server -Pdebian/workaround/vnu-server

	# vnu-client. Doesn't depend on any java library, except java itself.
	jh_depends -jheadless -pvnu-client
