##########################################################################
#                                                                        #
#  Copyright (C) Johannes Kanig, Stephane Lescuyer                       #
#  Jean-Christophe Filliatre, Romain Bardou and Francois Bobot           #
#                                                                        #
#  This software is free software; you can redistribute it and/or        #
#  modify it under the terms of the GNU Library General Public           #
#  License version 2.1, with the special exception on linking            #
#  described in file LICENSE.                                            #
#                                                                        #
#  This software is distributed in the hope that it will be useful,      #
#  but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  #
#                                                                        #
##########################################################################

#common variables set by configure script
############################################

# where to install the binaries
prefix=@prefix@
datarootdir = @datarootdir@
datadir = @datadir@
exec_prefix=@exec_prefix@
BINDIR=@bindir@
LIBDIR=@LIBDIR@
USEOCAMLFIND=@USEOCAMLFIND@
OCAMLFIND=@OCAMLFIND@
OCAMLBUILDBIN=@OCAMLBUILD@

# where to install the man page
MANDIR=@mandir@

PSVIEWER=@PSVIEWER@
PDFVIEWER=@PDFVIEWER@

# other variables set by ./configure
OCAMLC   = @OCAMLC@
OCAMLOPT = @OCAMLOPT@
OCAMLDEP = @OCAMLDEP@
OCAMLLEX = @OCAMLLEX@
OCAMLYACC= @OCAMLYACC@
#(not used) OCAMLLIB = @OCAMLLIB@
OCAMLBEST= @OCAMLBEST@
OCAMLVERSION = @OCAMLVERSION@
OCAMLWEB = @OCAMLWEB@
OCAMLWIN32 = @OCAMLWIN32@
EXE = @EXE@
LIBEXT = @LIBEXT@
OBJEXT = @OBJEXT@
TAGS = @TAGS@

INCLUDES = -I gui -I +threads @INCLUDEGTK2@
BFLAGS = -dtypes $(INCLUDES)
OFLAGS = -g -dtypes $(INCLUDES) -for-pack Mlpost

LABLGTK2 = @LABLGTK2@
LABLGTK2LIB = @LABLGTK2LIB@

CAIROLABLGTK2 = @CAIROLABLGTK2@
CAIROLABLGTK2LIB = @CAIROLABLGTK2LIB@

# main target
#############

NAME = mlpost
MLPOSTVERSION=@PACKAGE_VERSION@

# decide which Makefile to use
include ocamlbuild.Makefile

# common part of both Makefiles
##################################
# misc
######
dep:
	$(OCAMLDEP) *.mli *.ml | ocamldot | dot -Tps | $(PSVIEWER) -

