#-*-sh-*-
CC		= @CC@
CFLAGS		= @CFLAGS@ @SLANG_DLL_CFLAGS@
LDFLAGS		= @LDFLAGS@
DLINK_FLAGS	= @DYNAMIC_LINK_FLAGS@
CONFIG_DIR	= @CONFIG_DIR@
OBJDIR		= $(ARCH)objs
SOBJDIR		= static_objs
SRCDIR		= $(CONFIG_DIR)/slsh
#---------------------------------------------------------------------------
# Installation location of the slang library
#---------------------------------------------------------------------------
prefix 		= @prefix@
exec_prefix 	= @exec_prefix@
datarootdir	= @datarootdir@
INST_LIB_DIR	= @libdir@
SLANG_INST_INC	= -I@includedir@
SLANG_INST_LIB	= -L$(INST_LIB_DIR)
#---------------------------------------------------------------------------
SLANG_SRCINC	= -I@SRCDIR@
SLANG_ELFLIB	= -L@ELFDIR@#  for dynamically linked
SLANG_OBJLIB	= -L@OBJDIR@#  for statically linked
#---------------------------------------------------------------------------
DYNAMIC_LIBS	= @TERMCAP@ @DYNAMIC_LINK_LIB@ @LIBS@ @M_LIB@
STATIC_LIBS	= @TERMCAP@ @LIBS@ @M_LIB@
RPATH		= @RPATH@
#----------------------------------------------------------------------------
INSTALL		= @INSTALL@
INSTALL_DATA	= @INSTALL_DATA@
MKINSDIR	= $(CONFIG_DIR)/autoconf/mkinsdir.sh
#----------------------------------------------------------------------------
# Where system-wide slsh.rc, scripts, and library files reside
#----------------------------------------------------------------------------
SLSH_CONF_DIR	= @sysconfdir@
SLSH_LIB_DIR	= $(datarootdir)/slsh
SLSH_HELP_DIR	= $(SLSH_LIB_DIR)/help
SLSH_DOC_DIR	= $(datarootdir)/doc/slsh
SLSH_LOCALLIB_DIR = $(SLSH_LIB_DIR)/local-packages
SLSH_SCRIPT_DIR	= $(SLSH_LIB_DIR)/scripts
SCRIPTS		= jpegsize lsrpm sldb svnsh slprof slstkchk
#---------------------------------------------------------------------------
# Hard-coded paths may be configured during run-time via the SLSH_CONF_DIR
# and SLSH_PATH env variables.  The names of those variables may be
# changed here.  The SLSH_LIB_DIR variable has been deprecated.
#
SLSH_CONF_DIR_ENV = "SLSH_CONF_DIR"
SLSH_PATH_ENV	  = "SLSH_PATH"
SLSH_LIB_DIR_ENV  = "SLSH_LIB_DIR"
#---------------------------------------------------------------------------
# If you want GNU readline instead of slang's readline, then enable it here
#---------------------------------------------------------------------------
GNU_READLINE	= @GNU_READLINE@
READLINE_LIB	= @LIB_READLINE@
#---------------------------------------------------------------------------
# DESTDIR is designed to facilitate making packages.  Normally it is empty
#---------------------------------------------------------------------------
#DESTDIR		=
BIN_DIR		= @bindir@
MAN_DIR		= @mandir@/man1
DEST_LIB_DIR	= -L$(DESTDIR)@libdir@
DEST_BIN_DIR	= $(DESTDIR)$(BIN_DIR)
DEST_MAN_DIR	= $(DESTDIR)$(MAN_DIR)
DEST_SLSH_CONF_DIR = $(DESTDIR)$(SLSH_CONF_DIR)
DEST_SLSH_LIB_DIR  = $(DESTDIR)$(SLSH_LIB_DIR)
DEST_SLSH_HELP_DIR = $(DESTDIR)$(SLSH_HELP_DIR)
DEST_SLSH_LOCALLIB_DIR = $(DESTDIR)$(SLSH_LOCALLIB_DIR)
DEST_SLSH_SCRIPT_DIR = $(DESTDIR)$(SLSH_SCRIPT_DIR)
DEST_SLSH_DOC_DIR= $(DESTDIR)$(SLSH_DOC_DIR)

