BYTE_ENABLED   = true
NATIVE_ENABLED = $(getenv BATTERIES_NATIVE, true)

USE_OCAMLFIND = true
OCAMLPACKS[] +=
    camomile
    num
    str

#zip_pack = $(shell ocamlfind list | grep "zip\ " | cut -d\  -f1)
#OCAMLPACKS[] += $(zip_pack)

OCAMLCFLAGS += -g

OCAMLDEP_MODULES_ENABLED = false
OCAMLROOT = $(shell ocamlfind printconf destdir)
DOCROOT = $(getenv BATTERIES_DOCROOT,/usr/share/doc/ocaml-batteries/)
BROWSER_COMMAND = x-www-browser %s

NAME = batteries
VERSION = 1.1.0

LIBSDIR = $(ROOT)/libs
DESTOPT = 
if $(defined-env DESTDIR)
    DESTOPT += -destdir $(getenv DESTDIR)
    export DESTOPT

.PHONY: doc uninstall install all clean test reinstall install-doc release
.DEFAULT: all

clean:
    rm -f $(filter-proper-targets $(ls R, .))
    rm -f hdoc/*

BAT_FILES[] = battop.ml

.SUBDIRS: src testsuite
    include OMakefile
    export BAT_FILES

.SUBDIRS: libs
    .SUBDIRS: estring
        include OMakefile
        export BAT_FILES


build/META: build/META.in
	rm(-f build/META)
	sed -e 's|@VERSION@|$(VERSION)|' \
	    build/META.in > build/META
	chmod(444 build/META)

uninstall:
	$(OCAMLFIND) remove $(NAME)
	if $(test -e $(DOCROOT))
	  rm -rf $(DOCROOT)

install: all build/META
	$(OCAMLFIND) install $(DESTOPT) $(NAME) build/META $(BAT_FILES)

MLIS = $(glob src/*.mli)
hdoc/index.html: $(MLIS) $(replacesuffixes .mli,.cmi,$(MLIS))
	$(OCAMLFIND) ocamldoc -package camomile -package threads.posix \
		-sort -html -d hdoc -I src src/*.mli

man/BatStd.3o:
	mkdir man
	$(OCAMLFIND) ocamldoc -package camomile -package threads.posix \
		-sort -man -d man -I src src/*.mli

doc: hdoc/index.html man/BatStd.3o

install-doc: doc
	if $(equal $(OSTYPE), Win32)
		mkdir $(DOCROOT)
		copy hdoc.* doc/batteries/*.idex doc/batteries/*.help $(DOCROOT)
	else
		$(INSTALL) -d $(DOCROOT)
		$(INSTALL) -m 644 hdoc/* $(DOCROOT)
		$(INSTALL) -m 644 doc/batteries/*.idex doc/batteries/*.help $(DOCROOT)

test: $(if $(NATIVE_ENABLED), testsuite/main.opt) testsuite/main.run
	$(if $(NATIVE_ENABLED), testsuite/main.opt &&) testsuite/main.run

release: test :exists: ocaml-batteries-$(VERSION).tar.gz
	git archive --format=tar --prefix=batteries-$(VERSION)/ HEAD | gzip > batteries-$(VERSION).tar.gz


