all:
########################################################################
# config.make includes common.make, which brings with it
# lots of utility code.
include config.make

FSL.SRC := \
	fsl.c \
	fsl_appendf.c \
	fsl_bag.c \
	fsl_buffer.c \
	fsl_content.c \
	fsl_db.c \
	fsl_delta.c \
	fsl_encode.c \
	fsl_fs.c \
	fsl_md5.c \
	fsl_mf.c \
	fsl_sha1.c \
	fsl_tag.c \
	fsl_utf8.c

FSL.OBJ := $(subst .c,.o,$(FSL.SRC))

MAIN_MAKEFILES := $(PACKAGE.MAKEFILE) $(ShakeNMake.MAKEFILE) config.make
$(FSL.OBJ): $(MAIN_MAKEFILES)

#fsl_sha1.o: CFLAGS+=-std=c89

########################################################################
# DLL/LIB...
libfossil.LIB.OBJECTS := $(FSL.OBJ)
libfossil.DLL.OBJECTS := $(libfossil.LIB.OBJECTS)
$(eval $(call ShakeNMake.CALL.RULES.LIBS,libfossil))
$(eval $(call ShakeNMake.CALL.RULES.DLLS,libfossil))
all: $(libfossil.LIB) $(libfossil.DLL)

########################################################################
# Binary...
test.BIN.OBJECTS := test.o
test.BIN.LDFLAGS := -L. -lfossil -lsqlite3 -lz
$(eval $(call ShakeNMake.CALL.RULES.BIN,test))
all: $(test.BIN)

########################################################################
# A quick-n-dirty amalgamation build...
AMAL_C := libfossil_amalgamation.c
AMAL_H := libfossil_amalgamation.h
AMAL_C.SRC := \
	fsl_internal.h \
	$(FSL.SRC)

INCD := include/fossil
# ACHTUNG: in AMAL_H.SRC, config.h MUST come first.
AMAL_H.SRC := \
	$(INCD)/config.h \
	$(INCD)/fossil.h
$(AMAL_H.SRC):
$(AMAL_C.SRC):
$(AMAL_H): $(MAIN_MAKEFILES) $(AMAL_H.SRC)
$(AMAL_C): $(MAIN_MAKEFILES) $(AMAL_C.SRC)

$(AMAL_H): 
	@echo "Creating $@..."
	@{ \
		echo '#if !defined(FSL_AMALGAMATION_BUILD)'; \
		echo '#define FSL_AMALGAMATION_BUILD 1'; \
		echo '#endif'; \
	} > $@
	@{ \
		for i in $(AMAL_H.SRC); do \
			echo "/* start of file $$i */"; \
			cat $$i; \
			echo "/* end of file $$i */"; \
		done; \
	} | sed -e '/[ ]*#[ ]*include[ ]*.*fsl_.*\.h[">]/d' \
		 -e '/[ ]*#[ ]*include[ ]*.*fossil.*\.h[">]/d' \
		 -e '/[ ]*#[ ]*include[ ]*.*"config\.h"/d' \
		>> $@

$(AMAL_C): $(AMAL_H)
	@echo "Creating $@..."
	@echo '#include "$(AMAL_H)"' > $@
	@{ \
		for i in $(AMAL_C.SRC); do \
			echo "/* start of file $$i */"; \
			cat $$i; \
			echo "/* end of file $$i */"; \
		done; \
	} | sed -e '/[ ]*#[ ]*include[ ]*.*fsl_.*\.h[">]/d' \
		 -e '/[ ]*#[ ]*include[ ]*.*fossil.*\.h[">]/d' \
		>> $@

CLEAN_FILES += $(AMAL_H) $(AMAL_C)
amal: $(AMAL_C)
	gcc -c -pedantic -Wall -Werror -std=c99 $(AMAL_C)
# /amalgamation
########################################################################