#----------------------------------------------------------------------------
# These should be empty for production code
#----------------------------------------------------------------------------
SLSYSWRAP_DEF = @DSLSYSWRAP@
SLSYSWRAP_LIB = @LIB_SLSYSWRAP@
#----------------------------------------------------------------------------
# End of user configuration
#----------------------------------------------------------------------------
@SET_MAKE@
SHELL = /bin/sh
INST_LIBS = $(DEST_LIB_DIR) $(RPATH) -lslang $(READLINE_LIB) $(DYNAMIC_LIBS)
DEFS = -DSLSH_CONF_DIR='"$(SLSH_CONF_DIR)"' -DSLSH_PATH='"$(SLSH_LIB_DIR)"' \
 -DSLSH_CONF_DIR_ENV='$(SLSH_CONF_DIR_ENV)' -DSLSH_LIB_DIR_ENV='$(SLSH_LIB_DIR_ENV)' \
 -DSLSH_PATH_ENV='$(SLSH_PATH_ENV)' $(SLSYSWRAP_DEF)
SDEFS = $(DEFS) -DSLSH_STATIC
SRC_LIBS = $(SLANG_ELFLIB) -lslang $(SLSYSWRAP_LIB) $(READLINE_LIB) $(DYNAMIC_LIBS)
STATIC_SRC_LIBS = $(SLANG_OBJLIB) -lslang $(SLSYSWRAP_LIB) $(READLINE_LIB) $(STATIC_LIBS)
OBJDIR_TSTAMP = $(OBJDIR)/tstamp
SOBJDIR_TSTAMP = $(SOBJDIR)/tstamp
#
all: $(OBJDIR)/slsh_exe
slsh: $(OBJDIR)/slsh
$(OBJDIR)/slsh_exe: $(OBJDIR)/slsh.o $(OBJDIR)/readline.o
	$(CC) $(CFLAGS) $(OBJDIR)/slsh.o $(OBJDIR)/readline.o -o $(OBJDIR)/slsh_exe $(LDFLAGS) $(DLINK_FLAGS) $(SRC_LIBS)
$(OBJDIR)/slsh: $(OBJDIR)/slsh.o $(OBJDIR)/readline.o
	$(CC) $(CFLAGS) $(OBJDIR)/slsh.o $(OBJDIR)/readline.o -o $(OBJDIR)/slsh $(LDFLAGS) $(DLINK_FLAGS) $(INST_LIBS)
$(OBJDIR)/slsh.o: $(OBJDIR_TSTAMP) slsh.c slsh.h config.h Makefile
	cd $(OBJDIR) && $(CC) $(SLANG_SRCINC) $(CFLAGS) -c $(DEFS) $(SRCDIR)/slsh.c
$(OBJDIR)/readline.o: $(OBJDIR_TSTAMP) readline.c slsh.h config.h Makefile
	cd $(OBJDIR) && $(CC) $(SLANG_SRCINC) $(CFLAGS) -c $(DEFS) -DUSE_GNU_READLINE=$(GNU_READLINE) $(SRCDIR)/readline.c
$(OBJDIR_TSTAMP):
	-$(MKINSDIR) $(OBJDIR)
	touch $(OBJDIR_TSTAMP)
#
static: $(SOBJDIR)/slsh
$(SOBJDIR)/slsh: $(SOBJDIR)/slsh.o $(SOBJDIR)/readline.o
	$(CC) $(CFLAGS) $(SOBJDIR)/slsh.o $(SOBJDIR)/readline.o -o $(SOBJDIR)/slsh $(LDFLAGS) $(STATIC_SRC_LIBS)
$(SOBJDIR)/slsh.o: $(SOBJDIR_TSTAMP) slsh.c slsh.h config.h Makefile
	cd $(SOBJDIR) && $(CC) $(SLANG_SRCINC) $(CFLAGS) -c $(SDEFS) $(SRCDIR)/slsh.c
$(SOBJDIR)/readline.o: $(SOBJDIR_TSTAMP) readline.c slsh.h config.h Makefile
	cd $(SOBJDIR) && $(CC) $(SLANG_SRCINC) $(CFLAGS) -c $(SDEFS) -DUSE_GNU_READLINE=$(GNU_READLINE) $(SRCDIR)/readline.c
$(SOBJDIR_TSTAMP):
	-$(MKINSDIR) $(SOBJDIR)
	touch $(SOBJDIR_TSTAMP)
#
config.h: ../src/config.h
	cp ../src/config.h .
