#!/usr/bin/make -f

include /usr/share/coq/coqvars.mk

export DEB_BUILD_OPTIONS += noautodbgsym

export COQTOP := $(COQ_STDLIB_DIR)
export ROCQLIB := $(COQ_STDLIB_DIR)
export COQBIN := /usr/bin/

PKGS = algebra boot field finite-group group-representation order ssreflect solvable

%:
	dh $@ --with coq

override_dh_auto_build:
	$(MAKE) build
	$(MAKE) doc

override_dh_auto_install:
	$(MAKE) install DSTROOT=$(CURDIR)/debian/tmp
	for pkg in $(PKGS); do \
		pkgdir=$$(printf '%s' "$$pkg" | tr '-' '_'); \
		find debian/tmp$(COQ_USERCONTRIB_DIR)/mathcomp/$$pkgdir -regextype posix-awk \
		-regex '.*\.(v|vo|vos|glob)$$' \
		>> debian/libcoq-mathcomp-$$pkg.install ; \
	done
	find debian/tmp$(COQ_USERCONTRIB_DIR)/mathcomp/all -regextype posix-awk \
	-regex '.*\.(v|vo|vos|glob)$$' \
	>> debian/libcoq-mathcomp.install

override_dh_auto_clean:
	$(MAKE) clean
	rm -f mathcomp/Makefile.coq mathcomp/Makefile.coq.conf

override_dh_installdocs:
	dh_installdocs
	for html in `find debian -name "*.html"`; do \
		sed -i \
		-e 's#https://cdnjs.cloudflare.com/ajax/libs/jquery/[^/]*/jquery\.min\.js#file:///usr/share/javascript/jquery/jquery.min.js#' \
		-e 's#https://cdn.jsdelivr.net/npm/katex/dist/katex\.min\.css#file:///usr/share/javascript/katex/katex.min.css#' \
		-e 's#https://cdn.jsdelivr.net/npm/katex/dist/katex\.min\.js#file:///usr/share/javascript/katex/katex.min.js#' \
		-e 's#https://cdn.jsdelivr.net/npm/markdown-it/dist/markdown-it\.min\.js#file:///usr/share/nodejs/markdown-it/dist/markdown-it.min.js#' \
		-e 's#https://cdn.jsdelivr.net/npm/markdown-it-texmath/texmath\.min\.js#file:///usr/share/nodejs/markdown-it-texmath/texmath.js#' \
		$$html ; \
	done
