# 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.57 2004/10/17 17:04:56 rwmj Exp $

include Makefile.config

TMPDIR ?= /tmp
export TMPDIR

SED := sed

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

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

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

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

all:	META config.h mod_caml_config.ml mod_caml.so 500mod_caml.info \
	$(ALL_CMXS) \
	example-handlers example-scripts example-maillist 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' \
	-e 's,@MAILER_PATH@,$(MAILER_PATH),g' \
	-e 's,@MAILER_ARGS@,$(MAILER_ARGS),g' \
	< $< > $@

500mod_caml.info:
	echo LoadModule caml_module $(APACHELIBDIR)/mod_caml.so > $@

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 "$(PICFLAG) $(SHAREDFLAG) $(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/simple-scripts/exceptions.cmo \
	examples/useful-scripts/crumbs.cmo

ifeq ($(HAVE_POSTGRES),1)
example-maillist: examples/useful-scripts/maillist.cmo

examples/useful-scripts/maillist.cmo: examples/useful-scripts/maillist.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I +postgres -I +dbi -c $<

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.cmo: examples/comments/comments.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I +postgres -I +dbi -I examples/comments -c $<
examples/comments/comments_login_form.cmo: examples/comments/comments_login_form.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I +postgres -I +dbi -I examples/comments -c $<
examples/comments/comments_login.cmo: examples/comments/comments_login.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I +postgres -I +dbi -I examples/comments -c $<
examples/comments/delete_comment.cmo: examples/comments/delete_comment.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I +postgres -I +dbi -I examples/comments -c $<
examples/comments/post_comment.cmo: examples/comments/post_comment.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I +postgres -I +dbi -I examples/comments -c $<
examples/comments/post_comment_form.cmo: examples/comments/post_comment_form.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I +postgres -I +dbi -I examples/comments -c $<
examples/comments/show_comments.cmo: examples/comments/show_comments.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I +postgres -I +dbi -I examples/comments -c $<
examples/comments/view_comments.cmo: examples/comments/view_comments.ml
	$(OCAMLC) $(OCAMLCFLAGS) -I +postgres -I +dbi -I examples/comments -c $<
else
example-maillist:
example-comments:
endif

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

%.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 500mod_caml.info
	rm -f examples/*~
	for d in examples/simple-scripts examples/useful-scripts \
	    examples/simple-handlers examples/comments; do \
		for e in '~' .cmi .cmo .cmx; do \
			rm -f $$d/*$$e; \
		done; \
	done
	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-conf

install-main:
	$(INSTALL)    -m 0755 -d $(DESTDIR)$(APACHELIBDIR)
	$(INSTALL)    -m 0755 -d $(DESTDIR)$(OCAMLAPACHEDIR)
	$(INSTALL)    -m 0755 -d $(DESTDIR)$(MODCAMLLIBDIR)
	$(INSTALL) -c -m 0755 mod_caml.so $(DESTDIR)$(APACHELIBDIR)
	$(INSTALL) -c -m 0644 500mod_caml.info $(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.cmx cgi_escape.o \
	cgi_escape.mli cgi_escape.cmi $(DESTDIR)$(OCAMLAPACHEDIR)
	$(INSTALL) -c -m 0644 template.cmo template.cmx template.o \
	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)    -m 0755 -d $(DESTDIR)$(MODCAMLLIBDIR)/caml-icons
	$(INSTALL) -c -m 0644 icons/*.png $(DESTDIR)$(MODCAMLLIBDIR)/caml-icons

install-examples-simple:
	$(INSTALL)    -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)    -m 0755 -d $(DESTDIR)$(MODCAMLLIBDIR)/caml-bin
	$(INSTALL)    -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)    -m 0755 -d $(DESTDIR)$(MODCAMLLIBDIR)/templates
	$(INSTALL)    -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)    -m 0755 -d $(DESTDIR)$(DOCDIR)
	$(INSTALL) -c -m 0644 AUTHORS FAQ README TODO $(DESTDIR)$(DOCDIR)
	$(INSTALL) -c -m 0644 \
	  examples/simple-scripts/*.ml \
	  $(DESTDIR)$(DOCDIR)

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

install-conf:
	$(INSTALL)    -m 0755 -d $(DESTDIR)$(APACHECONFDIR)
	$(INSTALL) -c -m 0644 modcaml-example.conf $(DESTDIR)$(APACHECONFDIR)

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.
# 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 $(TMPDIR)/dbuild
	mkdir $(TMPDIR)/dbuild
	cp $(PACKAGE)-$(VERSION).tar.gz \
	  $(TMPDIR)/dbuild/modcaml_$(VERSION).orig.tar.gz
	export CVSROOT=`cat CVS/Root`; \
	  cd $(TMPDIR)/dbuild && \
	  cvs export \
	  -d modcaml-$(VERSION) \
	  -D now modcaml
	cd $(TMPDIR)/dbuild/modcaml-$(VERSION) && dpkg-buildpackage -rfakeroot
	rm -rf $(TMPDIR)/dbuild/modcaml-$(VERSION)
	ls -l $(TMPDIR)/dbuild

.PHONY: example-handlers example-scripts example-maillist 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
