# Initial config
# LIBS=
SRC:=
SRC+=$(wildcard src/*.c)
SRC+=$(filter-out src/util/buffer.c, $(wildcard src/*/*.c))

# Generated
SRC+=src/util/buffer.c

CFLAGS?=-Wall -s -O2

INCLUDES:=
INCLUDES+=-I src

include lib/.dep/config.mk

CFLAGS+=$(INCLUDES)

# Which objects to generate before merging everything together
OBJ:=$(SRC:.c=.o)

BIN=confgen

.PHONY: default
default: $(BIN)
# default: $(BIN) $(BIN).1

lib/cozis/tinytemplate/tt:
	$(MAKE) --directory lib/cozis/tinytemplate

src/util/buffer.h: template/util/buffer.h lib/cozis/tinytemplate/tt template/util/buffer.json
	cat $< | lib/cozis/tinytemplate/tt template/util/buffer.json > $@

src/util/buffer.c: template/util/buffer.c lib/cozis/tinytemplate/tt src/util/buffer.h template/util/buffer.json
	cat $< | lib/cozis/tinytemplate/tt template/util/buffer.json > $@

%.o: %.c $(LIBS)
	$(CC) $(CFLAGS) $(@:.o=.c) -D__NAME=\"$(BIN)\" -c -o $@

$(BIN): $(OBJ)
	$(CC) $(CFLAGS) $(OBJ) -o $@

# # Build manpage
# $(BIN).1: manpage.1.md
# 	env NAME=$(BIN) envsubst < manpage.1.md | pandoc --standalone --from markdown --to man -o $(BIN).1

.PHONY: clean
clean:
	rm -f $(OBJ)
