ifndef DUCE
  DUCE := $(shell if [ `which ocamlduce` ]; then echo yes; else echo no; fi)
endif

LIBNAME = textile
VERSION := $(shell head -n 1 VERSION)

TEXTILE = textile_parsercomb.ml textile.ml textile_parser.ml
HTML    = textile_html.ml
DUCEF   = textile_duce.ml

CAMLSRC = $(TEXTILE) $(HTML)
DUCESRC = xhtmlpretty_duce.ml $(DUCEF)

DOCSRC = textile.ml textile_parser.ml textile_html.ml textile.mli textile_parser.mli textile_html.mli
DUCEDOCSRC = $(DOCSRC) textile_duce.ml textile_duce.mli

PACKAGES = extlib
DUCEPACKAGES = ocamlduce,tyxml.duce

DUCELIB = -package $(PACKAGES),$(DUCEPACKAGES)
DUCEC   = ocamlducefind ocamlc   -thread $(DUCELIB)
DUCEOPT = ocamlducefind ocamlopt -thread $(DUCELIB)
DUCEDOC = ocamlducefind ocamldoc -keep-code -colorize-code $(DUCELIB)
DUCEDEP = ocamlducefind ocamldep

LIB = -package $(PACKAGES)
CAMLC   = ocamlfind ocamlc   $(LIB)
CAMLOPT = ocamlfind ocamlopt $(LIB)
CAMLDOC = ocamlfind ocamldoc -keep-code -colorize-code $(LIB)
CAMLDEP = ocamlfind ocamldep

CMA  = textile.cma
CMXA = textile.cmxa
CMXS = textile.cmxs
DUCECMA  = textile_duce.cma
DUCECMXA = textile_duce.cmxa
DUCECMXS = textile_duce.cmxs

all: byte native shared

ifeq "$(DUCE)" "yes"
byte:          textile.cma  textile_html.cma  textile_duce.cma  META
native:        textile.cmxa textile_html.cmxa textile_duce.cmxa META
shared: native textile.cmxs textile_html.cmxs textile_duce.cmxs META
else
byte:          textile.cma  textile_html.cma  META
native:        textile.cmxa textile_html.cmxa META
shared: native textile.cmxs textile_html.cmxs META
endif

META: META.in META.duce.in VERSION
	cp META.in META
	sed "s/_NAME_/$(LIBNAME)/" -i META
	sed "s/_VERSION_/$(VERSION)/" -i META
	sed "s/_REQUIRES_/$(PACKAGES)/" -i META
	sed "s/_BYTE_/$(CMA)/" -i META
	sed "s/_NATIVE_/$(CMXA)/" -i META
	sed "s/_SHARED_/$(CMXS)/" -i META
ifeq "$(DUCE)" "yes"
	cp META.duce.in META.duce
	sed "s/_NAME_/duce/" -i META.duce
	sed "s/_REQUIRES_/$(DUCEPACKAGES)/" -i META.duce
	sed "s/_BYTE_/$(DUCECMA)/" -i META.duce
	sed "s/_NATIVE_/$(DUCECMXA)/" -i META.duce
	sed "s/_SHARED_/$(DUCECMXS)/" -i META.duce
	cat META.duce >> META
endif

textile.cma:  $(TEXTILE:.ml=.cmo)
	$(CAMLC) -a -o $@ $^
textile.cmxa: $(TEXTILE:.ml=.cmx)
	$(CAMLOPT) -a -o $@ $^
textile.cmxs: $(TEXTILE:.ml=.cmx) textile.cmxa
	$(CAMLOPT) -a -o $@ $(TEXTILE:.ml=.cmx)

textile_html.cma:  $(HTML:.ml=.cmo)
	$(CAMLC) -a -o $@ $^
textile_html.cmxa: $(HTML:.ml=.cmx)
	$(CAMLOPT) -a -o $@ $^
textile_html.cmxs: $(HTML:.ml=.cmx) textile_html.cmxa
	$(CAMLOPT) -a -o $@ $(HTML:.ml=.cmx)

textile_duce.cmo: textile_duce.ml
	$(DUCEC) -c $<
textile_duce.cmi: textile_duce.mli
	$(DUCEC) -c $<
textile_duce.cmx: textile_duce.ml
	$(DUCEOPT) -c $<

textile_duce.cma:  $(DUCEF:.ml=.cmo)
	$(DUCEC) -a -o $@ $^
textile_duce.cmxa: $(DUCEF:.ml=.cmx)
	$(DUCEOPT) -a -o $@ $^
textile_duce.cmxs: $(DUCEF:.ml=.cmx) textile_duce.cmxa
	$(DUCEOPT) -a -o $@ $(DUCEF:.ml=.cmx)

textiler.cmx: textile.cmxa textile_html.cmxa textiler.ml
	$(CAMLOPT) -c $^
textiler: textile.cmxa textile_html.cmxa textiler.cmx
	$(CAMLOPT) -linkpkg -o $@ $^
textiler.cmo: textile.cma textile_html.cma textiler.ml
	$(CAMLC) -c $^
textiler.byte: textile.cma textile_html.cma textiler.cmo
	$(CAMLC) -linkpkg -o $@ $^

xhtmlpretty_duce.cmi: xhtmlpretty_duce.mli
	$(DUCEC) -c $<
xhtmlpretty_duce.cmx: xhtmlpretty_duce.ml
	$(DUCEOPT) -c $<
textiler_duce.cmx: native textiler_duce.ml xhtmlpretty_duce.cmx
	$(DUCEOPT) -I ./ -package textile.duce -c textiler_duce.ml
textiler_duce: xhtmlpretty_duce.cmx textiler_duce.cmx
	$(DUCEOPT) -I ./ -package textile.duce -linkpkg -o $@ $^

tests: textiler textiler_duce

install:
	ocamlfind install $(LIBNAME) META *.cmi *.cma *.cmxa *.cmxs *.a

uninstall:
	ocamlfind remove $(LIBNAME)

.SUFFIXES:
.SUFFIXES: .ml .mli .cmo .cmi .cmx

.PHONY: doc

.ml.cmo:
	$(CAMLC) -c $<
.mli.cmi:
	$(CAMLC) -c $<
.ml.cmx:
	$(CAMLOPT) -c $<

doc: byte
	-mkdir -p doc
ifeq "$(DUCE)" "yes"
	$(DUCEDOC) -d doc -html $(DUCEDOCSRC)
else
	$(CAMLDOC) -d doc -html $(DOCSRC)
endif


clean:
	-rm -f *.cm[ioxa] *.o *.a *.cmx[as] *~
	-rm -f .depend
	-rm -rf doc
	-rm -f META META.duce
	-rm -f textiler textiler.byte textiler_duce

depend: .depend

ifeq "$(DUCE)" "yes"
.depend: $(CAMLSRC) $(DUCESRC)
	$(DUCEDEP) $(LIB) $(CAMLSRC:.ml=.mli) $(CAMLSRC) $(DUCESRC:.ml=.mli) $(DUCESRC) > .depend
else
.depend: $(CAMLSRC)
	$(CAMLDEP) $(LIB) $(CAMLSRC:.ml=.mli) $(CAMLSRC) > .depend
endif

FORCE:

-include .depend
