# ocamldbi
# Copyright (C) 2003-2004 Merjis Ltd.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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.  See the GNU
# Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public
# License along with this library; if not, write to the Free
# Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# $Id: Makefile,v 1.20 2004/08/09 14:00:45 rwmj Exp $

include Makefile.config

# XXX Hack required by ocamlc.
# To work out what this should be, try:
# `shell perl -MExtUtils::Embed -e ldopts'
DYNALOADER_HACK := /usr/lib/perl/5.8.2/auto/DynaLoader/DynaLoader.a

TMPDIR ?= /tmp
export TMPDIR

OCAMLCINCS := -I $(PCRELIBDIR)
OCAMLOPTINCS := -w s -I $(PCRELIBDIR)
OCAMLCLIBS := pcre.cma
DBI_DRIVERS :=

ifeq ($(HAVE_POSTGRES),1)
OCAMLCINCS += -I $(shell ocamlfind query postgres)
OCAMLOPTINCS += -I $(shell ocamlfind query postgres)
OCAMLCLIBS += postgres.cma
DBI_DRIVERS += dbi_postgres.ml
endif

ifeq ($(HAVE_POSTGRESQL),1)
OCAMLCINCS += -I $(shell ocamlfind query postgresql)
OCAMLOPTINCS += -I $(shell ocamlfind query postgresql)
OCAMLCLIBS += postgresql.cma
DBI_DRIVERS += dbi_postgresql.ml
endif

ifeq ($(HAVE_MYSQL),1)
OCAMLCINCS += -I $(shell ocamlfind query mysql)
OCAMLOPTINCS += -I $(shell ocamlfind query mysql)
OCAMLCLIBS += mysql.cma
DBI_DRIVERS += dbi_mysql.ml
endif

ifeq ($(HAVE_PERL4CAML),1)
OCAMLCINCS += -I $(shell ocamlfind query perl)
OCAMLOPTINCS += -I $(shell ocamlfind query perl)
OCAMLCLIBS += perl4caml.cma $(DYNALOADER_HACK)
DBI_DRIVERS += dbi_perl.ml
endif

ifeq ($(HAVE_OCAMLODBC),1)
OCAMLCINCS += -I $(shell ocamlfind query ocamlodbc)
OCAMLOPTINCS += -I $(shell ocamlfind query ocamlodbc)
OCAMLCLIBS += ocamlodbc.cma
DBI_DRIVERS += dbi_odbc.ml
endif

ifeq ($(HAVE_SQLITE),1)
OCAMLCINCS += -I $(shell ocamlfind query sqlite)
OCAMLOPTINCS += -I $(shell ocamlfind query sqlite)
OCAMLCLIBS += sqlite.cma
DBI_DRIVERS += dbi_sqlite.ml
endif

ifeq ($(HAVE_FREETDS),1)
OCAMLCINCS += -I $(shell ocamlfind query freetds)
OCAMLOPTINCS += -I $(shell ocamlfind query freetds)
OCAMLCLIBS += freetds.cma
DBI_DRIVERS += dbi_freetds.ml
endif

OCAMLCFLAGS := -g $(OCAMLCINCS)
OCAMLOPTFLAGS := $(OCAMLOPTINCS)
OCAMLDOCFLAGS := -html -stars -sort -colorize-code $(OCAMLCINCS)

SED := sed

ifeq ($(BUILD_OPT),1)
all: byte opt html
else
all: byte html
endif

byte: META dbi.cma $(DBI_DRIVERS:.ml=.cmo)
opt:  META dbi.cmxa $(DBI_DRIVERS:.ml=.cmx)
doc:  html

dbi.cma:  dbi.cmo
dbi.cmxa: dbi.cmx

META:	META.in Makefile.config
	$(SED)  -e 's/@PACKAGE@/$(PACKAGE)/' \
		-e 's/@VERSION@/$(VERSION)/' \
		< $< > $@

# Generic compilation instructions.

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

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

%.cma: %.cmo
	$(OCAMLC) $(OCAMLCFLAGS) -a $^ -o $@

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

%.cmxa: %.cmx
	$(OCAMLOPT) $(OCAMLOPTFLAGS) -a $^ -o $@


.PHONY: dep depend
dep:	.depend
depend: .depend

.depend: dbi.mli dbi.ml $(DBI_DRIVERS)
	rm -f .depend
	ocamldep $^ > $@

ifeq ($(wildcard .depend),.depend)
include .depend
endif

# Documentation.

html:	html/index.html

