all:
include ../subdir-inc.make
#$(error $(TOP_SRCDIR))
#CPPFLAGS += -I$(TOP_INCDIR)
CPPFLAGS += -I$(TOP_SRCDIR)/include
CPPFLAGS += -I$(TOP_SRCDIR)/src# workaround for in-tree sqlite3.h

#INCLUDES_PATH ?= $(HOME)/include /usr/local/include /usr/include

LIBFOSSIL.LDFLAGS := -L.. -lfossil

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

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

########################################################################
# Look for -ldl or -lltdl for module loading
ENABLE_MODULES ?= @FSL_ENABLE_MODULE_LOADER@
ifeq (1,$(ENABLE_MODULES))
  ifeq (1,@HAVE_LIBDL@)
    DL_IMPL := dl
    DL_LDFLAGS := -ldl
  else
    ifeq (1,@HAVE_LIBLTDL@)
      DL_IMPL := ltdl
      DL_LDFLAGS := -lltdl
    endif
  endif
endif
ifeq (,$(DL_IMPL))
  ENABLE_MODULES := 0
endif

# cgiish...
cgiish.o th1ish_cgi.o: CPPFLAGS+=-DTH1ISH_AMALGAMATION_BUILD=1
cgiish.o th1ish_cgi.o: th1ish_amalgamation.h
ifeq (0,1)# enable this block to build cgiish as a loadable module
ifeq (1,$(ENABLE_MODULES))
  cgiish.DLL.OBJECTS := cgiish.o th1ish_cgi.o
  $(eval $(call ShakeNMake.CALL.RULES.DLLS,cgiish))
  $(cgiish.DLL): $(libth1ish.LIB)
  all: $(cgiish.DLL)
endif
endif
# /cgiish

# /ENABLE_MODULES
########################################################################

########################################
# Set up file-specific CPPFLAGS/LDFLAGS.
# Set up the loadable modules flags...
ifeq (1,$(ENABLE_MODULES))
  th1ish.BIN.LDFLAGS += -export-dynamic $(DL_LDFLAGS)
  th1ish_amalgamation.o shell.o: CPPFLAGS+=-DTH1ISH_ENABLE_MODULES=1
  ifeq (dl,$(DL_IMPL)) # use libdl
    th1ish_amalgamation.o: CPPFLAGS+=-DTH1ISH_HAVE_DLOPEN=1
  else # use libltdl
    th1ish_amalgamation.o: CPPFLAGS+=-DTH1ISH_HAVE_LTDLOPEN=1
  endif
endif
# basic module setup

# Module-related setup
########################################################################

shell.o: CPPFLAGS+=-DTH1ISH_AMALGAMATION_BUILD=1
shell.o: CPPFLAGS+=-DTH1ISH_SHELL_EXTEND
shell.o: shell_extend.c
Makefile:
shell.o shell_extend.o: Makefile
th1ish.BIN.OBJECTS += shell.o th1ish_amalgamation.o cgiish.o th1ish_cgi.o shell_extend.o
th1ish.BIN.LDFLAGS += -lm $(LIBFOSSIL.LDFLAGS)# -lsqlite3 -lz
$(eval $(call ShakeNMake.CALL.RULES.BIN,th1ish))
#$(th1ish.BIN): $(LIBFOSSIL.LDFLAGS)
all: $(th1ish.BIN)

TH1.SHELL := ./$(th1ish.BIN)
TH1.SHELL.FLAGS ?=-x -c -L -a
ifeq (0,1)
  # To stress the allocator more, disable some optimizations:
  TH1.SHELL.FLAGS += --r --t --p --s
else
  # Force-enable all recycling/memory optimizations:
  TH1.SHELL.FLAGS += -r -t -p -s
endif
# That has been known to reveal libcwal-level bugs (but not recently!;)

UNIT_SCRIPT_LIST := $(sort $(subst ./,,$(wildcard unit/???-???.th1ish)))
ifeq (,$(UNIT_SCRIPT_LIST))
$(error UNIT_SCRIPT_LIST is empty?)
endif
UNIT_SCRIPT_FLAGS :=# --verbose
UNIT_SCRIPT_RUNNER := unit-run.th1ish
.PHONY: unit
unit: $(th1ish.BIN)
	@echo "Running unit test scripts..."; \
	export LD_LIBRARY_PATH="$(TOP_SRCDIR):$${LD_LIBRARY_PATH}"; \
	for i in $(UNIT_SCRIPT_LIST); do \
		cmd="$(TH1.SHELL) $(TH1.SHELL.FLAGS) -f $(UNIT_SCRIPT_RUNNER) -- $(UNIT_SCRIPT_FLAGS) $$i"; \
		$$cmd || exit $$?; \
		true; \
	done
#	$(TH1.SHELL) $(TH1.SHELL.FLAGS) -f $(UNIT_SCRIPT_RUNNER) -- $(UNIT_SCRIPT_FLAGS) $(UNIT_SCRIPT_LIST)

########################################################################
# Valgrind sanity checks...
VG := $(call ShakeNMake.CALL.FIND_FILE,valgrind)
ifneq (,$(VG))
VG_REPORT = VG.report.csv
VG_FLAGS ?= --leak-check=full -v --show-reachable=yes --track-origins=yes
#SCRIPT_LIST := $(shell ls -1 test-[0-9]*.th1ish | sort)
.PHONY: $(VG_REPORT)
$(VG_REPORT): $(th1ish.BIN) Makefile
	@echo "Running: $(VG) $(VG_FLAGS) ./$(th1ish.BIN) $(TH1.SHELL.FLAGS) ..."; \
	export LD_LIBRARY_PATH="$(TOP_SRCDIR):$${LD_LIBRARY_PATH}"; \
	for i in $(UNIT_SCRIPT_LIST); do \
		vgout=$$i.vg; \
		cmd="$(VG) $(VG_FLAGS) ./$(th1ish.BIN) $(TH1.SHELL.FLAGS) -f $(UNIT_SCRIPT_RUNNER) -- $(UNIT_SCRIPT_FLAGS) $$i"; \
		echo -n "**** Script [$$i]: "; \
		$$cmd 2>&1 | tee $$vgout | grep 'total heap usage' || exit $$?; \
		grep 'ERROR SUMMARY' $$vgout | grep -v ' 0 errors' && echo "See $$vgout!"; \
		true; \
	done
	@echo "Done running through th1ish scripts. Collecting stats..."; \
	echo 'script,allocs,frees,totalMemory' > $@; \
	for i in $$(find . -name '*.th1ish.vg' | sort); do \
		base=$${i%%.vg}; \
		echo -n "$$base,"; \
		grep 'total heap usage' $$i | sed -e 's/,//g' | awk '{print $$5 "," $$7 "," $$9}'; \
	done >> $@; \
	echo "Stats are in $@:"; \
	tr ',' '\t' < $@

.PHONY: vg
vg: $(VG_REPORT)
CLEAN_FILES += $(wildcard unit/*.vg unit/*.db unit/*~ unit/*.uncompressed unit/*.z)
vgs: TH1.SHELL.FLAGS+=--s
vgs: vg
vgr: TH1.SHELL.FLAGS+=--r
vgr: vg
vgt: TH1.SHELL.FLAGS+=--t
vgt: vg
vgrs: TH1.SHELL.FLAGS+=--r --s
vgrs: vg
vgrst: TH1.SHELL.FLAGS+=--r --s --t
vgrst: vg
endif
#/$(VG)
########################################################################