install_directories:
	$(MKINSDIR) $(DEST_BIN_DIR)
	$(MKINSDIR) $(DEST_MAN_DIR)
	$(MKINSDIR) $(DEST_SLSH_CONF_DIR)
	$(MKINSDIR) $(DEST_SLSH_LIB_DIR)
	$(MKINSDIR) $(DEST_SLSH_LIB_DIR)/rline
	$(MKINSDIR) $(DEST_SLSH_HELP_DIR)
	$(MKINSDIR) $(DEST_SLSH_LOCALLIB_DIR)
	$(MKINSDIR) $(DEST_SLSH_SCRIPT_DIR)
	$(MKINSDIR) $(DEST_SLSH_DOC_DIR)/html
install_lib_files:
	@for X in lib/*.sl; \
	do \
		echo $(INSTALL_DATA) $$X $(DEST_SLSH_LIB_DIR); \
		$(INSTALL_DATA) $$X $(DEST_SLSH_LIB_DIR); \
		if [ "$$?" != "0" ]; then \
		  exit 1; \
		fi; \
	done
install_rline_files:
	@for X in lib/rline/*.sl lib/rline/slrline.rc; \
	do \
		echo $(INSTALL_DATA) $$X $(DEST_SLSH_LIB_DIR)/rline/; \
		$(INSTALL_DATA) $$X $(DEST_SLSH_LIB_DIR)/rline/; \
		if [ "$$?" != "0" ]; then \
		  exit 1; \
		fi; \
	done
install_scripts:
	@for X in $(SCRIPTS); \
	do \
		echo $(INSTALL) scripts/$$X $(DEST_SLSH_SCRIPT_DIR); \
		$(INSTALL) scripts/$$X $(DEST_SLSH_SCRIPT_DIR); \
		if [ "$$?" != "0" ]; then \
		  exit 1; \
		fi; \
	done
install_help:
	@for X in lib/help/*.hlp; \
	do \
		echo $(INSTALL_DATA) $$X $(DEST_SLSH_HELP_DIR); \
		$(INSTALL_DATA) $$X $(DEST_SLSH_HELP_DIR); \
		if [ "$$?" != "0" ]; then \
		  exit 1; \
		fi; \
	done
install_docs:
	@for X in doc/html/*.html; \
	do \
		echo $(INSTALL_DATA) $$X $(DEST_SLSH_DOC_DIR)/html/; \
		$(INSTALL_DATA) $$X $(DEST_SLSH_DOC_DIR)/html/; \
		if [ "$$?" != "0" ]; then \
		  exit 1; \
		fi; \
	done
	$(INSTALL_DATA) doc/man/slsh.1 $(DEST_MAN_DIR)/
#
install_slsh:
	$(INSTALL) $(OBJDIR)/slsh $(DEST_BIN_DIR)/
install_static_slsh:
	$(INSTALL) $(SOBJDIR)/slsh $(DEST_BIN_DIR)/
install_slshrc:
	$(INSTALL_DATA) etc/slsh.rc $(DEST_SLSH_CONF_DIR)/
	echo 'prepend_to_slang_load_path("$(SLSH_LOCALLIB_DIR)");' >> $(DEST_SLSH_CONF_DIR)/slsh.rc
install_common: install_directories install_lib_files install_rline_files install_scripts install_help install_docs
#
install: slsh install_common install_slsh install_slshrc
install-static: static install_common install_static_slsh install_slshrc
#---------------------------------------------------------------------------
runtests:
	cd lib/test && ./runtests.sh test_*.sl
#---------------------------------------------------------------------------
# Housekeeping
#---------------------------------------------------------------------------
clean:
	-/bin/rm -f $(OBJDIR_TSTAMP) $(SOBJDIR_TSTAMP)
	-/bin/rm -f *~ $(OBJDIR)/slsh.o $(OBJDIR)/readline.o $(OBJDIR)/slsh $(OBJDIR)/slsh_exe scripts/*~ lib/*~
	-/bin/rm -f $(SOBJDIR)/slsh.o $(SOBJDIR)/readline.o $(SOBJDIR)/slsh $(SOBJDIR)/slsh_exe
distclean: clean
	-/bin/rm -f Makefile config.h
#
.PHONY: all clean distclean symlinks slsh install install_directories \
  install_lib_files install_rline_files install_scripts install_help \
  install_docs install-static static install_common \
  install_slsh install_static_slsh install_slshrc

