# 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  = http_client.cmo
XOBJECTS = http_client.cmx
ARCHIVE  = netclient.cma
XARCHIVE = netclient.cmxa
MTARCHIVE  = netclient_mt.cma
MTXARCHIVE = netclient_mt.cmxa
NAME     = netclient
REQUIRES = netstring unix

all: $(ARCHIVE) $(MTARCHIVE)
non_mt: $(ARCHIVE)

opt: $(XARCHIVE) $(MTXARCHIVE)
non_mt_opt: $(XARCHIVE)

$(ARCHIVE): $(OBJECTS) non_mt/http_client_aux.cmo
	$(OCAMLC) -a -o $(ARCHIVE) non_mt/http_client_aux.cmo $(OBJECTS)

$(XARCHIVE): $(XOBJECTS) non_mt/http_client_aux.cmx 
	$(OCAMLOPT) -a -o $(XARCHIVE) non_mt/http_client_aux.cmx $(XOBJECTS)

$(MTARCHIVE): $(OBJECTS) mt/http_client_aux.cmo
	$(OCAMLC) -a -o $(MTARCHIVE) mt/http_client_aux.cmo $(OBJECTS)

$(MTXARCHIVE): $(XOBJECTS) mt/http_client_aux.cmx
	$(OCAMLOPT) -a -o $(MTXARCHIVE) mt/http_client_aux.cmx $(XOBJECTS)

http_client.cmo: mt/http_client_aux.cmo non_mt/http_client_aux.cmo 
http_client.cmx: mt/http_client_aux.cmx non_mt/http_client_aux.cmx 

mt/http_client_aux.cmo: mt/http_client_aux.ml http_client_aux.cmi
	cp http_client_aux.cmi http_client_aux.mli mt
	$(OCAMLC) `$(OCAMLFIND) use xstr` -thread -c mt/http_client_aux.ml 
mt/http_client_aux.cmx: mt/http_client_aux.ml http_client_aux.cmi
	cp http_client_aux.cmi http_client_aux.mli mt
	$(OCAMLOPT) `$(OCAMLFIND) use xstr` -thread -c mt/http_client_aux.ml 
non_mt/http_client_aux.cmo: non_mt/http_client_aux.ml http_client_aux.cmi
	cp http_client_aux.cmi http_client_aux.mli non_mt
	$(OCAMLC) -c non_mt/http_client_aux.ml 
non_mt/http_client_aux.cmx: non_mt/http_client_aux.ml http_client_aux.cmi
	cp http_client_aux.cmi http_client_aux.mli non_mt
	$(OCAMLOPT) -c non_mt/http_client_aux.ml 

testmt: testmt.ml netclient_mt.cma
	ocamlfind ocamlc -package "$(REQUIRES)" -package xstr,threads \
		-predicates mt_posix \
		-linkpkg -thread -custom netclient_mt.cma testmt.ml \
		-o testmt


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

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

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

depend.pkg: Makefile
	$(OCAMLFIND) query -prefix ROPTIONS= -recursive -separator ' ' -i-format $(REQUIRES) >depend.pkg

.PHONY: install
install: all
	{ test ! -f $(XARCHIVE) -a ! -f $(MTXARCHIVE) || extra="*.cmxa *.a"; }; \
	$(OCAMLFIND) install $(NAME) *.mli *.cmi *.cma META $$extra

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

.PHONY: clean
clean:
	rm -f *.cmi *.cmo *.cma *.cmx *.o *.a *.cmxa
	cd mt; rm -f *.cmi *.cmo *.cma *.cmx *.o *.a *.cmxa
	cd non_mt; rm -f *.cmi *.cmo *.cma *.cmx *.o *.a *.cmxa
	rm -f mt/http_client_aux.mli non_mt/http_client_aux.mli
	$(MAKE) -C tests clean

.PHONY: distclean
distclean: clean
	rm -f *~ depend depend.pkg
	cd mt; rm -f *~
	cd non_mt; rm -f *~
	$(MAKE) -C tests 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" $(NAME)-0.3

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

.PHONY: release
release: distclean
	$(MAKE) tag-release
	$(MAKE) dist

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

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

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

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

include depend
include depend.pkg
