OCAMLC := @OCAMLC@
OCAMLOPT := @OCAMLOPT@
OCAMLDEP := @OCAMLDEP@
OCAMLLIB := @OCAMLLIB@
OCAMLFIND := @OCAMLFIND@
OCAMLDOC := @OCAMLDOC@
OCAMLMKLIB := @OCAMLMKLIB@

INSTALL := @INSTALL@
INSTALL_DATA := @INSTALL_DATA@
INSTALLDIR := @INSTALLDIR@

CLIBS := $(filter -l%,@LIBS@)
CLIBSDIR := $(filter -L%,@LIBS@)
BZ_PREFIX := @BZ_PREFIX@
CPPFLAGS = $(if $(BZ_PREFIX),-D BZ_PREFIX) @CPPFLAGS@

NAME := camlbz2
SHORTNAME := bz2
VERSION := 0.6.0

INSTALLFILES := $(SHORTNAME).cmi $(SHORTNAME).cmx $(SHORTNAME).mli \
	$(SHORTNAME).a $(SHORTNAME).cma $(SHORTNAME).cmxa \
	dll$(NAME).so lib$(NAME).a

DISTSRC := $(SHORTNAME).ml $(SHORTNAME).mli \
	c_bz.c io.h \
	aclocal.m4 install-sh \
	Makefile.in configure configure.in \
	META doc \
	BUGS COPYING INSTALL LICENSE README ROADMAP ChangeLog

DESTDIR =

ifndef OCAMLOPT
all : stub lib
else
all : stub lib libopt
endif

ifdef OCAMLFIND
install : findlib-install
ifeq ($(DESTDIR),)
DESTDIR_FLAGS =
else
DESTDIR_FLAGS = -destdir $(DESTDIR)
findlib-install : install-mkdir
install-mkdir :
	test -d $(DESTDIR) || mkdir -p $(DESTDIR)
endif
else
install : normal-install
endif

stub   : dll$(NAME).so lib$(NAME).a
lib    : $(SHORTNAME).cma
libopt : $(SHORTNAME).cmxa

lib$(NAME).a dll$(NAME).so : c_bz.o
	$(OCAMLMKLIB) -oc $(NAME) $^ $(CLIBS) $(CLIBSDIR)

$(SHORTNAME).cma : $(SHORTNAME).cmo
	$(OCAMLMKLIB) -oc $(NAME) -o $(SHORTNAME) $^ $(CLIBS) $(CLIBSDIR)

$(SHORTNAME).cmxa : bz2.cmx
	$(OCAMLMKLIB) -oc $(NAME) -o $(SHORTNAME) $^ $(CLIBS) $(CLIBSDIR)


test : INCDIR= $(if $(OCAMLFIND),$(shell ocamlfind query $(SHORTNAME)),$(INSTALLDIR))
test : test.ml
	$(OCAMLC) -o $@ -dllpath $(INCDIR) -I $(INCDIR) $(SHORTNAME).cma $^

test.opt : INCDIR= $(if $(OCAMLFIND),$(shell ocamlfind query $(SHORTNAME)),$(INSTALLDIR))
test.opt : test.ml
	$(OCAMLOPT) -o $@ -I $(INCDIR) $(SHORTNAME).cmxa $^

normal-install: all
	mkdir -p $(INSTALLDIR)
	$(INSTALL_DATA) $(INSTALLFILES) $(INSTALLDIR)
	@echo "You need to put a link to the shared stub in $$($(OCAMLC) -where)/stublibs"

findlib-install: all
	-$(OCAMLFIND) remove $(DESTDIR_FLAGS) $(SHORTNAME)
	$(OCAMLFIND) install $(DESTDIR_FLAGS) \
		-patch-version $(VERSION) -ldconf ignore \
		$(SHORTNAME) META $(INSTALLFILES)

clean:
	rm -f *.cm* *.a *.so *.o *.opt test test.opt
distclean: clean
	rm -rf .depend Makefile aclocal.m4 autom4te.cache/ doc/
	rm -f config.log config.status configure

ifdef OCAMLDOC
doc : doc/index.html
doc/index.html : bz2.mli
	mkdir -p doc/
	$(OCAMLDOC) -html -d doc $^ 
else
doc : ; $(error "you need ocamldoc to generate the documentation")
endif

dist: doc $(NAME)-$(VERSION).tar.gz

$(NAME)-$(VERSION).tar.gz: dummy
	cd .. && mv $(NAME) $(NAME)-$(VERSION) && \
	tar zcvf $@ $(addprefix $(NAME)-$(VERSION)/, $(DISTSRC)) && \
	mv $(NAME)-$(VERSION) $(NAME)

.PHONY: all lib libopt clean doc dist normal-install findlib-install dummy stub

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

%.cmx : %.ml
	$(OCAMLOPT) -c $<

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

%.o : %.c
	$(OCAMLC) -ccopt "$(CPPFLAGS) $(CFLAGS)" -c $<

.depend : $(wildcard *.ml *.mli)
	-@$(OCAMLDEP) $^ > .depend

-include .depend

configure : configure.in aclocal.m4
	autoconf
aclocal.m4 : ocaml.m4
	aclocal -I .
