# mod_caml
# 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.44 2004/03/13 13:58:27 rwmj Exp $

include Makefile.config

SED := sed

OCAMLDOCFLAGS := -html -stars -sort $(OCAMLCINCS)

CC	:= $(OCAMLC)
CFLAGS	:= -I $(APACHEINCDIR) -ccopt -DEAPI

OCAMLCINCS := -I $(PCRELIBDIR) -I $(DBILIBDIR) -I $(APACHEINCDIR)
ALL_CMOS := apache.cmo mod_caml_config.cmo mod_caml.cmo cgi_escape.cmo \
	template.cmo cgi.cmo

ifeq ($(HAVE_CAMLTEMPLATE),1)
CAMLTEMPLATE_DIR := $(shell ocamlfind query camltemplate)
OCAMLCINCS += -I $(CAMLTEMPLATE_DIR)
ALL_CMOS += $(CAMLTEMPLATE_DIR)/camlTemplateInit.cmo
ALL_CMAS += camltemplate.cma
endif

ifeq ($(HAVE_POSTGRES),1)
OCAMLCINCS += -I $(shell ocamlfind query postgres)
OCAMLLIBS += -L$(shell ocamlfind query postgres) -lmlpostgres -lpq
ALL_CMAS += postgres.cma
endif

ifeq ($(HAVE_MYSQL),1)
OCAMLCINCS += -I $(shell ocamlfind query mysql)
OCAMLLIBS += $(shell ocamlfind query mysql)/mysql.a \
	-L$(shell ocamlfind query mysql) -lmysql_stubs -lmysqlclient
ALL_CMAS += mysql.cma
endif

ifeq ($(HAVE_PERL4CAML),1)
# 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

OCAMLCINCS += -I $(shell ocamlfind query perl)
OCAMLLIBS += $(shell ocamlfind query perl)/libperl4caml.a $(DYNALOADER_HACK)
ALL_CMAS += perl4caml.cma
endif

OCAMLCFLAGS := -g -w s $(OCAMLCINCS)
OCAMLLIBS += -lcamlrun -ltermcap -lunix -lstr 
ALL_CMAS += dynlink.cma str.cma pcre.cma unix.cma dbi.cma

all: META config.h mod_caml_config.ml mod_caml.so \
     example-handlers example-scripts example-comments html

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

config.h: config.h.in Makefile.config
	sed -e 's,@APACHELIBDIR@,$(APACHELIBDIR),g' \
	-e 's,@PACKAGE@,$(PACKAGE),g' \
	-e 's,@VERSION@,$(VERSION),g' \
	-e 's,@APACHE_MAJOR@,$(APACHE_MAJOR),g' \
	< $< > $@

mod_caml_config.ml: mod_caml_config.ml.in Makefile.config
	sed -e 's,@APACHELIBDIR@,$(APACHELIBDIR),g' \
	-e 's,@OCAMLLIBDIR@,$(OCAMLLIBDIR),g' \
	-e 's,@OCAMLAPACHEDIR@,$(OCAMLAPACHEDIR),g' \
	-e 's,@PACKAGE@,$(PACKAGE),g' \
	-e 's,@VERSION@,$(VERSION),g' \
	-e 's,@APACHE_MAJOR@,$(APACHE_MAJOR),g' \
	< $< > $@

mod_caml.so: mod_caml_c.o mod_caml.cmo apache_c.o wrappers.o $(ALL_CMOS)
	$(OCAMLC) -linkall -custom $(OCAMLCFLAGS) $(ALL_CMAS) $^ -o $@ \
	  -cclib "-fPIC -shared $(OCAMLLIBS)"

example-handlers: registry.cmo examples/simple-handlers/print_trans.cmo \
	examples/simple-handlers/auth_simple.cmo

example-scripts: examples/simple-scripts/hello.cmo \
	examples/simple-scripts/params.cmo \
	examples/simple-scripts/conditions.cmo \
	examples/simple-scripts/cookies.cmo \
	examples/simple-scripts/mail.cmo \
	examples/simple-scripts/upload_image.cmo \
	examples/useful-scripts/crumbs.cmo \
	examples/useful-scripts/maillist.cmo

