all:
include ../subdir-inc.make

CPPFLAGS += -I$(TOP_SRCDIR)/src# workaround for in-tree sqlite3.h
CPPFLAGS+=-DCWAL_ENABLE_TRACE=0

LIBFOSSIL.LDFLAGS := -L.. -lfossil
ifeq (1,1)
  # In static build, we need some extra flags...
  LIBFOSSIL.LDFLAGS += $(LDFLAGS_MODULE_LOADER) -lpthread -lz
endif

ifeq (,$(strip $(filter distclean clean,$(MAKECMDGOALS))))
$(LIBFOSSIL.LDFLAGS):
	$(MAKE) -C ..
endif

S2_CLIENT_LDFLAGS := $(LIBFOSSIL.LDFLAGS) $(LDFLAGS_MODULE_LOADER)

########################################################################
# INC_SEARCH: $(call)able function. $(1) should be the the name of
# a C header file to search for under $(INCLUDES_PATH).
INCLUDES_PATH ?= $(HOME)/include /usr/local/include /usr/include
define INC_SEARCH
$(call ShakeNMake.CALL.FIND_FILE,$(1),$(INCLUDES_PATH))
endef

#$(error ltdl=$(HAVE_LT_DLOPEN) dl=$(HAVE_DLOPEN) mods=$(ENABLE_MODULES))
########################################################################
# Bins and libs...

########################################################################
# Check for readline
ENABLE_READLINE ?= 1
LDFLAGS_READLINE :=
ifeq (1,$(ENABLE_READLINE))
  READLINE_H := $(call INC_SEARCH,readline/readline.h)
  ifneq (,$(READLINE_H))
#    READLINE_IMPL := readline
    $(info Enabling GNU Readline)
    LDFLAGS_READLINE := -lreadline
  else
    $(info Not enabling GNU Readline)
    ENABLE_READLINE := 0
  endif
endif
# /readline
########################################################################

########################################
# Linenoise (terminal input lib)...
LN_OBJ :=
ENABLE_LINENOISE := 0
ifeq (1,$(ENABLE_READLINE))
  $(info Not enabling linenoise)
else
  LN_DIR := Xlinenoise
  ifneq (,$(wildcard $(LN_DIR)/*.c))
    ENABLE_LINENOISE := 1
    $(info Enabling linenoise)
  else
    $(info Not enabling linenoise)
    ENABLE_LINENOISE := 0
  endif
  ifeq (1,$(ENABLE_LINENOISE))
    LN_OBJ := $(LN_DIR)/linenoise.o $(LN_DIR)/utf8.o
    CLEAN_FILES += $(LN_OBJ)
    $(LN_DIR)/utf8.o: CPPFLAGS+=-std=c99 -DUSE_UTF8=1
    $(LN_DIR)/linenoise.o: CPPFLAGS+=-std=c99 -DUSE_UTF8=1 -D_BSD_SOURCE
  else
    LN_OBJ :=
  endif
endif
# /linenoise
########################################

########################################
# s2sh a.k.a. f-s2sh
shell.o: CPPFLAGS+=-DS2_SHELL_EXTEND
shell.o: CPPFLAGS+=-DS2_SHELL_ENABLE_LINENOISE=$(ENABLE_LINENOISE)
shell.o: CPPFLAGS+=-DS2_SHELL_ENABLE_READLINE=$(ENABLE_READLINE)
shell.o shell_extend.o: CPPFLAGS+=-DS2_AMALGAMATION_BUILD

f-s2sh.BIN.OBJECTS := shell.o shell_extend.o $(LN_OBJ) s2_amalgamation.o
f-s2sh.BIN.LDFLAGS := $(S2_CLIENT_LDFLAGS) $(LDFLAGS_READLINE)
$(eval $(call ShakeNMake.CALL.RULES.BINS,f-s2sh))
all: $(f-s2sh.BIN)
# end bins and libs
########################################################################

########################################
# Set up file-specific CPPFLAGS/LDFLAGS.
ifeq (1,$(ENABLE_MODULES))
  f-s2sh.BIN.LDFLAGS += -export-dynamic
  s2_amalgamation.o: CPPFLAGS+=-DS2_ENABLE_MODULES=1
  f-s2sh.BIN.LDFLAGS += $(DL_LDFLAGS)
  ifeq (1,$(HAVE_DLOPEN)) # use libdl
    s2_amalgamation.o: CPPFLAGS+=-DS2_HAVE_DLOPEN=1
  else # use libltdl
    s2_amalgamation.o: CPPFLAGS+=-DS2_HAVE_LTDLOPEN=1
  endif
endif
# basic module setup

# cgimod...
ifeq (1,$(ENABLE_MODULES))
  cgimod.DLL.OBJECTS := cgimod.o s2_cgi.o
  cgimod.o s2_cgi.o: s2_amalgamation.h
#  cgimod.DLL.LDFLAGS += 
  cgimod.o s2_cgi.o: CPPFLAGS+=-DS2_AMALGAMATION_BUILD
  $(eval $(call ShakeNMake.CALL.RULES.DLLS,cgimod))
  all: $(cgimod.DLL)
endif
# /cgimod

# end modules
########################################################################

########################################################################
# Unit test stuff...
UNIT_SCRIPT_LIST := $(sort $(subst ./,,$(wildcard unit/???-???-*.s2 unit2/???-*.s2)))
$(UNIT_SCRIPT_LIST):
S2SH.SHELL.FLAGS ?= --a
# -w
UNIT_RUN_CMD = ./$(f-s2sh.BIN) $(S2SH.SHELL.FLAGS)
UNIT_MEGA.S2 := UNIT.s2
UNIT_MEGA2.S2 := UNIT-import.s2
UNIT_GENERATED := $(UNIT_MEGA.S2) $(UNIT_MEGA2.S2)
CLEAN_FILES += $(UNIT_GENERATED)
$(UNIT_MEGA.S2): $(UNIT_SCRIPT_LIST) Makefile
	@echo "Generating $@..."
	@{ \
		false && echo "const INTERN_THESE=['object','array','integer','double','string','function','bool'];"; \
		for i in $(UNIT_SCRIPT_LIST); do \
			echo "scope {/* begin file: $$i */"; \
			cat $$i; \
			echo "/* end file: $$i */;;}"; \
			echo ""; \
		done; \
	} > $@
$(UNIT_MEGA2.S2): $(UNIT_SCRIPT_LIST) Makefile
	@echo "Generating $@..."
	@{ \
		false && echo "const INTERN_THESE=['object','array','integer','double','string','function','bool'];"; \
		echo "const import=s2.import;"; \
		for i in $(UNIT_SCRIPT_LIST); do \
			echo "import('$$i');"; \
		done; \
	} > $@

.PHONY: unit
unit: $(f-s2sh.BIN) $(UNIT_GENERATED)
	@for i in $(UNIT_SCRIPT_LIST) $(UNIT_GENERATED); do \
		cmd="$(UNIT_RUN_CMD) -f $$i"; \
		echo "****************************** Script [$$i]"; \
		echo $$cmd; $$cmd || exit $$?; \
		echo "****************************** Done [$$i]"; \
	done
	@echo "Done running through unit test scripts."
unit-r: S2SH.SHELL.FLAGS:=-A -A -r -v
unit-r: unit
include vg.make
