# =============================================================================
# Makefile for CMU 15-213 Lecture 20 (network programming II)
# =============================================================================
#
#   make run      run both demos: the echo pair, then the HTTP pair
#   make clean
#
# All four programs link netutil.o (the sockets interface) and rio.o -- the
# Robust I/O package from LECTURE 18, compiled straight out of ../lecture18.
# That cross-lecture link is the C equivalent of the Python demos' course_path.
#
# Each demo starts a server (bound to the LOOPBACK interface only), runs a
# client against it, and the server exits by itself after serving its fixed
# number of connections -- so nothing is left running and no `kill` is needed.
# The server's own output is captured to a log and printed after the client's,
# so the transcript is ordered instead of two processes interleaving.
# -----------------------------------------------------------------------------

CC     ?= cc
CFLAGS := -std=c11 -O2 -Wall -Wextra -I../lecture18

ECHO_PORT ?= 15213
HTTP_PORT ?= 15214

.PHONY: all run run-echo run-http clean
all: build/echoserver build/echoclient build/tiny_server build/http_client

build:
	mkdir -p build

# ---- shared objects --------------------------------------------------------
build/rio.o: ../lecture18/rio.c ../lecture18/rio.h | build
	$(CC) $(CFLAGS) -c -o $@ ../lecture18/rio.c

build/netutil.o: netutil.c netutil.h | build
	$(CC) $(CFLAGS) -c -o $@ netutil.c

OBJS := build/netutil.o build/rio.o

# ---- the four programs -----------------------------------------------------
build/echoserver:  echoserver.c  netutil.h $(OBJS) | build
	$(CC) $(CFLAGS) -o $@ echoserver.c $(OBJS)
build/echoclient:  echoclient.c  netutil.h $(OBJS) | build
	$(CC) $(CFLAGS) -o $@ echoclient.c $(OBJS)
build/tiny_server: tiny_server.c netutil.h $(OBJS) | build
	$(CC) $(CFLAGS) -o $@ tiny_server.c $(OBJS)
build/http_client: http_client.c netutil.h $(OBJS) | build
	$(CC) $(CFLAGS) -o $@ http_client.c $(OBJS)

# ---- demo 1: the echo server and client ------------------------------------
run-echo: build/echoserver build/echoclient
	@echo "===== demo 1: echo server + client (port $(ECHO_PORT)) ====="
	@./build/echoserver $(ECHO_PORT) 1 > build/echoserver.log 2>&1 & \
	 sleep 0.2; \
	 ./build/echoclient localhost $(ECHO_PORT); \
	 wait; \
	 echo; echo "----- the server's own transcript -----"; \
	 cat build/echoserver.log

# ---- demo 2: the tiny web server and an HTTP client ------------------------
run-http: build/tiny_server build/http_client
	@echo "===== demo 2: tiny web server + raw HTTP client (port $(HTTP_PORT)) ====="
	@./build/tiny_server $(HTTP_PORT) www 2 > build/tiny_server.log 2>&1 & \
	 sleep 0.2; \
	 ./build/http_client localhost $(HTTP_PORT) /; \
	 echo; echo "----- and a 404, to see an error response -----"; \
	 ./build/http_client localhost $(HTTP_PORT) /nope.txt 2>&1 | \
	   sed -n '/response status line/,/end of body/p'; \
	 wait; \
	 echo; echo "----- the server's own transcript -----"; \
	 cat build/tiny_server.log

run: run-echo
	@echo
	@$(MAKE) --no-print-directory run-http

clean:
	rm -rf build __pycache__