ifeq ($(HAVE_POSTGRES),1)
example-comments: examples/comments/comments.cmo \
	examples/comments/comments_login_form.cmo \
	examples/comments/comments_login.cmo \
	examples/comments/delete_comment.cmo \
	examples/comments/post_comment.cmo \
	examples/comments/post_comment_form.cmo \
	examples/comments/show_comments.cmo \
	examples/comments/view_comments.cmo

examples/comments/comments_login_form.cmo: \
		examples/comments/comments_login_form.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I examples/comments -c $<
examples/comments/comments_login.cmo: examples/comments/comments_login.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I examples/comments -c $<
examples/comments/delete_comment.cmo: examples/comments/delete_comment.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I examples/comments -c $<
examples/comments/post_comment.cmo: examples/comments/post_comment.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I examples/comments -c $<
examples/comments/post_comment_form.cmo: examples/comments/post_comment_form.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I examples/comments -c $<
examples/comments/show_comments.cmo: examples/comments/show_comments.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I examples/comments -c $<
examples/comments/view_comments.cmo: examples/comments/view_comments.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I examples/comments -c $<
else
example-comments:
endif

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

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

dep:	.depend
depend: .depend

.depend:
	rm -f .depend
	ocamldep *.mli *.ml */*/*.ml > $@

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

# Documentation.

html:	html/index.html

html/index.html: $(wildcard *.ml) $(wildcard *.mli)
	rm -rf html
	mkdir html
	-$(OCAMLDOC) $(OCAMLDOCFLAGS) -d html $^

clean:
	rm -f *~ *.bak core *.o *.lo *.slo
	rm -f *.cmi *.cmo *.cmx *.cma *.so *.a
	rm -f examples/*~
	rm -f examples/simple-scripts/{*~,*.cmi,*.cmo,*.cmx}
	rm -f examples/useful-scripts/{*~,*.cmi,*.cmo,*.cmx}
	rm -f examples/simple-handlers/{*~,*.cmi,*.cmo,*.cmx}
	rm -f examples/comments/{*~,*.cmi,*.cmo,*.cmx}
	rm -f mod_caml_config.ml config.h META
	rm -rf .libs

install: install-main \
	install-icons \
	install-examples-simple install-examples-useful \
	install-examples-comments \
	install-doc install-html

install-main:
	install -c -m 0755 -d $(DESTDIR)$(APACHELIBDIR)
	install -c -m 0755 -d $(DESTDIR)$(OCAMLAPACHEDIR)
	install -c -m 0755 -d $(DESTDIR)$(MODCAMLLIBDIR)
	install -c -m 0755 mod_caml.so $(DESTDIR)$(APACHELIBDIR)
	install -c -m 0644 mod_caml_config.cmi $(DESTDIR)$(OCAMLAPACHEDIR)
	install -c -m 0644 apache.mli apache.cmi $(DESTDIR)$(OCAMLAPACHEDIR)
	install -c -m 0644 mod_caml.mli mod_caml.cmi \
	$(DESTDIR)$(OCAMLAPACHEDIR)
	install -c -m 0644 cgi.mli cgi.cmi $(DESTDIR)$(OCAMLAPACHEDIR)
	install -c -m 0644 cgi_escape.cmo cgi_escape.mli cgi_escape.cmi \
	$(DESTDIR)$(OCAMLAPACHEDIR)
	install -c -m 0644 template.cmo template.mli template.cmi \
	$(DESTDIR)$(OCAMLAPACHEDIR)
	install -c -m 0644 registry.mli registry.cmi \
	$(DESTDIR)$(OCAMLAPACHEDIR)
	install -c -m 0644 META $(DESTDIR)$(OCAMLAPACHEDIR)
	install -c -m 0644 registry.cmo $(DESTDIR)$(MODCAMLLIBDIR)

install-icons:
	install -c -m 0755 -d $(DESTDIR)$(MODCAMLLIBDIR)/caml-icons
	install -c -m 0644 icons/*.png $(DESTDIR)$(MODCAMLLIBDIR)/caml-icons

install-examples-simple:
	install -c -m 0755 -d $(DESTDIR)$(MODCAMLLIBDIR)/caml-bin
	install -c -m 0644 examples/simple-scripts/*.cmo \
		$(DESTDIR)$(MODCAMLLIBDIR)/caml-bin
	install -c -m 0644 examples/simple-handlers/*.cmo \
	$(DESTDIR)$(MODCAMLLIBDIR)

install-examples-useful:
	install -c -m 0755 -d $(DESTDIR)$(MODCAMLLIBDIR)/caml-bin
	install -c -m 0755 -d $(DESTDIR)$(MODCAMLLIBDIR)/templates
	install -c -m 0644 examples/useful-scripts/*.cmo \
		$(DESTDIR)$(MODCAMLLIBDIR)/caml-bin
	install -c -m 0644 examples/useful-scripts/maillist.html \
		$(DESTDIR)$(MODCAMLLIBDIR)/templates
	install -c -m 0644 examples/useful-scripts/maillist-body.txt \
		$(DESTDIR)$(MODCAMLLIBDIR)/templates

ifeq ($(HAVE_POSTGRES),1)
install-examples-comments:
	install -c -m 0644 examples/comments/*.cmo \
		$(DESTDIR)$(MODCAMLLIBDIR)/caml-bin
	install -c -m 0644 examples/comments/comments.cmo \
		$(DESTDIR)$(MODCAMLLIBDIR)
	install -c -m 0644 examples/comments/comments.cmi \
		$(DESTDIR)$(MODCAMLLIBDIR)
	install -c -m 0755 -d $(DESTDIR)$(MODCAMLLIBDIR)/templates
	install -c -m 0755 -d $(DESTDIR)$(MODCAMLLIBDIR)/templates/comments
	install -c -m 0644 examples/comments/*.html \
		$(DESTDIR)$(MODCAMLLIBDIR)/templates/comments
else
install-examples-comments:
endif

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:
	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 -rf /tmp/sv_upload
#	mkdir -p /tmp/sv_upload/unstable.pkg/$(VERSION)
#	cp $(PACKAGE)-$(VERSION).tar.gz /tmp/sv_upload/unstable.pkg/$(VERSION)
#	scp -2r /tmp/sv_upload/* rwmj@subversions.gnu.org:/upload/modcaml/
#	rm -rf /tmp/sv_upload

upload:
	gpg -b $(PACKAGE)-$(VERSION).tar.gz

check-manifest:
	@for d in `find -type d -name CVS`; \
	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; \
	rm -f .orig-manifest .check-manifest

# Build Debian package.
# NB. Debian source packages cannot contain '_', so we have to pretend this
# module is called modcaml instead of mod_caml.
dpkg:
	@if [ 0 != `cvs -q update | wc -l` ]; then \
	echo Please commit all changes to CVS first.; \
	exit 1; \
	fi
	$(MAKE) dist
	rm -rf /tmp/dbuild
	mkdir /tmp/dbuild
	cp $(PACKAGE)-$(VERSION).tar.gz \
	  /tmp/dbuild/modcaml_$(VERSION).orig.tar.gz
	export CVSROOT=`cat CVS/Root`; \
	  cd /tmp/dbuild && \
	  cvs export \
	  -d modcaml-$(VERSION) \
	  -D now modcaml
	cd /tmp/dbuild/modcaml-$(VERSION) && dpkg-buildpackage -rfakeroot
	rm -rf /tmp/dbuild/modcaml-$(VERSION)
	ls -l /tmp/dbuild

.PHONY: example-handlers example-scripts example-comments \
	install-main install-examples-simple install-examples-useful \
	install-examples-comments \
	dep depend dist upload check-manifest html
.SUFFIXES: .ml .mli .cmi .cmo