wc:
	ocamlwc *.ml* backend/*.ml* -p

man:
	nroff -Tascii -mandoc mlpost.1 | less

# headers
#########

headers:
	headache -c headache_config.txt -h header.txt \
	 *.in README.txt *.mli *.ml *.mll backend/*.ml backend/*.ml[iyl]
	./config.status

# installation
##############

install: install-$(OCAMLBEST) install-bin

install-contrib : install-$(OCAMLBEST)-contrib
install-byte-contrib: install-byte-dot install-byte-lablgtk
install-opt-contrib: install-opt-dot install-opt-lablgtk


BCMA = $(addprefix $(BUILD), $(CMA))
BCMXA = $(addprefix $(BUILD), $(CMXA) $(OBJ))

ifeq "@USEOCAMLFIND@" "no"
install-byte:
	mkdir -p $(LIBDIR)
	cp -f $(BUILD)mlpost.cmi META $(BCMA) "$(LIBDIR)"

install-opt:
	mkdir -p $(LIBDIR)
	cp -f $(BUILD)mlpost.cmi META $(BCMA) "$(LIBDIR)"
	cp -f $(BUILD)mlpost$(LIBEXT) $(BCMXA) "$(LIBDIR)"

install-byte-dot:
	mkdir -p $(LIBDIR)_dot
	cp -f contrib/dot/META "$(LIBDIR)_dot"
	cp -f $(addprefix contrib/dot/_build/mlpost_dot,.cmi .cma) "$(LIBDIR)_dot"

install-opt-dot:
	mkdir -p $(LIBDIR)_dot
	cp -f contrib/dot/META "$(LIBDIR)_dot"
	cp -f $(addprefix contrib/dot/_build/mlpost_dot,.cmi .cma .cmxa $(LIBEXT)) "$(LIBDIR)_dot"

ifeq "$(LABLGTK2)$(CAIROLABLGTK2)$(USEOCAMLFIND)" "yesyesyes"

install-byte-lablgtk:
	mkdir -p $(LIBDIR)_lablgtk
	cp -f contrib/lablgtk/META "$(LIBDIR)_lablgtk"
	cp -f $(addprefix contrib/lablgtk/_build/mlpost_lablgtk,.cmi .cma) "$(LIBDIR)_lablgtk"

install-opt-lablgtk:
	mkdir -p $(LIBDIR)_lablgtk
	cp -f contrib/lablgtk/META "$(LIBDIR)_lablgtk"
	cp -f $(addprefix contrib/lablgtk/_build/mlpost_lablgtk,.cmi .cma .cmxa $(LIBEXT)) "$(LIBDIR)_lablgtk"

else
install-byte-lablgtk:

install-opt-lablgtk:

endif

else
DESTDIR=-destdir $(LIBDIR:/mlpost=)

install-byte:
	$(OCAMLFIND) remove $(DESTDIR) mlpost
	$(OCAMLFIND) install $(DESTDIR) mlpost $(BUILD)mlpost.cmi META $(BCMA)

install-opt:
	$(OCAMLFIND) remove $(DESTDIR) mlpost
	$(OCAMLFIND) install $(DESTDIR) mlpost $(BUILD)mlpost$(LIBEXT) $(BUILD)mlpost.cmi META $(BCMXA) $(BCMA)


install-byte-dot:
	$(OCAMLFIND) remove $(DESTDIR) mlpost_dot
	$(OCAMLFIND) install $(DESTDIR) mlpost_dot contrib/dot/META \
	   $(addprefix contrib/dot/_build/mlpost_dot,.cmi .cma)

install-opt-dot:
	$(OCAMLFIND) remove $(DESTDIR) mlpost_dot
	$(OCAMLFIND) install $(DESTDIR) mlpost_dot contrib/dot/META \
           $(addprefix contrib/dot/_build/mlpost_dot,.cmi .cma .cmxa $(LIBEXT))

ifeq "$(LABLGTK2)$(CAIROLABLGTK2)$(USEOCAMLFIND)" "yesyesyes"
install-byte-lablgtk:
	$(OCAMLFIND) remove $(DESTDIR) mlpost_lablgtk
	$(OCAMLFIND) install $(DESTDIR) mlpost_lablgtk contrib/lablgtk/META \
	   $(addprefix contrib/lablgtk/_build/mlpost_lablgtk,.cmi .cma)

install-opt-lablgtk:
	$(OCAMLFIND) remove $(DESTDIR) mlpost_lablgtk
	$(OCAMLFIND) install $(DESTDIR) mlpost_lablgtk contrib/lablgtk/META \
           $(addprefix contrib/lablgtk/_build/mlpost_lablgtk,.cmi .cma .cmxa $(LIBEXT))
else
install-byte-lablgtk:

install-opt-lablgtk:

endif

endif

install-byte-contrib: install-byte-dot install-byte-lablgtk


install-bin:
	mkdir -p $(BINDIR)
	cp -f $(BUILD)$(TOOL) $(BINDIR)/mlpost
	cp -f mlpost.1 $(MANDIR)/man1

ifeq "@USEOCAMLFIND@" "no"
uninstall: uninstall-contrib
	rm -rf $(LIBDIR)
	rm -f $(BINDIR)/mlpost
	rm -f $(MANDIR)/mlpost
else
uninstall: uninstall-contrib
	$(OCAMLFIND) remove $(DESTDIR) mlpost
	rm -f $(BINDIR)/mlpost
	rm -f $(MANDIR)/mlpost
endif

ifeq "@USEOCAMLFIND@" "no"
uninstall-contrib:
	rm -rf $(LIBDIR)/mlpost_lablgtk
	rm -rf $(LIBDIR)/mlpost_dot
else
uninstall-contrib:
	$(OCAMLFIND) remove $(DESTDIR) mlpost_dot
	$(OCAMLFIND) remove $(DESTDIR) mlpost_lablgtk
endif



# export
########

EXPORTDIR=$(NAME)-$(MLPOSTVERSION)
TAR=$(EXPORTDIR).tar

WWW = /users/www-perso/projets/mlpost
FTP = $(WWW)/download

FILES := $(wildcard *.ml) $(wildcard *.mli) $(wildcard *.mll) \
	 $(wildcard *.in) configure README.txt INSTALL LICENSE CHANGES FAQ \
	 mlpost.1 _tags *.mlpack mlpost_yeslablgtk.odocl mlpost_nolablgtk.odocl ocamlbuild.Makefile
BACKENDFILES = backend/*ml backend/*mli backend/_tags
DVIFILES = dvi/*mly dvi/*mll dvi/*ml dvi/*mli dvi/_tags
CONCRETEFILES = concrete/*ml concrete/*mli concrete/_tags
GENERATEDSOURCEFILES = version.ml myocamlbuild.ml $(GENERATED)
GUIFILES = gui/*.mll gui/*.ml gui/_tags
EXFILES = examples/Makefile examples/*.ml examples/all.template\
	  examples/index.html examples/parse.mll examples/README\
	  examples/prototype.js examples/style.css \
	  examples/powered-by-caml.128x58.png

CONTRIBDOTFILES = $(addprefix contrib/dot/, dot.ml dot.mli Makefile META mlpost_dot.mli mlpost_dot.mlpack _tags xdot_ast.mli xdot_lexer.mll xdot_parser.mly)
CONTRIBLABLGTKFILES = $(addprefix contrib/lablgtk/, META mlpost_lablgtk.ml mlpost_lablgtk.mli)
CUSTOMDOCFILES = customdoc/all.template customdoc/img_doc.ml customdoc/img.ml \
		 customdoc/Makefile customdoc/_tags
LATEXFILES = latex/*sty latex/*tex latex/README

export: export-source export-www export-examples export-doc
	cp README.txt INSTALL LICENSE CHANGES FAQ $(FTP)

export-source: source
	cp export/$(TAR).gz $(FTP)

source:
	mkdir -p export/$(EXPORTDIR)
	cp $(filter-out $(GENERATEDSOURCEFILES), $(FILES)) export/$(EXPORTDIR)
	mkdir -p export/$(EXPORTDIR)/backend
	cp $(BACKENDFILES) export/$(EXPORTDIR)/backend
	mkdir -p export/$(EXPORTDIR)/dvi
	cp $(DVIFILES) export/$(EXPORTDIR)/dvi
	mkdir -p export/$(EXPORTDIR)/concrete
	cp $(CONCRETEFILES) export/$(EXPORTDIR)/concrete
	mkdir -p export/$(EXPORTDIR)/gui
	cp $(GUIFILES) export/$(EXPORTDIR)/gui
	mkdir -p export/$(EXPORTDIR)/examples
	cp $(EXFILES) export/$(EXPORTDIR)/examples
	mkdir -p export/$(EXPORTDIR)/customdoc
	cp $(CUSTOMDOCFILES) export/$(EXPORTDIR)/customdoc
	mkdir -p export/$(EXPORTDIR)/latex
	cp $(LATEXFILES) export/$(EXPORTDIR)/latex
	mkdir -p export/$(EXPORTDIR)/contrib/dot
	mkdir -p export/$(EXPORTDIR)/contrib/lablgtk
	cp $(CONTRIBDOTFILES) export/$(EXPORTDIR)/contrib/dot
	cp $(CONTRIBLABLGTKFILES) export/$(EXPORTDIR)/contrib/lablgtk
	cd export ; tar cf $(TAR) $(EXPORTDIR) ; gzip -f --best $(TAR)


DOCFILES:=$(shell echo *.mli)
DOCFILES:=$(filter-out types.mli, $(DOCFILES))

export-doc: doc
	mkdir -p $(WWW)/doc/img
	cp doc/*.html doc/style.css $(WWW)/doc
	cp doc/img/*.png $(WWW)/doc/img

export-www: www/version.prehtml
	make -C www

www/version.prehtml: Makefile
	echo "<#def version>$(MLPOSTVERSION)</#def>" > www/version.prehtml

export-examples:
	$(MAKEEXAMPLES)
	cp -f --parents examples/*.png examples/*.html examples/*.svg examples/prototype.js examples/style.css $(WWW)

# Emacs tags
############

tags:
	find . -name "*.ml*" | sort -r | xargs \
	etags "--regex=/let[ \t]+\([^ \t]+\)/\1/" \
	      "--regex=/let[ \t]+rec[ \t]+\([^ \t]+\)/\1/" \
	      "--regex=/and[ \t]+\([^ \t]+\)/\1/" \
	      "--regex=/type[ \t]+\([^ \t]+\)/\1/" \
              "--regex=/exception[ \t]+\([^ \t]+\)/\1/" \
	      "--regex=/val[ \t]+\([^ \t]+\)/\1/" \
	      "--regex=/module[ \t]+\([^ \t]+\)/\1/"

.PHONY: ocamlwizard
ocamlwizard:
	ocamlrun -bt ocamlwizard compile types.mli $(CMO:.cmo=.ml) mlpost.mli


# Makefile is rebuilt whenever Makefile.in or configure.in is modified
######################################################################

Makefile META version.ml myocamlbuild.ml: Makefile.in META.in version.ml.in config.status myocamlbuild.ml.in
	./config.status
	chmod a-w myocamlbuild.ml META Makefile version.ml

config.status: configure
	./config.status --recheck

configure: configure.in
	autoconf

