include config.make

REBUILD := $(filter rebuild,$(MAKECMDGOALS))
CLEAN :=

.PHONY: all
all: mcvs mcvs-debug mcvs.mem mcvs-debug.mem

mcvs mcvs-debug: config.make
	printf "#!$$(type -P clisp) -M$(prefix)/lib/$@.mem\n" > $@
	printf "(meta-cvs:main)\n" >> $@
CLEAN += mcvs mcvs-debug

ifneq ($(REBUILD),)
.PHONY: rebuild
rebuild: all rebuild-mem rebuild-debug-mem

.PHONY: rebuild-mem rebuild-debug-mem

rebuild-mem: mcvs.mem
rebuild-debug-mem: mcvs-debug.mem

rebuild-mem rebuild-debug-mem:
	clisp -q -M $< \
	  -x "(asdf:operate $(if $(findstring debug,$@),\
			       'asdf:load-source-op,\
			       'asdf:load-op) 'mcvs)" \
	  -x "(ext:saveinitmem \"$<\" :quiet t)"
endif

mcvs.mem mcvs-debug.mem: config.make
	clisp -q \
	  -x "(let ((*load-compiling* t)) \
	        (load \"$(top_srcdir)/asdf/asdf\"))" \
	  -x "(setf asdf:*central-registry* \
	            '(#p\"code/\"))" \
	  -x "(asdf:operate $(if $(findstring debug,$@),\
			       'asdf:load-source-op,\
			       'asdf:load-op) 'mcvs)" \
	  -x "(ext:saveinitmem \"$@\" :quiet t)"
CLEAN += mcvs.mem mcvs-debug.mem

.PHONY: install
install: all
	install -d $(prefix)/bin $(prefix)/lib
	install mcvs mcvs-debug $(prefix)/bin
	install mcvs.mem mcvs-debug.mem $(prefix)/lib

.PHONY: clean
clean:
	-rm $(CLEAN)