html/index.html: dbi.mli $(DBI_DRIVERS:.ml=.mli) $(DBI_DRIVERS:.ml=.cmi)
	rm -rf html
	mkdir -p html
	-$(OCAMLDOC) $(OCAMLDOCFLAGS) -d html dbi.mli $(DBI_DRIVERS:.ml=.mli)

clean:
	rm -f *~ *.bak core *.o *.lo *.slo
	rm -f *.cmi *.cmo *.cmx *.cma *.so *.a
	rm -f META
	rm -rf html

# Installation.

ifeq ($(BUILD_OPT),1)
install: install-main install-byte install-opt install-dbi-interfaces \
	install-doc install-html
else
install: install-main install-byte install-dbi-interfaces \
	install-doc install-html
endif

install-byte:
	install -c -m 0644 dbi.cma $(DBI_DRIVERS:.ml=.cmo) \
	  $(DESTDIR)$(OCAMLDBIDIR)
install-opt:
	install -c -m 0644 dbi.cmxa dbi.a $(DBI_DRIVERS:.ml=.cmx) \
	  $(DBI_DRIVERS:.ml=.o) $(DESTDIR)$(OCAMLDBIDIR)

install-main:
	install -c -m 0755 -d $(DESTDIR)$(OCAMLDBIDIR)
	install -c -m 0644 META $(DESTDIR)$(OCAMLDBIDIR)

install-dbi-interfaces:
	install -c -m 0644 dbi.mli dbi.cmi $(DBI_DRIVERS:.ml=.mli) \
		$(DBI_DRIVERS:.ml=.cmi) $(DESTDIR)$(OCAMLDBIDIR)

install-doc:
	install -c -m 0755 -d $(DESTDIR)$(DOCDIR)
	install -c -m 0644 AUTHORS FAQ README TODO $(DESTDIR)$(DOCDIR)

install-html:
	install -c -m 0755 -d $(DESTDIR)$(DOCDIR)/html
	install -c -m 0644 html/*.html $(DESTDIR)$(DOCDIR)/html

dist:
	$(MAKE) check-manifest
	rm -rf $(PACKAGE)-$(VERSION)
	mkdir $(PACKAGE)-$(VERSION)
	tar -cf - -T MANIFEST | tar -C $(PACKAGE)-$(VERSION) -xf -
	tar zcf $(PACKAGE)-$(VERSION).tar.gz $(PACKAGE)-$(VERSION)
	rm -rf $(PACKAGE)-$(VERSION)
	ls -l $(PACKAGE)-$(VERSION).tar.gz

upload:
	rm -f $(PACKAGE)-$(VERSION).tar.gz.sig
	gpg -b $(PACKAGE)-$(VERSION).tar.gz
	ftp-upload -v -h savannah.gnu.org --dir /incoming/savannah/modcaml \
	  $(PACKAGE)-$(VERSION).tar.gz{,.sig}

check-manifest:
	@for d in `find -type d -name CVS | grep -v '^\./debian/'`; \
	do \
	b=`dirname $$d`/; \
	awk -F/ '$$1 != "D" {print $$2}' $$d/Entries | \
	sed -e "s|^|$$b|" -e "s|^\./||"; \
	done | sort > .check-manifest; \
	sort MANIFEST > .orig-manifest; \
	diff -u .orig-manifest .check-manifest; rv=$$?; \
	rm -f .orig-manifest .check-manifest; \
	exit $$rv

# Build Debian package.
dpkg:
	@if [ 0 != `cvs -q update | wc -l` ]; then \
	echo Please commit all changes to CVS first.; \
	exit 1; \
	fi
	$(MAKE) dist
	rm -rf $(TMPDIR)/dbuild
	mkdir $(TMPDIR)/dbuild
	cp $(PACKAGE)-$(VERSION).tar.gz \
	  $(TMPDIR)/dbuild/ocamldbi_$(VERSION).orig.tar.gz
	export CVSROOT=`cat CVS/Root`; \
	  cd $(TMPDIR)/dbuild && \
	  cvs export \
	  -d ocamldbi-$(VERSION) \
	  -D now ocamldbi
	cd $(TMPDIR)/dbuild/ocamldbi-$(VERSION) && dpkg-buildpackage -rfakeroot
	rm -rf $(TMPDIR)/dbuild/ocamldbi-$(VERSION)
	ls -l $(TMPDIR)/dbuild

.PHONY: install-main install-dbi install-dbi-postgres install-dbi-mysql \
	install-dbi-perl install-dbi-odbc \
	install-dbi-postgres-opt install-dbi-mysql-opt \
	install-dbi-perl-opt install-dbi-odbc \
	dep depend dist upload check-manifest html byte opt doc

.SUFFIXES: .ml .mli .cmi .cmo
