# -*- sh -*-

#---------------------------------------------------------------------------
# List of modules and associated .sl files to install
#---------------------------------------------------------------------------
MODULES = sqlite-module.so
SL_FILES = sqlite.sl
HLP_FILES = ../doc/help/sqlite.hlp
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

#---------------------------------------------------------------------------
# C Compiler to create a shared library
#---------------------------------------------------------------------------
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
#---------------------------------------------------------------------------
SQLITE3_INC	= @SQLITE3_INC@
SQLITE3_LIB	= @SQLITE3_LIB@ -lsqlite3
X_XTRA_LIBS	= @X_EXTRA_LIBS@
MODULE_LIBS	= $(SQLITE3_LIB) # $(X_LIBS) $(X_XTRA_LIBS)
RPATH		= @RPATH@

#---------------------------------------------------------------------------
# Misc Programs required for installation
#---------------------------------------------------------------------------
INSTALL		= @INSTALL@
INSTALL_DATA	= @INSTALL_DATA@
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)
#---------------------------------------------------------------------------
UPDATE_VERSION_SCRIPT = $(HOME)/bin/update_changes_version
#---------------------------------------------------------------------------
LIBS = $(SLANG_LIB) $(MODULE_LIBS) $(RPATH) $(DL_LIB) -lm
INCS = $(SLANG_INC) $(SQLITE3_INC)

all: $(MODULES)

#---------------------------------------------------------------------------
# Put Rules to create the modules here
#---------------------------------------------------------------------------
sqlite-module.so: sqlite-module.c version.h
	$(CC_SHARED) $(INCS) $(LDFLAGS) sqlite-module.c -o sqlite-module.so $(LIBS)
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: install-directories-stamp
	$(MKINSDIR) $(DEST_MODULE_INSTALL_DIR)
	$(MKINSDIR) $(DEST_SL_FILES_INSTALL_DIR)
	$(MKINSDIR) $(DEST_HLP_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_DATA) $$X $$YDEST; \
		$(INSTALL_DATA) $$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: all install_modules install_slfiles install_hlpfiles

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