# Test client Makefile
#
# Builds the C integration test client against 9p4z client API.
# Links the existing unix tcp_transport instead of duplicating it.

CC ?= cc
CFLAGS = -Wall -Wextra -O2 -g -include $(UNIX_DIR)/include/ninep_unix_config.h

# Paths
UNIX_DIR = ../../unix
AETHERD_DIR = ../..
# Probe order mirrors unix/Makefile — keep the two in step, or the test
# clients silently fail to build on a machine where the server builds fine.
NINEP_DIR ?= $(shell \
    for d in /mnt2/src/9p4z.tmp "$(AETHERD_DIR)/modules/9p4z" "$(AETHERD_DIR)/../9p4z" "$(HOME)/src/9p4z"; do \
        if [ -d "$$d/src" ] && [ -f "$$d/src/union_fs.c" ]; then \
            echo "$$d"; break; \
        fi; \
    done)

# Include paths - unix port's Zephyr shims + tcp_transport.h
INCLUDES = -I$(UNIX_DIR)/include -I$(UNIX_DIR)/include/zephyr \
           -I$(NINEP_DIR)/include -I$(AETHERD_DIR)/include \
           -I$(AETHERD_DIR)/include/zephyr

LDFLAGS = -lpthread

# 9p4z sources needed by the client
NINEP_SRCS = $(NINEP_DIR)/src/client.c \
             $(NINEP_DIR)/src/message.c \
             $(NINEP_DIR)/src/proto.c

# Reuse unix TCP transport (no duplication)
UNIX_SRCS = $(UNIX_DIR)/src/tcp_transport.c

TEST_CLIENT = 9pcli
CHAT_PIPELINE = chat_pipeline_test
GFX_PIPELINE = gfx_pipeline_test

.PHONY: all clean

all: $(TEST_CLIENT) $(CHAT_PIPELINE) $(GFX_PIPELINE)

$(TEST_CLIENT): test_client.c test_harness.h $(NINEP_SRCS) $(UNIX_SRCS)
	$(CC) $(CFLAGS) $(INCLUDES) -o $@ test_client.c $(NINEP_SRCS) $(UNIX_SRCS) $(LDFLAGS)

$(CHAT_PIPELINE): chat_pipeline_test.c test_harness.h $(NINEP_SRCS) $(UNIX_SRCS)
	$(CC) $(CFLAGS) $(INCLUDES) -o $@ chat_pipeline_test.c $(NINEP_SRCS) $(UNIX_SRCS) $(LDFLAGS)

$(GFX_PIPELINE): gfx_pipeline_test.c test_harness.h $(NINEP_SRCS) $(UNIX_SRCS)
	$(CC) $(CFLAGS) $(INCLUDES) -o $@ gfx_pipeline_test.c $(NINEP_SRCS) $(UNIX_SRCS) $(LDFLAGS)

# Not part of `all` — debugging/demo tools, not tests
gfx_snap: gfx_snap.c test_harness.h $(NINEP_SRCS) $(UNIX_SRCS)
	$(CC) $(CFLAGS) $(INCLUDES) -o $@ gfx_snap.c $(NINEP_SRCS) $(UNIX_SRCS) $(LDFLAGS)

# Live terminal client (half-block renderer): browse the BBS, play doom
gfxterm: gfxterm.c test_harness.h $(NINEP_SRCS) $(UNIX_SRCS)
	$(CC) $(CFLAGS) $(INCLUDES) -o $@ gfxterm.c $(NINEP_SRCS) $(UNIX_SRCS) $(LDFLAGS)

# Screen-share uplink: stream /dev/fb0 or a PNM pipe to /gfx/broadcast
frstcast: frstcast.c test_harness.h $(NINEP_SRCS) $(UNIX_SRCS)
	$(CC) $(CFLAGS) $(INCLUDES) -o $@ frstcast.c $(NINEP_SRCS) $(UNIX_SRCS) $(LDFLAGS)

clean:
	rm -f $(TEST_CLIENT) $(CHAT_PIPELINE) $(GFX_PIPELINE) gfx_snap gfxterm
