CC = gcc
CFLAGS = -Wall -Wextra -Wpedantic -std=c11 -Isrc

all: looper-client test_status_parse

looper-client: src/main.c src/tui.c $(CARLA_OBJ)
	$(CC) $(CFLAGS) -o $@ $^ -lncurses

test_status_parse: tests/test_status_parse.c
	$(CC) $(CFLAGS) -o test_status_parse tests/test_status_parse.c src/tui.c -lncurses

# --- Carla host stubs ---
CARLA_OBJ = src/carla_host.o

$(CARLA_OBJ): src/carla_host.c src/carla_host.h
	$(CC) $(CFLAGS) -c -o $@ $<

# --- Carla host tests ---
TEST_CARLA_BIN = test_carla_host
TEST_CARLA_OBJ = tests/test_carla_host.o

$(TEST_CARLA_OBJ): tests/test_carla_host.c src/carla_host.h
	$(CC) $(CFLAGS) -c -o $@ $<

$(TEST_CARLA_BIN): $(TEST_CARLA_OBJ) $(CARLA_OBJ)
	$(CC) $(CFLAGS) -o $@ $^

# ensure the tests directory exists
tests/test_carla_host.o: | tests

# --- send_command test ---
TEST_CLIENT_BIN = test_client
TEST_CLIENT_OBJ = tests/test_client.o

$(TEST_CLIENT_OBJ): tests/test_client.c src/tui.h
	$(CC) $(CFLAGS) -c -o $@ $<

$(TEST_CLIENT_BIN): $(TEST_CLIENT_OBJ) src/tui.c
	$(CC) $(CFLAGS) -o $@ $^ -lncurses

test: looper-client test_status_parse $(TEST_CARLA_BIN) $(TEST_CLIENT_BIN)
	./test_status_parse
	./$(TEST_CARLA_BIN)
	./$(TEST_CLIENT_BIN)

.PHONY: all test clean

clean:
	rm -f looper-client test_status_parse $(TEST_CARLA_BIN) $(TEST_CLIENT_BIN) *.o tests/*.o src/*.o
