###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2009 by The BRLTTY Developers.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any
# later version. Please see the file LICENSE-GPL for details.
#
# Web Page: http://mielke.cc/brltty/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

include $(SRC_TOP)bindings.mk

CAML_OK = @CAML_OK@
OCAMLC = @OCAMLC@
OCAMLOPT = @OCAMLOPT@
OCAMLMKLIB = @OCAMLMKLIB@
OCAMLMKLIB_FLAGS = -dllib dll$(API_NAME)_stubs.so -cclib -l$(API_NAME)_stubs -cclib -l$(API_NAME)
OCAMLMKLIBOPT_FLAGS = -cclib -l$(API_NAME)_stubs -cclib -l$(API_NAME)
OCAMLBEST = @OCAMLBEST@
OCAMLVERSION = @OCAMLVERSION@
OCAMLLIB = @OCAMLLIB@
OCAMLWIN32 = @OCAMLWIN32@
OCAMLFIND = @OCAMLFIND@
OCAML_INSTALL_TARGET = @OCAML_INSTALL_TARGET@
OCAML_UNINSTALL_TARGET = @OCAML_UNINSTALL_TARGET@

OCAML_LIB = $(API_NAME)
OCAML_BCLIB = $(OCAML_LIB).cma
OCAML_NCLIB = @OCAML_NCLIB@

OCAML_MISC = $(SRC_DIR)/META brlapi.mli brlapi.cmi

OCAML_CLIBS = @OCAML_CLIBS@

OCAML_DESTDIR = $(INSTALL_ROOT)$(OCAMLLIB)
OCAML_PKGDIR = $(OCAML_DESTDIR)/$(OCAML_LIB)
OCAML_STUBDIR = $(OCAML_DESTDIR)/stublibs
OCAML_STUB = dll$(OCAML_LIB)_stubs.so
OCAML_OWNER = $(OCAML_STUB).owner

OCAML_FILES = $(OCAML_MISC) $(OCAML_CLIBS) $(OCAML_BCLIB) $(OCAML_NCLIB)

.PHONY: all clean
.PHONY: install install-with-findlib install-without-findlib
.PHONY: uninstall uninstall-with-findlib uninstall-without-findlib

all : $(OCAML_FILES)

$(OCAML_CLIBS) : brlapi_stubs.$O
	$(OCAMLMKLIB) $(API_LDFLAGS) -oc $(OCAML_LIB)_stubs brlapi_stubs.$O

$(OCAML_BCLIB) : $(OCAML_CLIBS) brlapi.cmo
	$(OCAMLC) -a $(OCAMLMKLIB_FLAGS) -o $(OCAML_LIB).cma brlapi.cmo

$(OCAML_NCLIB) : $(OCAML_CLIBS) brlapi.cmx
	$(OCAMLMKLIB) $(OCAMLMKLIBOPT_FLAGS) -o $(OCAML_LIB) brlapi.cmx

brlapi.cmi: brlapi.mli
	$(OCAMLC) -o $@ -c brlapi.mli

brlapi.cmo: brlapi.ml brlapi.cmi
	$(OCAMLC) -o $@ -c brlapi.ml

brlapi.cmx: brlapi.ml brlapi.cmi
	$(OCAMLOPT) -o $@ -c brlapi.cmx brlapi.ml

brlapi_stubs.$O: $(SRC_DIR)/brlapi_stubs.c
	$(OCAMLC) -I $(BLD_TOP)$(PGM_DIR) -I $(SRC_TOP)$(PGM_DIR) -c $(SRC_DIR)/brlapi_stubs.c

brlapi.mli : $(SRC_DIR)/main.mli constants.mli
	cat $(SRC_DIR)/main.mli constants.mli > $@

brlapi.ml : $(SRC_DIR)/main.ml constants.ml
	cat $(SRC_DIR)/main.ml constants.ml > $@

constants.mli : constants.ml
	$(OCAMLC) -o /dev/null -c -i constants.ml > $@

constants.ml: $(CONSTANTS_SCRIPTS) $(CONSTANTS_SOURCES)
	$(AWK) $(CONSTANTS_OPTIONS) $(CONSTANTS_SOURCES) >$@

clean::
	rm -f *.cm* *.a *.so constants.ml* brlapi.ml*

install: all $(OCAML_INSTALL_TARGET)

install-without-findlib:
	$(INSTALL_DIRECTORY) $(OCAML_PKGDIR)
	for file in $(OCAML_FILES); do $(INSTALL) $$file $(OCAML_PKGDIR); done
	$(INSTALL_DIRECTORY) $(OCAML_STUBDIR)
	[ ! -f "$(OCAML_PKGDIR)/$(OCAML_STUB)" ] || mv $(OCAML_PKGDIR)/$(OCAML_STUB) $(OCAML_STUBDIR)
	echo >$(OCAML_STUBDIR)/$(OCAML_OWNER) $(OCAML_LIB)

install-with-findlib:
	$(INSTALL_DIRECTORY) $(OCAML_DESTDIR)
	$(OCAMLFIND) remove -destdir "$(OCAML_DESTDIR)" $(OCAML_LIB)
	$(OCAMLFIND) install -destdir "$(OCAML_DESTDIR)" -ldconf "$(OCAML_DESTDIR)/ld.conf" $(OCAML_LIB) $(OCAML_FILES) 

uninstall: $(OCAML_UNINSTALL_TARGET)

uninstall-without-findlib:
	-rm -f -r -- $(OCAML_PKGDIR)
	-rm -f -- $(OCAML_STUBDIR)/$(OCAML_STUB)
	-rm -f -- $(OCAML_STUBDIR)/$(OCAML_OWNER)

uninstall-with-findlib:
	-$(OCAMLFIND) remove -destdir "$(OCAML_DESTDIR)" $(OCAML_LIB)
