# -*- 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
#

SL_FILES = gsl.sl gslcore.sl gslsf.sl gslconst.sl gslinterp.sl gslrand.sl \
   gslcdf.sl gslfft.sl gslmatrix.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@
#
COMPILE		= $(CC) -c $(CFLAGS) $(INCS)

#---------------------------------------------------------------------------
# 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)

#---------------------------------------------------------------------------
LIBS = $(SLANG_LIB) $(MODULE_LIBS) $(RPATH) $(DL_LIB) -lm
INCS = $(SLANG_INC) $(GSL_INC) 
DEPS = config.h

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) $(INCS) gslvers.c -o gslvers $(LIBS)
#---------------------------------------------------------------------------
# Put Rules to create the modules here
#---------------------------------------------------------------------------
gsl-module.so: $(OFILES)
	$(CC_SHARED) $(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
#---------------------------------------------------------------------------
# Regression tests
#---------------------------------------------------------------------------
test:
	@for X in tests/test_*.sl; \
	do \
		slsh $$X; \
	done
#---------------------------------------------------------------------------
# Installation Rules
#---------------------------------------------------------------------------
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:
	@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:
	@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:
	@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:
	@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_directories install_modules install_slfiles \
 install_hlpfiles install_docfiles

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