APP=hop_server
TEMPLATES=$(wildcard web/bootstrap/templates/*.xml)
HTML=$(subst web/bootstrap/templates/,web/,$(subst .xml,.html,$(TEMPLATES)))

# Augment the path ocamlfind uses to discover installed packages.
OCAMLPATH=$(CURDIR)/thirdparty/_dist
export OCAMLPATH

LWT_SRC_DIR=thirdparty/lwt-2.3.2

all: \
	thirdparty/_dist \
	message.ml amqp_spec.ml \
	$(APP).native \
	webpages

thirdparty/_dist:
	mkdir -p $@
	(mkdir $@/lwt && \
		(cd $(LWT_SRC_DIR) && \
			./configure && \
			make && \
			OCAMLFIND_LDCONF=ignore \
			OCAMLFIND_DESTDIR="$(CURDIR)/$@" \
				make install))

webpages: $(HTML) web/bootstrap/css/bootstrap.css

web/bootstrap/css/bootstrap.css: web/bootstrap/less/*.less
	recess --compile web/bootstrap/less/bootstrap.less > $@

web/%.html: web/bootstrap/templates/%.xml web/bootstrap/template.xsl web/bootstrap/nav.xml
	xsltproc web/bootstrap/template.xsl $< > $@

message.ml: ../protocol/messages.json codegen.py
	python codegen.py > $@

amqp_spec.ml: amqp0-9-1.stripped.xml amqp_codegen.py
	python amqp_codegen.py > $@

webclean:
	rm -f $(HTML)

clean: webclean
	ocamlbuild -clean
	rm -f message.ml
	rm -f amqp_spec.ml

thirdpartyclean:
	rm -rf thirdparty/_dist
	rm -rf $(LWT_SRC_DIR)/_build
	rm -f $(LWT_SRC_DIR)/setup.data
	rm -f $(LWT_SRC_DIR)/setup.log
	rm -f $(LWT_SRC_DIR)/src/unix/lwt_config.h
	rm -f $(LWT_SRC_DIR)/src/unix/lwt_config.ml

veryclean: clean thirdpartyclean
	rm -f web/bootstrap/css/bootstrap.css

$(APP).native: $(wildcard *.ml)
	ocamlbuild -use-ocamlfind -X thirdparty -X scratch $@

$(APP).p.native: $(wildcard *.ml)
	ocamlbuild -use-ocamlfind -X thirdparty -X scratch $@

run: all
	./$(APP).native
