# -*- sh -*-
#---------------------------------------------------------------------------
# ANSI C compiler
#---------------------------------------------------------------------------
CC              = @CC@
CFLAGS 		= @CFLAGS@
LDFLAGS		= @LDFLAGS@ @DYNAMIC_LINK_FLAGS@
#-----------------------------------------------------------------------------
# System library that contains functions for dynamic linking (-ldl)
#-----------------------------------------------------------------------------
DL_LIB= @DYNAMIC_LINK_LIB@

#---------------------------------------------------------------------------
# ELF specific (Linux)
#---------------------------------------------------------------------------
ELF_CC 		= @ELF_CC@
ELF_CFLAGS	= @ELF_CFLAGS@
ELF_LINK        = @ELF_LINK@
ELF_DEP_LIBS	= $(DL_LIB) -lm -lc

#---------------------------------------------------------------------------
# Set these values to ABSOLUTE path names
#---------------------------------------------------------------------------
SRCDIR = @SRCDIR@# Location of sources
OBJDIR = @OBJDIR@# Location of objects
ELFDIR = @ELFDIR@# Location of elf objects

#---------------------------------------------------------------------------
# Set for termcap support
#TCAPLIB = -ltermcap
TCAPLIB = @TERMCAP@
#---------------------------------------------------------------------------
# Directory where library is going to go when installed
#---------------------------------------------------------------------------
prefix = @prefix@
exec_prefix = @exec_prefix@
install_lib_dir = @libdir@
install_include_dir = @includedir@
install_doc_dir = $(prefix)/doc/slang/@slang_version@
DOC_FILES = ../changes.txt ../COPY* ../doc/slangdoc.html ../doc/text/*.txt
#---------------------------------------------------------------------------
# Misc commands (safe to leave these untouched)
#---------------------------------------------------------------------------
RANLIB 		= @RANLIB@
INSTALL		= @INSTALL@
INSTALL_DATA	= @INSTALL_DATA@
MKINSDIR	= ../autoconf/mkinsdir.sh
RM 		= rm -f
RM_R		= rm -rf
AR_CR 		= ar cr
RMDIR 		= rmdir
LN 		= /bin/ln -sf
CP 		= cp
MKDIR		= mkdir
@SET_MAKE@
#---------------------------------------------------------------------------
# There should be no need to change anything below here.
#---------------------------------------------------------------------------
THIS_LIB		= slang#
OTHERSTUFF 		= 
THIS_LIB_DEFINES	= -DSLANG
ELF_MAJOR_VERSION	= @slang_major_version@#
ELF_MINOR_VERSION	= @slang_minor_version@#

CONFIG_H		= config.h
ALL_CFLAGS		= $(CFLAGS) -Dunix $(THIS_LIB_DEFINES)
ALL_ELF_CFLAGS		= $(ELF_CFLAGS) -Dunix $(THIS_LIB_DEFINES)
COMPILE_CMD		= $(CC) -c $(ALL_CFLAGS)
ELFCOMPILE_CMD		= $(ELF_CC) -c $(ALL_ELF_CFLAGS)
ELF_LINK_CMD		= @ELF_LINK_CMD@

NORMAL_LIB = lib$(THIS_LIB).a
OBJDIR_NORMAL_LIB = $(OBJDIR)/$(NORMAL_LIB)

ELFLIB = lib$(THIS_LIB).so#
ELFLIB_MAJOR = $(ELFLIB).$(ELF_MAJOR_VERSION)#
ELFLIB_MAJOR_MINOR = $(ELFLIB).$(ELF_MAJOR_VERSION).$(ELF_MINOR_VERSION)#

ELFDIR_ELF_LIB = $(ELFDIR)/$(ELFLIB_MAJOR_MINOR)#

EXECLIBS = -L$(OBJDIR) -lslang -lm $(TCAPLIB) $(DL_LIB)
EXECDEPS = $(OBJDIR_NORMAL_LIB)

OFILES = @PROGRAM_OFILES@
OBJS = @PROGRAM_OBJECTS@
ELFOBJS = @PROGRAM_ELFOBJECTS@

SHELL = /bin/sh

sltoken_O_DEP = keywhash.c
slarith_O_DEP = slarith.inc
slarrfun_O_DEP = slarrfun.inc
slmisc_O_DEP = slang.h
#---------------------------------------------------------------------------
# Rules
#---------------------------------------------------------------------------
all: $(OBJDIR) $(CONFIG_H) $(OBJDIR_NORMAL_LIB) $(OTHERSTUFF)
elf: $(ELFDIR) $(CONFIG_H) $(ELFDIR_ELF_LIB)

$(OBJDIR_NORMAL_LIB): $(OBJDIR) $(CONFIG_H) $(OBJS)
	-$(RM) $(OBJDIR_NORMAL_LIB)
	cd $(OBJDIR); $(AR_CR) $(NORMAL_LIB) $(OFILES)
	$(RANLIB) $(OBJDIR_NORMAL_LIB)
	@echo ""
	@echo $(NORMAL_LIB) created in $(OBJDIR)

$(ELFDIR_ELF_LIB): $(ELFDIR) $(CONFIG_H) $(ELFOBJS)
	-$(RM) $(ELFDIR_ELF_LIB)
	cd $(ELFDIR); $(ELF_LINK_CMD) -o $(ELFLIB_MAJOR_MINOR) $(OFILES) $(ELF_DEP_LIBS)
	cd $(ELFDIR); $(RM) $(ELFLIB); $(LN) $(ELFLIB_MAJOR_MINOR) $(ELFLIB)
	@echo ""
	@echo $(ELFLIB_MAJOR_MINOR) created in $(ELFDIR).
	@echo The link $(ELFLIB) to $(ELFLIB_MAJOR_MINOR) was also created.
	@echo ""
$(OBJDIR) :
	-$(MKDIR) $(OBJDIR)
$(ELFDIR) :
	-$(MKDIR) $(ELFDIR)
$(CONFIG_H) : sysconf.h
	-$(CP) sysconf.h $(CONFIG_H)

#---------------------------------------------------------------------------
# Intallation rules
#---------------------------------------------------------------------------
install: install_basic_lib install_docs
install_basic_lib: $(install_lib_dir) $(install_include_dir) $(OBJDIR_NORMAL_LIB)
	@echo installing $(OBJDIR_NORMAL_LIB) in $(install_lib_dir)
	$(INSTALL_DATA) $(OBJDIR_NORMAL_LIB) $(install_lib_dir)
	$(RANLIB) $(install_lib_dir)/$(NORMAL_LIB)
	@echo installing slang.h and slcurses.h in $(install_include_dir)
	$(INSTALL_DATA) $(SRCDIR)/slang.h $(install_include_dir)
	$(INSTALL_DATA) $(SRCDIR)/slcurses.h $(install_include_dir)
install_docs: $(install_doc_dir)
	$(INSTALL_DATA) $(DOC_FILES) $(install_doc_dir)/
	@for i in $(DOC_FILES); \
	do \
		echo $(INSTALL_DATA) $$i $(install_doc_dir)/; \
		$(INSTALL_DATA) $$i $(install_doc_dir); \
	done
$(install_doc_dir):
	$(MKINSDIR) $(install_doc_dir)
$(install_include_dir):
	$(MKINSDIR) $(install_include_dir)
$(install_lib_dir):
	$(MKINSDIR) $(install_lib_dir)
install-elf: elf install
	-$(RM) $(install_lib_dir)/$(ELFLIB)
	-$(RM) $(install_lib_dir)/$(ELFLIB_MAJOR)
	@echo installing $(ELFLIB_MAJOR_MINOR) in $(install_lib_dir)
	$(INSTALL_DATA) $(ELFDIR_ELF_LIB) $(install_lib_dir)
	@echo creating symbolic links to $(ELFLIB_MAJOR_MINOR)
	-cd $(install_lib_dir); $(LN) $(ELFLIB_MAJOR_MINOR) $(ELFLIB)
	@echo ""
	@echo $(ELFLIB_MAJOR_MINOR) created in $(install_lib_dir).
	@echo The link $(ELFLIB) to $(ELFLIB_MAJOR_MINOR) was also created.
	@echo ""
install-links:
	-$(RM) $(install_lib_dir)/$(ELFLIB)
	-$(RM) $(install_lib_dir)/$(ELFLIB_MAJOR)
	cd $(install_lib_dir); $(LN) $(ELFLIB_MAJOR_MINOR) $(ELFLIB_MAJOR)
	cd $(install_lib_dir); $(LN) $(ELFLIB_MAJOR) $(ELFLIB)
	@echo
	@echo A link $(ELFLIB_MAJOR) to $(ELFLIB_MAJOR_MINOR) was created.
	@echo A link $(ELFLIB) to $(ELFLIB_MAJOR) was created.
	@echo ""
#---------------------------------------------------------------------------
# Tests
#---------------------------------------------------------------------------
runtests:
	cd test; $(MAKE) CC="$(CC)" CFLAGS="$(CFLAGS)"
#---------------------------------------------------------------------------
# Housekeeping
#---------------------------------------------------------------------------
clean:
	-$(RM) $(OBJDIR)/* *~ $(ELFDIR)/*
distclean: clean
	-$(RM_R) $(OBJDIR) $(ELFDIR) Makefile sysconf.h $(CONFIG_H)

# The symlinks target is for my own private use.  It simply creates the object
# directory as a symbolic link to a local disk instead of an NFS mounted one.
symlinks:
	-/bin/rm -f $(ARCH)objs
	mkdir -p $(HOME)/sys/$(ARCH)/objs/slang/src
	ln -s $(HOME)/sys/$(ARCH)/objs/slang/src $(ARCH)objs
	echo "Also try: make elfsymlinks"
elfsymlinks: symlinks
	-/bin/rm -f elf$(ARCH)objs
	mkdir -p $(HOME)/sys/$(ARCH)/objs/slang/src/elf
	ln -s $(HOME)/sys/$(ARCH)/objs/slang/src/elf elf$(ARCH)objs
#---------------------------------------------------------------------------
# Object Rules : These are created from the configure script (hopefully)
#---------------------------------------------------------------------------
