all:
include ../config.make
INCLUDES_PATH ?= $(HOME)/include /usr/local/include /usr/include

FOSSIL.LIB := ../libfossil.a
$(FOSSIL.LIB):
	$(MAKE) -C ..

########################################################################
# 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 ?= 1
ifeq (1,$(ENABLE_MODULES))
  DLFCN_H := $(call INC_SEARCH,dlfcn.h)
  ifneq (,$(DLFCN_H))
    DL_IMPL := dl
    DL_LDFLAGS := -ldl
  else
    DLFCN_H := $(call INC_SEARCH,ltdl.h)
    ifneq (,$(DLFCN_H))
      DL_IMPL := ltdl
      DL_LDFLAGS := -lltdl
    endif
  endif
endif
ifeq (,$(DL_IMPL))
  ENABLE_MODULES := 0
endif
# /ENABLE_MODULES
########################################################################

########################################
# Set up file-specific CPPFLAGS/LDFLAGS.
# Set up cpdoish module
ifeq (1,$(ENABLE_MODULES))
  th1ish.BIN.LDFLAGS += -export-dynamic $(DL_LDFLAGS)
  th1ish_amalgamation.o shell.o: CPPFLAGS+=-DTH1ISH_ENABLE_MODULES=1
  shell.o: CPPFLAGS+=-DTH1ISH_AMALGAMATION_BUILD=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_SHELL_EXTEND
shell.o: shell_extend.c
Makefile:
shell.o shell_extend.o: Makefile
th1ish.BIN.OBJECTS += shell.o th1ish_amalgamation.o shell_extend.o
th1ish.BIN.LDFLAGS += -lm $(FOSSIL.LIB) -lsqlite3 -lz
$(eval $(call ShakeNMake.CALL.RULES.BIN,th1ish))
$(th1ish.BIN): $(FOSSIL.LIB)
all: $(th1ish.BIN)

TH1.SHELL := ./$(th1ish.BIN)
TH1.SHELL.FLAGS ?=-x -c --L -a
SCRIPT_LIST := $(shell ls -1 test-[0-9]*.th1ish | sort -n)
SCRIPT_FLAGS := -c
ifeq (,$(SCRIPT_LIST))
$(error SCRIPT_LIST is empty?)
endif


run: $(th1ish.BIN)
	@banner='------------------------------'; \
	for i in $(SCRIPT_LIST); do \
		cmd="$(TH1.SHELL) $(TH1.SHELL.FLAGS) -f $$i -- $(SCRIPT_FLAGS)"; \
		echo $$banner "Running: $$cmd"; \
		$$cmd || exit $$?; \
		echo $$banner "Done: $$cmd"; \
	done


########################################################################
# 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)
$(VG_REPORT) vg: $(th1ish.BIN)
	@echo "Running: $(VG) $(VG_FLAGS) ./$(th1ish.BIN) $(TH1.SHELL.FLAGS) ..."; \
	for i in $(SCRIPT_LIST); do \
		vgout=vg.$$i; \
		cmd="$(VG) $(VG_FLAGS) ./$(th1ish.BIN) $(TH1.SHELL.FLAGS) -f $$i -- $(SCRIPT_FLAGS)"; \
		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..."; \
	for i in vg.*.th1ish; do \
		base=$${i##vg.}; \
		echo -n "$$base,"; \
		grep 'total heap usage' $$i | sed -e 's/,//g' | awk '{print $$5 "," $$7 "," $$9}'; \
	done > $(VG_REPORT); \
	echo "Stats are in $(VG_REPORT)."
CLEAN_FILES += $(wildcard vg.*)
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)
########################################################################
