# -*- sh -*-----------------------------------------------------------------
# List of modules and associated .sl files to install
#---------------------------------------------------------------------------
MODULES = gsl-module.so
OFILES = gsl-module.o gslinterp-module.o gslrand-module.o \
   gslfft-module.o gslmatrix-module.o gslcdf-module.o gslconst-module.o \
   gslsf-module.o gsldwt-module.o gslinteg-module.o
#

SL_FILES = gsl.sl gslcore.sl gslsf.sl gslconst.sl gslinterp.sl gslrand.sl \
   gslcdf.sl gslfft.sl gslmatrix.sl gsldwt.sl gslinteg.sl
HLP_FILES = ../doc/help/slgsl.hlp
DOC_FILES = ../doc/index.html ../COPYRIGHT ../ChangeLog
MODULE_VERSION	= `./mkversion.sh`

#---------------------------------------------------------------------------
# Installation Directories
#---------------------------------------------------------------------------
prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
MODULE_INSTALL_DIR = @MODULE_INSTALL_DIR@
SL_FILES_INSTALL_DIR = @SL_FILES_INSTALL_DIR@
HLP_FILES_INSTALL_DIR = $(SL_FILES_INSTALL_DIR)/help
DOC_FILES_INSTALL_DIR = $(datarootdir)/doc/slang-gsl

#---------------------------------------------------------------------------
# C Compiler to create a shared library
#---------------------------------------------------------------------------
CC		= @CC@
CFLAGS		= @CFLAGS@ @SLANG_DLL_CFLAGS@
LDFLAGS		= @LDFLAGS@
CC_SHARED 	= @CC_SHARED@

#---------------------------------------------------------------------------
# Location of the S-Lang library and its include file
#---------------------------------------------------------------------------
SLANG_INC	= @SLANG_INC@
SLANG_LIB	= @SLANG_LIB@ -lslang

#---------------------------------------------------------------------------
# Additional Libraries required by the module
#---------------------------------------------------------------------------
GSL_INC	= @GSL_INC@
GSL_LIB	= @GSL_LIB@ -lgsl -lgslcblas
MODULE_LIBS = $(GSL_LIB)
RPATH		= @RPATH@

#---------------------------------------------------------------------------
# Misc Programs required for installation
#---------------------------------------------------------------------------
INSTALL		= @INSTALL@
INSTALL_DATA	= @INSTALL_DATA@
INSTALL_MODULE	= @INSTALL_MODULE@
MKINSDIR = ../autoconf/mkinsdir.sh
RM		= rm -f
LN		= ln -s

#---------------------------------------------------------------------------
# DESTDIR is designed to facilitate making packages.  Normally it is empty
#---------------------------------------------------------------------------
DESTDIR =
DEST_MODULE_INSTALL_DIR = $(DESTDIR)$(MODULE_INSTALL_DIR)
DEST_SL_FILES_INSTALL_DIR = $(DESTDIR)$(SL_FILES_INSTALL_DIR)
DEST_HLP_FILES_INSTALL_DIR = $(DESTDIR)$(HLP_FILES_INSTALL_DIR)
DEST_DOC_FILES_INSTALL_DIR = $(DESTDIR)$(DOC_FILES_INSTALL_DIR)

#---------------------------------------------------------------------------
UPDATE_VERSION_SCRIPT = $(HOME)/bin/update_changes_version
#---------------------------------------------------------------------------
LIBS = $(SLANG_LIB) $(MODULE_LIBS) $(RPATH) $(DL_LIB) -lm
INCS = $(SLANG_INC) $(GSL_INC) 
DEPS = config.h version.h slgsl.h
#
COMPILE		= $(CC_SHARED) -c $(INCS) 
#
all: $(MODULES)

config.h: sysconf.h gslvers.out
	cp sysconf.h config.h
	cat gslvers.out >> config.h
gslvers.out: gslvers
	./gslvers > gslvers.out
gslvers: gslvers.c
	$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)  $(INCS) gslvers.c -o gslvers $(LIBS)
#---------------------------------------------------------------------------
# Put Rules to create the modules here
#---------------------------------------------------------------------------
gsl-module.so: $(OFILES)
	$(CC_SHARED) $(LDFLAGS) $(OFILES) -o gsl-module.so $(LIBS)
