# -*- makefile-gmake -*-

# @configure_input@
include ../../include/config.Makefile
# we want to ignore the --datarootdir setting, whatever that means, so here we we mention it: @datarootdir@
VPATH = @srcdir@
ALL_PACKAGES = $(shell cat @srcdir@/=distributed-packages)

# this one can be overridden by the user:
PACKAGES = $(ALL_PACKAGES)

# packages under development that should be checked but not installed:
DEVEL = EngineTests

MakeDocumentation ?= $(if @DOCUMENTATION@,true,false)
MakeHTML ?= $(if $(filter html, @DOCUMENTATION@),true,false)
MakeInfo ?= $(if $(filter info, @DOCUMENTATION@),true,false)
MakePDF  ?= $(if $(filter pdf , @DOCUMENTATION@),true,false)
RemakeAllDocumentation ?= false
IgnoreExampleErrors ?= false
CheckDocumentation ?= true
Verbose := $(if $(VERBOSE),true,false)
RerunExamples ?= false
debugLevel ?= 0
errorDepth ?= 0

ARGS := MakeDocumentation => $(MakeDocumentation),		\
	MakeHTML => $(MakeHTML),				\
	MakeInfo => $(MakeInfo),				\
	MakePDF => $(MakePDF),					\
	RemakeAllDocumentation => $(RemakeAllDocumentation),	\
	IgnoreExampleErrors => $(IgnoreExampleErrors),		\
	RerunExamples => $(RerunExamples),			\
	CheckDocumentation => $(CheckDocumentation),		\
	UserMode => false, 					\
	Verbose => $(Verbose),					\
	InstallPrefix => \"@LOCALSTAGINGAREA@/\",		\
	SeparateExec => true,					\
	DebuggingMode => true

# so the user can override arguments to installPackage:
ifneq "$(IARGS)" ""
ARGS := $(ARGS), $(IARGS)
endif

STOP = --stop --silent
ArgumentMode = defaultMode
m2-need-template = "needsPackage(\"$(1)\",LoadDocumentation=>true,DebuggingMode=>true)"
m2-check-template = "check($(1),UserMode=>false,Verbose=>$(Verbose)); exit 0"
$(foreach i,\
	$(sort $(ALL_PACKAGES) $(DEVEL)),\
	$(eval check::check-$i)\
	$(eval check-$i:; \
		@pre_bindir@/M2 -q --no-preload $(STOP)                 \
			-e $(call m2-need-template,$i)                  \
			-e "debug Core; argumentMode = $(ArgumentMode)" \
			-e $(call m2-check-template,$i)))
info-dir: @pre_infodir@/dir
@pre_infodir@/dir:|@pre_infodir@
	@INSTALL_DATA@ @abs_top_srcdir@/files/info-dir-template $@
@pre_infodir@:; $(MKDIR_P) $@

define uninstall
	rm -rf @pre_libm2dir@/$1
	rm -rf @pre_infodir@/$1.info @pre_infodir@/$1.info.errors
	rm -rf @pre_packagesdir@/$1
	rm -rf @pre_docdir@/$1
endef

define bld
install-$1: all-$1
uninstall-packages: uninstall-package-$1
uninstall-package-$1:
	$(call uninstall,$1)
all-$1: @pre_libm2dir@/$1/.installed
unmark-packages: unmark-$1
unmark-$1: ; rm -rf  @pre_libm2dir@/$1/.installed
@pre_libm2dir@/$1/.installed : $1.m2 $$(shell if [ -d @srcdir@/$1 ] ; then find @srcdir@/$1 -type f ; fi) | info-dir
	: installing package $1
	$$(WHY)
	$(MKDIR_P) $1-temporary
	@echo "make: Entering directory \`$(shell pwd)/$1-temporary'"
	cd $1-temporary && @pre_bindir@/M2 $(M2DIRS) \
	    -q $(STOP) \
	    --no-preload \
	    -e errorDepth=$(errorDepth) \
	    -e debugLevel=$(debugLevel) \
	    $(EARGS) \
	    -e "installPackage(\"$1\", $(ARGS)); exit 0"
	@echo "make: Leaving directory \`$(shell pwd)/$1-temporary'"
	ls -l $1-temporary
	rm -rf $1-temporary
	if [ -e @pre_infodir@/$1.info ]; then \
		install-info --info-dir=@pre_infodir@ @pre_infodir@/$1.info; fi
	@ [ "${IgnoreExampleErrors}" = true -o -f $$@ ] || (echo error: file $$@ not made by installPackage >&2; false)
check-info::@pre_infodir@/$1.info.errors
@pre_infodir@/$1.info.errors: @pre_infodir@/$1.info
	emacs --batch --eval '(progn (find-file "$$<") (Info-validate) (switch-to-buffer " *problems in info file*") (write-file "$$@") (kill-emacs))'
	cat $$@
	@ [ ! -s $$@ ] || (echo error: stopping, info validation errors (above) >&2; false)
endef
$(foreach i, $(ALL_PACKAGES), $(eval $(call bld,$i)))
ifeq "$(ReinstallPackages)" "true"
$(foreach i, $(PACKAGES), $(eval all: unmark-$i))
endif
$(foreach i, $(PACKAGES), $(eval all: all-$i) $(eval install: install-$i))

# make packages Style, FirstPackage, and Macaulay2Doc get installed
# before the others:
$(foreach i, $(patsubst Style,,$(ALL_PACKAGES)), $(eval @pre_libm2dir@/$i/.installed: | @pre_libm2dir@/Style/.installed))
$(foreach i, $(patsubst FirstPackage,,$(patsubst Style,,$(ALL_PACKAGES))), $(eval @pre_libm2dir@/$i/.installed: | @pre_libm2dir@/FirstPackage/.installed))
$(foreach i, $(patsubst Macaulay2Doc,,$(patsubst FirstPackage,,$(patsubst Style,,$(ALL_PACKAGES)))), $(eval @pre_libm2dir@/$i/.installed: | @pre_libm2dir@/Macaulay2Doc/.installed))

# the Info-validate function doesn't work well enough to be useful:
# check::check-info

clean::; rm -rf tmp
big-check:
Makefile: Makefile.in; cd ../..; ./config.status Macaulay2/packages/Makefile
distclean:clean
	rm -f Makefile

# requires html5validator:
# python3 -m pip install html5validator
validate-html:
	@ INVALID_PACKAGES="";                                       \
	for PACKAGE in $(PACKAGES);                                  \
	do echo "validating $$PACKAGE documentation ...";            \
	   html5validator --show-warnings                            \
		--root @pre_prefix@/@tail_htmldir@/$$PACKAGE/html || \
		INVALID_PACKAGES="$$INVALID_PACKAGES $$PACKAGE";     \
	done;                                                        \
	if test "$$INVALID_PACKAGES";                                \
	then echo "invalid packages:$$INVALID_PACKAGES";  exit 1;    \
	fi

## here is the list of other files to install along with Macaulay2
@pre_packagesdir@/%.m2 : %.m2 ; @INSTALL_DATA@ $^ $@
# Local Variables:
# compile-command: "make -C $M2BUILDDIR/Macaulay2/packages RemakeAllDocumentation=false IgnoreExampleErrors=false Verbose=false debugLevel=0"
# End:
