#########################################
# Configuration part 
#########################################

# where to put executable files
prefix=@prefix@
exec_prefix=@exec_prefix@
BINDIR=@bindir@

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

# where to install the doc
DOCDIR = $(prefix)/doc/bibtex2html

#########################################
# End of configuration part
#########################################

VERSION=1.91

OCAMLC   = @OCAMLC@
OCAMLOPT = @OCAMLOPT@ 
CAMLDEP  = @OCAMLDEP@
OCAMLLEX = @OCAMLLEX@
ZLIBS    =
DEBUG    =
FLAGS    = $(ZLIBS) $(DEBUG)
PROFILE  =

STRLIB = -cclib -lstr

OBJS =  options.cmx html.cmx latexmacros.cmx latexscan.cmx bbl_lexer.cmx \
	bibtex.cmx bibtex_parser.cmx bibtex_lexer.cmx \
	readbib.cmx expand.cmx bibfilter.cmx \
	biboutput.cmx version.cmx translate.cmx \
	copying.cmx main.cmx

BIB2BIBOBJS = options.cmx bibtex.cmx bibtex_lexer.cmx \
        bibtex_parser.cmx readbib.cmx \
	latex_accents.cmx condition.cmx \
	condition_parser.cmx condition_lexer.cmx parse_condition.cmx \
	bibfilter.cmx \
	html.cmx biboutput.cmx version.cmx copying.cmx bib2bib.cmx

all: @OCAMLBEST@

opt: bibtex2html bib2bib

byte: bibtex2html.byte bib2bib.byte

bench: bibtex2html
	cd bench; sh bench

test: bibtex2html bib2bib
	make -C bench test

install-indep:
	mkdir -p $(BINDIR)
	cp aux2bib $(BINDIR)/aux2bib
	mkdir -p $(MANDIR)/man1
	cp bibtex2html.1 $(MANDIR)/man1/bibtex2html.1
	cp bibtex2html.1 $(MANDIR)/man1/bib2bib.1
	cp aux2bib.1 $(MANDIR)/man1/aux2bib.1

install: install-indep
	if test @OCAMLBEST@ = opt ; then \
		cp bibtex2html bib2bib $(BINDIR) ; \
	else \
		cp bibtex2html.byte $(BINDIR)/bibtex2html ; \
		cp bib2bib.byte $(BINDIR)/bib2bib ; \
	fi

install-byte: install-indep
	cp bibtex2html.byte $(BINDIR)/bibtex2html
	cp bib2bib.byte $(BINDIR)/bib2bib

local: bibtex2html bib2bib
	cp bibtex2html bib2bib aux2bib $$HOME/bin/$$OSTYPE
	cp *.1 $$HOME/man/man1

demons: bibtex2html bib2bib
	cp bibtex2html bib2bib aux2bib /users/demons/demons/bin/$$OSTYPE
	cp *.1 $$HOME/man/man1

bibtex2html: $(OBJS)
	$(OCAMLOPT) $(PROFILE) $(FLAGS) -o bibtex2html str.cmxa $(OBJS) $(STRLIB)
	strip bibtex2html$(EXE)

bibtex2html.byte: $(OBJS:.cmx=.cmo)
	$(OCAMLC) $(PROFILE) $(FLAGS)-o bibtex2html.byte str.cma $(OBJS:.cmx=.cmo) $(STRLIB) 

bibtex2html.pbyte: $(OBJS:.cmx=.cmo)
	$(OCAMLC) -use-runtime ~demons/bin/$(OSTYPE)/ocamlcustomrun \
		-o bibtex2html.pbyte str.cma $(OBJS:.cmx=.cmo)  

bibtex2html.static: $(OBJS)
	$(OCAMLOPT) $(PROFILE) $(FLAGS) -o $@ str.cmxa $(OBJS) $(STRLIB) -cclib "-L. -static"
	strip $@

SRC=$(OBJS:.cmx=.ml)
bibtex2html.dfc: $(SRC)
	ocamldefun -d defun `ocamldefun-args $(SRC)`
	cd defun; $(OCAMLOPT) $(FLAGS) -o ../$@ str.cmxa $(SRC)

bib2bib: $(BIB2BIBOBJS)
	$(OCAMLOPT) $(PROFILE) $(FLAGS) -o bib2bib str.cmxa $(BIB2BIBOBJS) $(STRLIB)
	strip bib2bib$(EXE)

bib2bib.byte: $(BIB2BIBOBJS:.cmx=.cmo)
	$(OCAMLC)  $(PROFILE) $(FLAGS) -o bib2bib.byte str.cma $(BIB2BIBOBJS:.cmx=.cmo) $(STRLIB)

bib2bib.pbyte: $(BIB2BIBOBJS:.cmx=.cmo)
	$(OCAMLC) -use-runtime ~demons/bin/$(OSTYPE)/ocamlcustomrun \
		-o bib2bib.pbyte str.cma $(BIB2BIBOBJS:.cmx=.cmo) 

bib2bib.static: $(BIB2BIBOBJS)
	$(OCAMLOPT) $(PROFILE) $(FLAGS) -o $@ str.cmxa $(BIB2BIBOBJS) $(STRLIB) -cclib "-L. -static"
	strip $@

static: bibtex2html.static bib2bib.static
	cp bibtex2html.static $$HOME/bin/$$OSTYPE/bibtex2html
	cp bib2bib.static $$HOME/bin/$$OSTYPE/bib2bib

bibtex_parser.mli bibtex_parser.ml: bibtex_parser.mly
	ocamlyacc bibtex_parser.mly

condition_parser.mli condition_parser.ml: condition_parser.mly
	ocamlyacc condition_parser.mly

version.ml: Makefile
	echo "let version = \""$(VERSION)"\"" > version.ml
	echo "let date = \""`date`"\"" >> version.ml

latexscan.ml: latexscan.mll
	$(OCAMLLEX) latexscan.mll

bibtex_lexer.ml: bibtex_lexer.mll
	$(OCAMLLEX) bibtex_lexer.mll

# literate programming
######################

lp: bibtex2html.dvi

WEBFILES=options.mli options.ml latexmacros.mli latexmacros.ml \
         latexscan.mll bbl_lexer.mll \
         bibtex.mli bibtex.ml bibtex_lexer.mll bibtex_parser.mly \
	 readbib.mli readbib.ml expand.mli expand.ml \
         bibfilter.mli bibfilter.ml \
         html.ml biboutput.mli biboutput.ml version.mli \
         translate.mli translate.ml copying.mli copying.ml main.ml

bibtex2html.tex: $(WEBFILES)
	ocamlweb -o $@ $(WEBFILES)

.SUFFIXES: .tex .dvi .ps

.tex.dvi:
	latex $< && latex $<

.dvi.ps:
	dvips $< -o $@

# export
########

NAME=bibtex2html-$(VERSION)

FTP = /users/demons/filliatr/ftp/ocaml/bibtex2html
WWWSRC = /users/demons/filliatr/www/bibtex2html

SOURCEFILES = *.ml* aux2bib.in Makefile.in configure.in configure .depend README COPYING GPL CHANGES manual.tex *.1

export: source export-doc linux
	echo "<#def version>$(VERSION)</#def>" > $(WWWSRC)/version.prehtml
	make -C $(WWWSRC) install
	mail -s "nouvelle release de bibtex2html" treinen@debian.org < /dev/null
	echo "*** prevenir Gerald Pfeifer <pfeifer@dbai.tuwien.ac.at> ***"

move-olds:
	cp $(FTP)/bibtex2html* $(FTP)/olds

source: clean 
	mkdir -p export/$(NAME)
	cp $(SOURCEFILES) export/$(NAME)
	(cd export ; tar cf $(NAME).tar $(NAME) ; \
	gzip -f --best $(NAME).tar)
	if test -f $(FTP)/$(NAME).tar.gz && \
	   ! cmp -s export/$(NAME).tar.gz $(FTP)/$(NAME).tar.gz; then \
		echo "IL FAUT CHANGER DE NUM�RO DE VERSION"; exit 1; \
	fi
	cp README COPYING GPL CHANGES export/$(NAME).tar.gz $(FTP)

BINARY = bibtex2html-$(VERSION)-$(OSTYPE)

linux: clean binary

solaris:
	ssh sun-graphe make -C $(HOME)/soft/ocaml/bibtex clean binary

sunos4:
	rmake ??? $(HOME)/soft/ocaml/bibtex clean binary

BINARYFILES = README COPYING GPL bibtex2html bib2bib aux2bib \
	      bibtex2html.1 bib2bib.1 aux2bib.1

binary: bibtex2html bib2bib
	mkdir -p export/$(BINARY)
	cp $(BINARYFILES) export/$(BINARY)
	(cd export; tar czf $(BINARY).tar.gz $(BINARY))
	cp export/$(BINARY).tar.gz $(FTP)

AIX=bibtex2html-$(VERSION)-AIX

aix:
	mkdir -p export/$(AIX)
	cp $(BINARYFILES) export/$(AIX)
	(cd export; tar cf $(AIX).tar $(AIX); gzip --best $(AIX).tar)

# documentation
###############

WWW=/users/demons/filliatr/WWW/bibtex2html

doc: manual.ps manual.html

install-doc: manual.ps manual.html
	mkdir -p $(DOCDIR)
	gzip -c manual.ps > $(DOCDIR)/manual.ps.gz
	cp -f manual.html $(DOCDIR)

export-doc: manual.ps manual.html
	gzip -c manual.ps > $(WWW)/doc/manual.ps.gz
	cp -f manual.html $(WWW)/doc

manual.ps: manual.tex
	latex manual && latex manual && latex manual
	dvips manual.dvi -o manual.ps

manual.html: manual.tex
	hevea -fix manual.tex

# generic rules :
#################

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

.mli.cmi:
	$(OCAMLC) -c $(FLAGS) $<

.ml.cmo:
	$(OCAMLC) -c $(FLAGS) $<

.ml.o:
	$(OCAMLOPT) -c $(FLAGS) $<

.ml.cmx:
	$(OCAMLOPT) -c $(PROFILE) $(FLAGS) $<

.mll.ml:
	$(OCAMLLEX) $<

# 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/"

# myself

Makefile: Makefile.in config.status
	./config.status

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

configure: configure.in
	autoconf 

# clean and depend
##################

GENERATED = bibtex_lexer.ml bbl_lexer.ml \
	    latexscan.ml latex_accents.ml condition_lexer.ml \
	    bibtex_parser.mli bibtex_parser.ml \
            condition_parser.mli condition_parser.ml version.ml

clean::
	rm -f *~ *.cm[iox] *.o 
	rm -f $(GENERATED)
	rm -f bibtex2html bib2bib
	rm -f manual.{aux,log,dvi,ps,toc,haux,html}
	rm -f bibtex2html.static bib2bib.static
	rm -f bibtex2html.byte bib2bib.byte

dist-clean:: clean
	rm -f Makefile aux2bib

depend:: $(GENERATED)
	rm -f .depend
	ocamldep $(ZLIBS) *.mli *.ml > .depend

include .depend