gsl-module.o: gsl-module.c $(DEPS)
	$(COMPILE) gsl-module.c
gslconst-module.o: gslconst-module.c $(DEPS)
	$(COMPILE) gslconst-module.c
gslsf-module.o: gslsf-module.c $(DEPS)
	$(COMPILE) gslsf-module.c
gslinterp-module.o: gslinterp-module.c $(DEPS)
	$(COMPILE) gslinterp-module.c
gslrand-module.o: gslrand-module.c $(DEPS)
	$(COMPILE) gslrand-module.c
gslcdf-module.o: gslcdf-module.c $(DEPS)
	$(COMPILE) gslcdf-module.c
gslfft-module.o: gslfft-module.c $(DEPS)
	$(COMPILE) gslfft-module.c
gslmatrix-module.o: gslmatrix-module.c $(DEPS)
	$(COMPILE) gslmatrix-module.c
gsldwt-module.o: gsldwt-module.c $(DEPS)
	$(COMPILE) gsldwt-module.c
gslinteg-module.o: gslinteg-module.c $(DEPS)
	$(COMPILE) gslinteg-module.c
version.h: ../changes.txt
	if [ -x $(UPDATE_VERSION_SCRIPT) ]; then \
	  $(UPDATE_VERSION_SCRIPT) ../changes.txt ./version.h; \
	fi
#---------------------------------------------------------------------------
# Regression tests
#---------------------------------------------------------------------------
check:
	@failed=0; \
        for X in tests/test_*.sl; \
	do \
		slsh $$X || failed=1; \
	done; \
        exit $$failed
#---------------------------------------------------------------------------
# Installation Rules
#---------------------------------------------------------------------------
install-directories-stamp: install_directories
	touch install-directories-stamp
install_directories:
	$(MKINSDIR) $(DEST_MODULE_INSTALL_DIR)
	$(MKINSDIR) $(DEST_SL_FILES_INSTALL_DIR)
	$(MKINSDIR) $(DEST_HLP_FILES_INSTALL_DIR)
	$(MKINSDIR) $(DEST_DOC_FILES_INSTALL_DIR)

install_modules: install-directories-stamp
	@for X in $(MODULES); \
	do \
		Y=$$X.$(MODULE_VERSION); \
		YDEST=$(DEST_MODULE_INSTALL_DIR)/$$Y; \
		echo $(INSTALL_MODULE) $$X $$YDEST; \
		$(INSTALL_MODULE) $$X $$YDEST; \
		if [ "$$?" != "0" ]; then \
		  exit 1; \
		fi; \
		$(RM) $(DEST_MODULE_INSTALL_DIR)/$$X; \
		$(LN) $$Y $(DEST_MODULE_INSTALL_DIR)/$$X; \
	done

install_slfiles: install-directories-stamp
	@for X in $(SL_FILES); \
	do \
		echo $(INSTALL_DATA) $$X $(DEST_SL_FILES_INSTALL_DIR); \
		$(INSTALL_DATA) $$X $(DEST_SL_FILES_INSTALL_DIR); \
		if [ "$$?" != "0" ]; then \
		  exit 1; \
		fi; \
	done

install_hlpfiles: install-directories-stamp
	@for X in $(HLP_FILES); \
	do \
		echo $(INSTALL_DATA) $$X $(DEST_HLP_FILES_INSTALL_DIR); \
		$(INSTALL_DATA) $$X $(DEST_HLP_FILES_INSTALL_DIR); \
		if [ "$$?" != "0" ]; then \
		  exit 1; \
		fi; \
	done
install_docfiles: install-directories-stamp
	@for X in $(DOC_FILES); \
	do \
		echo $(INSTALL_DATA) $$X $(DEST_DOC_FILES_INSTALL_DIR); \
		$(INSTALL_DATA) $$X $(DEST_DOC_FILES_INSTALL_DIR); \
		if [ "$$?" != "0" ]; then \
		  exit 1; \
		fi; \
	done

install: all install_modules install_slfiles \
 install_hlpfiles install_docfiles

clean:
	-/bin/rm -f $(MODULES) $(OFILES) *~ \#* gslvers gslvers.out config.h
	-/bin/rm -f install-directories-stamp
distclean: clean
	-/bin/rm -f sysconf.h Makefile config.h
