CC := cc
CFLAGS := -Wall -Wextra -Werror -pedantic
CFLAGS_RELEASE := -O2 -DNDEBUG
CFLAGS_DEBUG := -g3 -O0
# NOTE: exposes header impls into dynamically linked modules
LDFLAGS := -rdynamic
LDLIBS := \
	-lm \
	-lssl \
	-lgit2
DEPS := \
	-I$(HOME)/git/sock/src \
	-I$(HOME)/git/tpee/src \
	-I$(HOME)/git/tpee_ext/src \
	-I$(HOME)/git/buf/src \
	-I$(HOME)/git/orb/src \
	-I$(HOME)/git/md4c/src
SOURCE := \
	src/main.c \
	$(HOME)/git/md4c/src/*.c \
	src/module/page.c \
	src/module/default.c \
	src/module/index.c \
	src/module/git_list.c \
	src/module/git_repo.c \
	src/module/git_repo_log.c \
	src/module/git_repo_tree.c \
	src/module/git_repo_refs.c \
	src/module/git_repo_commit.c \
	src/module/git_repo_blob.c
VALGRIND := valgrind \
	--leak-check=full \
	--show-leak-kinds=all \
	--track-origins=yes
TARGET := soso

export DUC_HEADERPATH := ./src/templ/
export DUC_BUILDPATH := ./src/templ/
export DUC_LIBPATH := $(CFLAGS) $(DEPS)

all: build

build-templ:
	mkdir -p ./src/templ
ifdef TEMPL
	ductape ./templ/$(TEMPL).html
else
	ductape ./templ/*.html
endif

build: $(SOURCE)
	$(CC) -o $(TARGET) $(CFLAGS) $(DEPS) $(LDFLAGS) $^ $(LDLIBS)

build-release: $(SOURCE)
	$(CC) -o $(TARGET) $(CFLAGS) $(DEPS) $(CFLAGS_RELEASE) $(LDFLAGS) $^ $(LDLIBS)

build-debug: $(SOURCE)
	$(CC) -o $(TARGET) $(CFLAGS) $(DEPS) $(CFLAGS_DEBUG) $(LDFLAGS) $^ $(LDLIBS)

run: build
	./$(TARGET)

run-release: build-release
	./$(TARGET)

run-debug: build-debug
	$(VALGRIND) ./$(TARGET)

clean:
	rm -f ./$(TARGET)
	rm -rf ./src/templ
