# 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 telnet_client.cmo 
#ftp_data_endpoint.cmo
XOBJECTS = $(OBJECTS:.cmo=.cmx)
ARCHIVE  = netclient.cma
XARCHIVE = netclient.cmxa
MTARCHIVE  = netclient_mt.cma
MTXARCHIVE = netclient_mt.cmxa
NAME     = netclient
REQUIRES = unix equeue netstring

BYTE_THREAD = -vmthread
NAT_THREAD = -thread

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) -package xstr $(BYTE_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) -package xstr $(NAT_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 $(BYTE_THREAD) -custom netclient_mt.cma testmt.ml \
		-o testmt


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

OPTIONS   =
OCAMLC    = ocamlfind ocamlc  -g $(OPTIONS) -package "$(REQUIRES)"
OCAMLOPT  = ocamlfind ocamlopt $(OPTIONS)   -package "$(REQUIRES)"
OCAMLDEP  = ocamldep $(OPTIONS)
OCAMLFIND = ocamlfind

depend: *.ml *.mli
	#$(OCAMLDEP) *.ml *.mli >depend
	$(OCAMLDEP) http_client.ml http_client.mli \
		telnet_client.ml telnet_client.mli \
		http_client_aux.mli >depend 

.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
	[ ! -d tests ] || $(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
	$(MAKE) -C tests_std distclean
	$(MAKE) -C examples/spider distclean
	$(MAKE) -C examples/telnet_labltk 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)

.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
	$(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
