
ifndef INSTALL
INSTALL:=install --preserve-timestamps
endif


default: help


help:
	@echo
	@if test -d dist; then \
		echo "To install ARNL, use 'make install'."; \
		else echo "To see and build example programs, enter the 'examples' directory."; \
	fi
	@echo
	@echo "ARNL cannot be (re)compiled since most of its source code ('src' dir) "
	@echo "is proprietary and not distributed."

.PHONY: install install-base install-arnl install-sonarnl install-mogs all help examples default info


-include Makefile.dev


examples/%:
	$(MAKE) -C examples $*

examples/%Static:
	$(MAKE) -C examples $*Static

examples:
	$(MAKE) -C examples

ifndef INSTALL_DIR
INSTALL_DIR:=/usr/local/Arnl
endif
ifndef SYSTEM_ETC_DIR
SYSTEM_ETC_DIR:=/etc
endif

ifdef DESTDIR
INSTALL_DIR:=$(DESTDIR)$(INSTALL_DIR)
SYSTEM_ETC_DIR:=$(DESTDIR)$(SYSTEM_ETC_DIR)
endif


# Arguments: $(1)=lib name, $(2)=files
# e.g. $(call install-files, "ARNL", $(shell cat dist/arnl.files) )
define install-files 
	@echo        --------------------------------------------
	@echo        Installing $(1) files in $(INSTALL_DIR)...
	@echo        --------------------------------------------
	$(INSTALL) -d -m 777 $(INSTALL_DIR)
	for f in $(2); \
	do \
		fext=`echo $$f | cut -d . -f 2` ;\
		if test `basename $$f | cut -b -11` = 'proprietary'; then echo Skipping proprietary file $$f;  \
		elif test -d $$f ; then    echo Copying directory $$f;  cp --parents -R $$f $(INSTALL_DIR)/; \
		elif test -x $$f -a "`file -b $$f | cut -d ' ' -f 2-`" = "script text executable" ; then echo "Installing executable script $(INSTALL_DIR)/$$f (not stripping)..."; $(INSTALL) -D -m 777 $$f $(INSTALL_DIR)/$$f; \
		elif test "$$fext" = "so" -o "$$fext" = "a" ; then echo "Installing library $(INSTALL_DIR)/$$f (not stripping)..."; $(INSTALL) -D -m 666 $$f $(INSTALL_DIR)/$$f ;\
		elif test -x $$f ; then  echo Installing executable program  $(INSTALL_DIR)/$$f...;  $(INSTALL) -D --strip -m 777 $$f $(INSTALL_DIR)/$$f ; \
		else                     echo Installing file $(INSTALL_DIR)/$$f...;                $(INSTALL) -D -m 666 $$f $(INSTALL_DIR)/$$f ; \
		fi; \
	done
	-chmod a+x $(INSTALL_DIR)/docs/*/installdox
	-chmod o-w $(INSTALL_DIR)/Changes.txt
	-chmod o-w $(INSTALL_DIR)/README.txt
	-chmod o-w $(INSTALL_DIR)/LICENSE.txt
	-chmod o-w $(INSTALL_DIR)/version.txt
	-chmod o-w $(INSTALL_DIR)/Makefile
	@echo        --------------------------------------------
	@echo        $(1) has been installed in $(INSTALL_DIR).
	@echo        Header files are in $(INSTALL_DIR)/include. Libraries are in $(INSTALL_DIR)/lib.
	@echo        Documentation is in $(INSTALL_DIR) and $(INSTALL_DIR)/docs.
	@echo        To be able to use the libraries, you must now add $(INSTALL_DIR)/lib
	@echo        to your LD_LIBRARY_PATH environment variable, or to the 
	@echo        /etc/ld.so.conf system file then run \"ldconfig\".
	@echo        --------------------------------------------
endef

# $(1)=id
# $(2)=name
define gen_install_rule
ifeq (dist/$(1).files,$(wildcard dist/$(1).files))
$(1)_files:=$(shell cat dist/$(1).files)
install-$(1): dist/$(1).files $$($(1)_files)
	$$(call install-files,$(2),$$($(1)_files))
	if test -z "$$(DIST_INSTALL)"; then \
    if test \! -d $$(SYSTEM_ETC_DIR); then $$(INSTALL) -d -m 777 $$(SYSTEM_ETC_DIR); fi ;\
    echo '/usr/local/Arnl' > $$(SYSTEM_ETC_DIR)/Arnl ;\
	fi
else
install-$(1): ;
endif
endef


#ifeq (dist/base.files,$(wildcard dist/base.files))
#ifeq (dist/base.files.exclude,$(wildcard dist/base.files.exclude))
#BASE_FILES:=$(filter-out $(shell cat dist/base.files.exclude),$(shell cat dist/base.files))
#else
#BASE_FILES:=$(shell cat dist/base.files)
#endif
#install-base: dist/base.files $(BASE_FILES)
#	$(call install-files,"BaseArnl",$(BASE_FILES))
#	if test \! -d $(SYSTEM_ETC_DIR); then $(INSTALL) -d -m 777 $(SYSTEM_ETC_DIR); fi
#	echo '/usr/local/Arnl' > $(SYSTEM_ETC_DIR)/Arnl
#else
#install-base:
##	$(info No dist/base.files, not attempting to install BaseArnl files.)
#endif

$(eval $(call gen_install_rule,base,BaseArnl))


#ifeq (dist/arnl.files,$(wildcard dist/arnl.files))
#ARNL_FILES:=$(shell cat dist/arnl.files)
#
#install-arnl: dist/arnl.files docs/ARNL-Reference/index.html $(ARNL_FILES)
#	$(call install-files,"ARNL",$(ARNL_FILES))
#else
#install-arnl:
##	$(info No dist/arnl.files, not attempting to install Arnl files.)
#endif

$(eval $(call gen_install_rule,arnl,Arnl))

#ifeq (dist/sonarnl.files,$(wildcard dist/sonarnl.files))
#SONARNL_FILES:=$(shell cat dist/sonarnl.files)
#
#install-sonarnl: dist/sonarnl.files docs/SONARNL-Reference/index.html $(SONARNL_FILES)
#	$(call install-files,"SONARNL",$(SONARNL_FILES))
#else
#install-sonarnl:
##	$(info No dist/sonarnl.files, not attempting to install SonArnl files.)
#endif

$(eval $(call gen_install_rule,sonarnl,SonArnl))

#ifeq (dist/mogs.files,$(wildcard dist/mogs.files))
#MOGS_FILES:=$(shell cat dist/mogs.files)
#install-mogs: dist/mogs.files docs/MOGS-Reference/index.html $(MOGS_FILES)
#	$(call install-files,"MOGS",$(MOGS_FILES))
#else
#install-mogs:
##	$(info No dist/mogs.files, not attempting to install MOGS files.)
#endif

$(eval $(call gen_install_rule,mogs,MOGS))


install: install-base install-arnl install-sonarnl install-mogs

info:
	@echo base_files=$(base_files)
	@echo
	@echo arnl_files=$(arnl_files)
	@echo
	@echo sonarnl_files=$(sonarnl_files)
	@echo
	@echo mogs_files=$(mogs_files)
	@echo
	@echo DESTDIR=$(DESTDIR)
	@echo INSTALL_DIR=$(INSTALL_DIR)
	@echo SYSTEM_ETC_DIR=$(SYSTEM_ETC_DIR)



