# make all: 		make bytecode archive
# make opt: 		make native archive
# make world:           make as much as possible
# make findlib-install:	install bytecode archive, and if present, native archive
# make findlib-uninstall: uninstall package
# make conventional-install, conventional-uninstall
# make clean: 		remove intermediate files
# make distclean: 	remove any superflous files
#----------------------------------------------------------------------

# Config:

# INSTALLDIR: directory where to install the files for conventional-install.
# This must be an absolute path.
# Leave this variable empty if you are going to findlib-install.
#INSTALLDIR = /opt/ocaml-3.00/lib
INSTALLDIR = 

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

OBJECTS  = unix_exts.cmo shell_sys.cmo shell.cmo
XOBJECTS = $(OBJECTS:.cmo=.cmx)
COBJECTS = unix_exts_c.o
ARCHIVE  = shell.cma
XARCHIVE = shell.cmxa
CARCHIVE = libshell.a
CLINK    = -lshell
NAME     = shell

world: all try_opt

all: $(ARCHIVE) $(CARCHIVE)

opt: $(XARCHIVE) $(CARCHIVE)

try_opt:
	test ! ocamlopt >/dev/null 2>/dev/null || $(MAKE) opt

$(ARCHIVE): $(OBJECTS)
	if [ -n "$(INSTALLDIR)" ]; then                                      \
		autolink="-ccopt -L$(INSTALLDIR) -cclib $(CLINK)";           \
	else                                                                 \
	        autolink="-cclib $(CLINK)";                                  \
	fi;                                                                  \
	$(OCAMLC) -a -o $(ARCHIVE) $$autolink $(OBJECTS)

$(XARCHIVE): $(XOBJECTS)
	if [ -n "$(INSTALLDIR)" ]; then                                      \
		autolink="-ccopt -L$(INSTALLDIR) -cclib $(CLINK)";           \
	else                                                                 \
	        autolink="-cclib $(CLINK)";                                  \
	fi;                                                                  \
	$(OCAMLOPT) -a -o $(XARCHIVE) $$autolink $(XOBJECTS)

$(CARCHIVE): $(COBJECTS)
	if ocamlmklib; then                                                  \
		ocamlmklib -o shell $(COBJECTS);                             \
	else                                                                 \
		rm -f $(CARCHIVE);                                           \
		ar q $(CARCHIVE) $(COBJECTS);                                \
		ranlib $(CARCHIVE);                                          \
	fi

t: all
	OCAMLPATH=. ocamlfind ocamlmktop -o t -package . -linkpkg

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

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

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

.PHONY: install
install: findlib-install

.PHONY: findlib-install
findlib-install: all
	files=`./collect_files *.mli *.cmi *.cma META *.cmxa *.a *.so` && \
	$(OCAMLFIND) install $(NAME) $$files

.PHONY: conventional-install
conventional-install: all
	test -n "$(INSTALLDIR)"
	mkdir -p $(INSTALLDIR)
	files=`./collect_files *.mli *.cmi *.cma META *.cmxa *.a *.so` && \
	FILES="$$files packlist-$(NAME)" && \
	echo $$FILES >packlist-$(NAME) && \
	cp $$FILES $(INSTALLDIR)

.PHONY: uninstall
uninstall: findlib-uninstall

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

.PHONY: conventional-uninstall
conventional-uninstall: 
	cd $(INSTALLDIR) && rm -f `cat packlist-$(NAME)`

.PHONY: clean
clean:
	rm -f *.cmi *.cmo *.cma *.cmx *.o *.a *.cmxa *.so

.PHONY: CLEAN
CLEAN: clean
	$(MAKE) -C doc CLEAN

.PHONY: distclean
distclean: clean
	rm -f *~ depend depend.pkg 
	$(MAKE) -C doc 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

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

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

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

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

.c.o:
	$(OCAMLC) -c $<

include depend
