# $Id: Makefile 44 2008-10-20 00:21:41Z ohl $
# Copyright (C) 2008 by Thorsten Ohl <ohl@physik.uni-wuerzburg.de>
#
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by 
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# It 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
########################################################################

SQLITE3_PATH = $(PWD)/ocaml-sqlite3-1.2.0
SQLITE3_OCAMLC = -I $(SQLITE3_PATH) -cclib -L$(SQLITE3_PATH) \
	-dllpath $(SQLITE3_PATH) sqlite3.cma
SQLITE3_OCAMLOPT = -I $(SQLITE3_PATH) -cclib -L$(SQLITE3_PATH) \
	-cclib -Wl,-rpath=$(SQLITE3_PATH) sqlite3.cmxa

CAMLPDF_PATH = $(PWD)/camlpdf-0.3
CAMLPDF_OCAMLC = -I $(CAMLPDF_PATH) -cclib -L$(CAMLPDF_PATH) \
	-dllpath $(CAMLPDF_PATH) camlpdf.cma
CAMLPDF_OCAMLOPT = -I $(CAMLPDF_PATH) -cclib -L$(CAMLPDF_PATH) \
	-cclib -Wl,-rpath=$(CAMLPDF_PATH) unix.cmxa bigarray.cmxa str.cmxa camlpdf.cmxa

all: bin

bin: screrge thopdfinfo
opt: screrge.opt thopdfinfo.opt

screrge: hex.cmo metadata.cmo scribble.cmo thoPDF.cmo screrge.cmo
	ocamlc -o $@ $(SQLITE3_OCAMLC) $(CAMLPDF_OCAMLC) $^

screrge.opt: hex.cmx metadata.cmx scribble.cmx thoPDF.cmx screrge.cmx
	ocamlopt -o $@ $(SQLITE3_OCAMLOPT) $(CAMLPDF_OCAMLOPT) $^

thopdfinfo: thoPDF.cmo thopdfinfo.cmo
	ocamlc -o $@ $(SQLITE3_OCAMLC) $(CAMLPDF_OCAMLC) $^

thopdfinfo.opt: thoPDF.cmx thopdfinfo.cmx
	ocamlopt -o $@ $(SQLITE3_OCAMLOPT) $(CAMLPDF_OCAMLOPT) $^

test: hex.cmo metadata.cmo scribble.cmo test.cmo
	ocamlc -o $@ $(SQLITE3_OCAMLC) $^

%.cmo: %.ml
	ocamlc -I $(SQLITE3_PATH) -I $(CAMLPDF_PATH) -c $<

%.cmx: %.ml
	ocamlopt -I $(SQLITE3_PATH) -I $(CAMLPDF_PATH) -c $<

%.cmi: %.mli
	ocamlc -I $(SQLITE3_PATH) -I $(CAMLPDF_PATH) -c $<

metadata.cmi: $(SQLITE3_PATH)/sqlite3.cma
thoPDF.cmo: $(CAMLPDF_PATH)/camlpdf.cma

$(SQLITE3_PATH)/sqlite3.cma: ocaml-sqlite3-1.2.0.tar.gz
	tar xzf $<
	cd ocaml-sqlite3-1.2.0 && ./configure
	cd ocaml-sqlite3-1.2.0 && make

ocaml-sqlite3-1.2.0.tar.gz:
	wget http://www.ocaml.info/ocaml_sources/ocaml-sqlite3-1.2.0.tar.gz

$(CAMLPDF_PATH)/camlpdf.cma: camlpdf-0.3.tar.bz2
	tar xjf $<
	cd camlpdf-0.3 && make

camlpdf-0.3.tar.bz2:
	wget http://www.coherentgraphics.co.uk/camlpdf-0.3.tar.bz2

include .depend

depend:
	ocamldep *.mli *.ml > .depend

clean:
	rm -f *~
	rm -f *.cm[ioxa] *.cmxa *.o *~
	rm -f screrge screrge.opt thopdfinfo thopdfinfo.opt
	rm -f test test.opt
	rm -fr screrge-preview

preview:
	rm -fr screrge-preview
	mkdir screrge-preview
	cp -p Makefile .depend *.mli *.ml screrge-preview/
	tar czf screrge-preview.tar.gz screrge-preview
	rm -fr screrge-preview
