SUBDIRS = src macro help etc

all:
	@if [ ! -f etc/config ]; then make config; fi
	@for i in $(SUBDIRS); do (cd $$i; make $@) done

recompile:
	@if [ ! -f etc/config ]; then make config; fi
	cd src; make $@

install:
	@bindir=$$HOME/bin; \
	echo "Installation directory for executables [$$bindir]: " |\
	awk '{printf("%s",$$0)}'; read answer; \
	if [ "$$answer" ]; then bindir=`etc/abspath $$answer`; fi; \
	libdir=`pwd | sed -e 's@^/tmp_mnt@@'`; \
	libdir=$$HOME/lib/`basename $$libdir`; \
	echo "Installation directory for other files [$$libdir]: " |\
	awk '{printf("%s",$$0)}'; read answer; \
	if [ "$$answer" ]; then libdir=`etc/abspath $$answer`; fi; \
	if [ -d $$libdir ]; then \
	  echo "$$libdir exists. Remove and recreate [n]? " |\
	  awk '{printf("%s",$$0)}'; read answer; \
	  if [ "$$answer" != "y" ]; then \
	    echo "Installation not started."; exit; \
	  fi; \
	fi; \
	rm -rf $$libdir; \
	exec make install-files BINDIR=$$bindir LIBDIR=$$libdir

install-files: all
	@echo "$(BINDIR),$(LIBDIR)" >> etc/installed
	echo; echo "Installing COFIMA files in $(LIBDIR):"; echo
	cd etc; ../etc/install $(LIBDIR)/etc identify keep start.in config establish 
	cd etc; ../etc/install $(LIBDIR) setup
	etc/install $(LIBDIR) README LICENSE
	cd src/cofima; ../../etc/install $(LIBDIR)/src/cofima cofimaexe.*; \
	cd $(LIBDIR)/src/cofima; \
	for i in cofimaexe.*; do \
	  ln -s $$i `echo $$i | sed 's/^cofima/difima/'`; \
	  ln -s $$i `echo $$i | sed 's/^cofima/ancoma/'`; \
	done
	etc/install $(LIBDIR) help macro
	cd $(LIBDIR)/help; make clean
	cd $(LIBDIR)/macro; make clean
	rm -f `find $(LIBDIR) -name Makefile -print`
	
	@echo; echo "Installing executables in $(BINDIR):"; echo
	basedir=`pwd | sed -e 's@^/tmp_mnt@@'`; \
	for f in cofima/cofima cofima/difima cofima/ancoma; do \
	  (cd `dirname $(LIBDIR)/src/$$f`; \
	  $$basedir/etc/establish `basename $$f` $(BINDIR); \
	  $$basedir/etc/establish `basename $$f` $(LIBDIR)); \
	done
	
	rm -f $(LIBDIR)/etc/keep $(LIBDIR)/etc/config
	
uninstall:
	@etc/uninstall

clean:
	touch etc/config
	@for i in $(SUBDIRS); do (cd $$i; make $@) done
	etc/keep $(SUBDIRS) README LICENSE Makefile
	
config:
	@etc/configure cofima
