CC      ?= cc
CFLAGS  ?= -std=c99 -Wall -Wextra -O2

SRC_DIR   := src
TEST_DIR  := tests
BUILD_DIR := build
OBJ_DIR   := $(BUILD_DIR)/obj

# Shares deck-engine's build output directory (see ../engine/Makefile),
# so a daily run can find both binaries in one place:
#   dist/deck-engine ... --format json | dist/interpreter-cli
DIST_DIR := ../dist
BINARY   := $(DIST_DIR)/interpreter-cli

# Only ever compiles this module's own sources - significance.h/
# reading_io.h reach into engine/src/ for struct/enum *definitions*
# (plain #includes), but no engine/src/*.c (or the vendored Astronomy
# Engine) is compiled or linked here. That's what keeps this module (and
# its tests) independent of the engine's actual ephemeris/tarot-draw
# implementation - see significance.c's and reading_io.c's own comments
# for why each duplicates a small table instead of linking astro.c.
LIB_SRCS := $(SRC_DIR)/significance.c $(SRC_DIR)/json.c $(SRC_DIR)/reading_io.c
LIB_OBJS := $(patsubst %.c,$(OBJ_DIR)/%.o,$(LIB_SRCS))
MAIN_OBJ := $(OBJ_DIR)/$(SRC_DIR)/main.o

SIGNIFICANCE_TEST_OBJ := $(OBJ_DIR)/$(TEST_DIR)/significance_test.o
JSON_TEST_OBJ         := $(OBJ_DIR)/$(TEST_DIR)/json_test.o
SIGNIFICANCE_TEST_BIN := $(BUILD_DIR)/significance_test
JSON_TEST_BIN         := $(BUILD_DIR)/json_test

.PHONY: all test clean

all: $(BINARY)

$(BINARY): $(LIB_OBJS) $(MAIN_OBJ)
	@mkdir -p $(DIST_DIR)
	$(CC) $(CFLAGS) -o $@ $^

test: $(SIGNIFICANCE_TEST_BIN) $(JSON_TEST_BIN)
	./$(SIGNIFICANCE_TEST_BIN)
	./$(JSON_TEST_BIN)

$(SIGNIFICANCE_TEST_BIN): $(OBJ_DIR)/$(SRC_DIR)/significance.o $(SIGNIFICANCE_TEST_OBJ)
	$(CC) $(CFLAGS) -o $@ $^

$(JSON_TEST_BIN): $(OBJ_DIR)/$(SRC_DIR)/json.o $(OBJ_DIR)/$(SRC_DIR)/reading_io.o $(JSON_TEST_OBJ)
	$(CC) $(CFLAGS) -o $@ $^

$(OBJ_DIR)/%.o: %.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c -o $@ $<

clean:
	rm -rf $(BUILD_DIR) $(BINARY)
