# make all: 		make bytecode archive
# make opt: 		make native archive
# make install: 	install bytecode archive, and if present, native archive
# make uninstall: 	uninstall package
# make clean: 		remove intermediate files
# make distclean: 	remove any superflous files
# make release: 	cleanup, create archive, tag CVS module 
#			(for developers)

#----------------------------------------------------------------------
# specific rules for this package:

OBJECTS  = netstring_str.cmo netdate.cmo \
           netencoding.cmo netbuffer.cmo netstream.cmo \
	   mimestring.cmo cgi.cmo base64.cmo \
           nethtml_scanner.cmo nethtml.cmo \
	   neturl.cmo \
	   netmappings.cmo netconversion.cmo
XOBJECTS = $(OBJECTS:.cmo=.cmx)
ARCHIVE  = netstring.cma
XARCHIVE = netstring.cmxa

NAME     = netstring
REQUIRES = str

ISO_MAPPINGS   = mappings/iso*.unimap
OTHER_MAPPINGS = mappings/cp*.unimap \
                 mappings/adobe*.unimap \
	         mappings/jis*.unimap \
                 mappings/koi*.unimap \
                 mappings/mac*.unimap \
                 mappings/windows*.unimap

all: $(ARCHIVE) \
     netstring_top.cmo netstring_mt.cmo \
     netmappings_iso.cmo netmappings_other.cmo

opt: $(XARCHIVE) \
     netstring_mt.cmx \
     netmappings_iso.cmx netmappings_other.cmx


$(ARCHIVE): $(OBJECTS) 
	$(OCAMLC) -a -o $(ARCHIVE) $(OBJECTS)

$(XARCHIVE): $(XOBJECTS) 
	$(OCAMLOPT) -a -o $(XARCHIVE) $(XOBJECTS)

netmappings_iso.ml:
	$(MAKE) -C tools 
	test ! -d mappings || tools/unimap_to_ocaml/unimap_to_ocaml \
		-o netmappings_iso.ml $(ISO_MAPPINGS)

netmappings_other.ml:
	$(MAKE) -C tools 
	test ! -d mappings || tools/unimap_to_ocaml/unimap_to_ocaml \
		-o netmappings_other.ml $(OTHER_MAPPINGS)

#----------------------------------------------------------------------
# general rules:

OPTIONS   =
OCAMLC    = ocamlc $(DEBUG) $(OPTIONS) $(ROPTIONS)
OCAMLOPT  = ocamlopt $(OPTIONS) $(ROPTIONS)
OCAMLLEX  = ocamllex
OCAMLDEP  = ocamldep $(OPTIONS)
OCAMLFIND = ocamlfind

DEBUG  =
# Invoke with: make DEBUG=-g

depend: *.ml *.mli
	$(OCAMLDEP) *.ml *.mli >depend

depend.pkg: Makefile
	$(OCAMLFIND) use -p ROPTIONS= $(REQUIRES) >depend.pkg

.PHONY: install
install: all
	{ test ! -f $(XARCHIVE) || extra="*.cmxa *.a netstring_mt.cmx netmappings_iso.cmx netmappings_other.cmx netstring_mt.o netmappings_iso.o netmappings_other.o"; }; \
	$(OCAMLFIND) install $(NAME) *.mli *.cmi *.cma netstring_top.cmo netstring_mt.cmo netmappings_iso.cmo netmappings_other.cmo META $$extra

.PHONY: install-cgi
install-cgi: 
	$(OCAMLFIND) install cgi compat-cgi/META


.PHONY: install-base64
install-base64: 
	$(OCAMLFIND) install base64 compat-base64/META


.PHONY: uninstall
uninstall:
	$(OCAMLFIND) remove $(NAME)

.PHONY: uninstall-cgi
uninstall-cgi:
	$(OCAMLFIND) remove cgi

.PHONY: uninstall-base64
uninstall-base64:
	$(OCAMLFIND) remove base64

.PHONY: clean
clean:
	rm -f *.cmi *.cmo *.cma *.cmx *.o *.a *.cmxa
	test ! -d mappings || rm -f netmappings_iso.ml netmappings_other.ml

.PHONY: distclean
distclean:
	rm -f *.cmi *.cmo *.cma *.cmx *.o *.a *.cmxa
	rm -f *~ depend depend.pkg compat-cgi/*~ compat-base64/*~
	$(MAKE) -C tests distclean
	$(MAKE) -C doc distclean
	$(MAKE) -C tools distclean

RELEASE: META
	awk '/version/ { print substr($$3,2,length($$3)-2) }' META >RELEASE

.PHONY: dist
dist: RELEASE
	r=`head -1 RELEASE`; cd ..; gtar czf $(NAME)-$$r.tar.gz --exclude='*/CVS*' --exclude="*/depend.pkg" --exclude="*/depend" --exclude="*/doc/common.xml" --exclude="*/doc/config.xml" --exclude="*/doc/readme.dtd" --exclude="*/Mail" --exclude="*/mappings" $(NAME)

.PHONY: tag-release
tag-release: RELEASE
	r=`head -1 RELEASE | sed -e s/\\\./-/g`; cd ..; cvs tag -F $(NAME)-$$r $(NAME)

.PHONY: release
release: distclean
	test -f netmappings_iso.ml
	test -f netmappings_other.ml
	$(MAKE) tag-release
	$(MAKE) dist

.SUFFIXES: .cmo .cmi .cmx .ml .mli .mll

.ml.cmx:
	$(OCAMLOPT) -c -thread $<

.ml.cmo:
	$(OCAMLC) -g -c -thread $<

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

.mll.ml:
	$(OCAMLLEX) $<

include depend
include depend.pkg
