CC := gcc
SRCD := src
TSTD := tests
BLDD := build
BIND := bin
INCD := include
LIBD := lib
UTILD := util

MAIN  := $(BLDD)/main.o
LIB := $(LIBD)/pbx.a
LIB_DB := $(LIBD)/pbx.a

ALL_SRCF := $(shell find $(SRCD) -type f -name *.c)
ALL_OBJF := $(patsubst $(SRCD)/%,$(BLDD)/%,$(ALL_SRCF:.c=.o))
ALL_FUNCF := $(filter-out $(MAIN), $(ALL_OBJF))

TEST_SRC := $(shell find $(TSTD) -type f -name *.c)

INC := -I $(INCD)

CFLAGS := -Wall -Werror -Wno-unused-function -Wno-error=switch -MMD
DFLAGS := -g -DDEBUG -DCOLOR
PRINT_STAMENTS := -DERROR -DSUCCESS -DWARN -DINFO

STD := -std=gnu11
TEST_LIB := -lcriterion
LIBS := $(LIB) -lpthread
LIBS_DB := $(LIB_DB) -lpthread
EXCLUDES := excludes.h

CFLAGS += $(STD) -DTEST_CONFIG_C

EXEC := pbx
TEST_EXEC := $(EXEC)_tests

.PHONY: clean all setup debug

all: setup $(BIND)/$(EXEC) $(INCD)/$(EXCLUDES) $(BIND)/$(TEST_EXEC)

debug: CFLAGS += $(DFLAGS) $(PRINT_STAMENTS)
debug: all

tester: $(UTILD)/tester

setup: $(BIND) $(BLDD)
$(BIND):
	mkdir -p $(BIND)
$(BLDD):
	mkdir -p $(BLDD)

$(UTILD)/tester: $(UTILD)/tester.c src/globals.c
	$(CC) $(DFLAGS) $(INC) $^ -o $@

$(BIND)/$(EXEC): $(MAIN) $(ALL_FUNCF)
	$(CC) $^ -o $@ $(LIBS)

$(BIND)/$(TEST_EXEC): $(ALL_FUNCF) $(TEST_SRC)
	$(CC) $(CFLAGS) $(INC) $(ALL_FUNCF) $(TEST_SRC) $(TEST_LIB) $(LIBS) -o $@

$(BLDD)/%.o: $(SRCD)/%.c
	$(CC) $(CFLAGS) $(INC) -c -o $@ $<

clean:
	rm -rf $(BLDD) $(BIND)

$(INCD)/$(EXCLUDES): $(BIND)/$(EXEC)
	rm -f $@
	touch $@
	if nm $(BIND)/$(EXEC) | grep INSTRUCTOR_MAIN > /dev/null; then \
           echo "#define NO_MAIN" >> $@; \
        fi
	if nm $(BIND)/$(EXEC) | grep INSTRUCTOR_SERVER > /dev/null; then \
           echo "#define NO_SERVER" >> $@; \
        fi
	if nm $(BIND)/$(EXEC) | grep INSTRUCTOR_PBX > /dev/null; then \
           echo "#define NO_PBX" >> $@; \
        fi

.PRECIOUS: $(BLDD)/*.d
-include $(BLDD)/*.d
