# TermTCP - Makefile

CC  = gcc
CXX = g++
CFLAGS   = -Wall -Wextra -O2 -std=gnu99 -D_XOPEN_SOURCE_EXTENDED
CXXFLAGS = -Wall -Wextra -O2 -std=c++11
LDFLAGS  = -lm -lncursesw

# FLTK
FLTK_CXXFLAGS = $(shell fltk-config --cxxflags)
FLTK_LDFLAGS  = $(shell fltk-config --ldflags)

TARGET     = termtcp
GUI_TARGET = termtcp-gui
SOURCE     = termtcp.c
GUI_SOURCE = termtcp_gui.cpp
OBJECT     = $(SOURCE:.c=.o)
GUI_OBJECT = $(GUI_SOURCE:.cpp=.o)

# Default target
all: $(TARGET)

# Build executable
$(TARGET): $(OBJECT)
	$(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS)
	@echo "Build complete: $(TARGET)"
	@ls -lh $(TARGET)

# Compile
%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

# GUI target
gui: $(GUI_TARGET)

$(GUI_TARGET): $(GUI_OBJECT)
	$(CXX) $(CXXFLAGS) -o $@ $^ $(FLTK_LDFLAGS)
	@echo "Build complete: $(GUI_TARGET)"
	@ls -lh $(GUI_TARGET)

$(GUI_OBJECT): $(GUI_SOURCE)
	$(CXX) $(CXXFLAGS) $(FLTK_CXXFLAGS) -c $< -o $@

# Debug build
debug: CFLAGS += -g -O0 -DDEBUG
debug: clean $(TARGET)
	@echo "Debug build complete"

# Run the application
run: $(TARGET)
	./$(TARGET)

# Clean
clean:
	rm -f $(OBJECT) $(GUI_OBJECT) $(TARGET) $(GUI_TARGET)
	@echo "Clean complete"

# Install to system
install: $(TARGET)
	install -m 755 $(TARGET) /usr/local/bin/
	@echo "Installed $(TARGET) to /usr/local/bin/"

# Uninstall from system
uninstall:
	rm -f /usr/local/bin/$(TARGET)
	@echo "Uninstalled $(TARGET)"

# Static analysis (if available)
lint:
	@which cppcheck > /dev/null && cppcheck --quiet $(SOURCE) || echo "cppcheck not found"

# Help
help:
	@echo "TermTCP Build Targets:"
	@echo "  make              - Build the application"
	@echo "  make debug        - Build with debug symbols"
	@echo "  make run          - Build and run"
	@echo "  make clean        - Remove build artifacts"
	@echo "  make install      - Install to /usr/local/bin/"
	@echo "  make uninstall    - Remove from /usr/local/bin/"
	@echo "  make lint         - Run static analysis"
	@echo "  make help         - Show this message"

.PHONY: all gui debug run clean install uninstall lint help
