# Makefile for Socket Server/Client
# Windows: Build server
# Linux: Build client

# Detect operating system
ifeq ($(OS),Windows_NT)
    # Windows settings
    CXX = g++
    CXXFLAGS = -Wall -Wextra -O2 -std=c++11 -D_WIN32_WINNT=0x0601
    LDFLAGS = -lws2_32 -static-libgcc -static-libstdc++
    EXE_SUFFIX = .exe
    RM = del /Q
    MKDIR = mkdir
    TARGET = server
    SRC = server_code.cpp
else
    # Linux settings
    CXX = g++
    CXXFLAGS = -Wall -Wextra -O2 -std=c++11
    LDFLAGS = -pthread
    EXE_SUFFIX = 
    RM = rm -f
    MKDIR = mkdir -p
    TARGET = client
    SRC = client_code.cpp
endif

# Output executables
SERVER = server_boxed$(EXE_SUFFIX)
CLIENT = client$(EXE_SUFFIX)

# Default target based on platform
ifeq ($(OS),Windows_NT)
all: $(SERVER)
	@echo.
	@echo ===================================
	@echo Windows Server built successfully!
	@echo Run: $(SERVER)
	@echo ===================================
else
all: $(CLIENT)
	@echo
	@echo ===================================
	@echo Linux Client built successfully!
	@echo Run: sudo ./$(CLIENT)
	@echo ===================================
endif

# Server target (Windows)
$(SERVER): server_code.cpp
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

# Client target (Linux)
$(CLIENT): client_code.cpp
	$(CXX) $(CXXFLAGS) $< -o $@ $(LDFLAGS)

# Build both (manual override)
both: server client

server: server_code.cpp
	$(CXX) $(CXXFLAGS) $< -o $(SERVER) $(LDFLAGS) -lws2_32

client: client_code.cpp
	$(CXX) $(CXXFLAGS) $< -o $(CLIENT) $(LDFLAGS) -pthread

# Clean target
ifeq ($(OS),Windows_NT)
clean:
	if exist $(SERVER) $(RM) $(SERVER)
	if exist *.o $(RM) *.o
	@echo Cleaned Windows build files
else
clean:
	$(RM) $(CLIENT) *.o
	@echo Cleaned Linux build files
endif

# Clean all platforms
clean-all:
	-$(RM) $(SERVER) $(CLIENT) *.o 2>nul || $(RM) $(SERVER) $(CLIENT) *.o 2>/dev/null || true

# Test targets
test: all
ifeq ($(OS),Windows_NT)
	@echo.
	@echo Starting Windows server...
	@echo Make sure to configure RNDIS network adapter first:
	@echo   IP: 192.168.7.100
	@echo   Subnet: 255.255.255.0
	@echo.
	$(SERVER)
else
	@echo
	@echo Testing network connectivity...
	@ping -c 2 192.168.7.100 || echo "Warning: Cannot reach server IP"
	@echo
	@echo Starting Linux client...
	sudo ./$(CLIENT)
endif

# Network check
check-network:
ifeq ($(OS),Windows_NT)
	@echo Checking Windows network configuration...
	ipconfig | findstr /C:"192.168.7" /C:"RNDIS" /C:"Remote NDIS"
else
	@echo Checking Linux network configuration...
	@echo === USB0 Interface ===
	@ifconfig usb0 2>/dev/null || ip addr show usb0 2>/dev/null || echo "usb0 not found"
	@echo
	@echo === Route Table ===
	@route -n 2>/dev/null || ip route show
	@echo
	@echo === Ping Test ===
	@ping -c 2 192.168.7.100 2>/dev/null || echo "Cannot ping server"
endif

# Install target
ifeq ($(OS),Windows_NT)
install:
	@echo To install on Windows, copy $(SERVER) to a convenient location
	@echo Example: copy $(SERVER) C:\Tools\
else
install:
	@echo To install on Linux:
	@echo   sudo cp $(CLIENT) /usr/local/bin/
	@echo   sudo chmod +x /usr/local/bin/$(CLIENT)
endif

# Help target
help:
	@echo ========================================
	@echo Socket Server/Client Makefile Help
	@echo ========================================
	@echo.
ifeq ($(OS),Windows_NT)
	@echo Windows Platform Detected
	@echo.
	@echo Available targets:
	@echo   make          - Build server for Windows
	@echo   make server   - Build server explicitly
	@echo   make clean    - Remove Windows executables
	@echo   make test     - Build and run server
	@echo   make check-network - Check network config
	@echo   make help     - Show this help
	@echo.
	@echo Network Setup:
	@echo   1. Connect Zynq board via USB Type-C
	@echo   2. Install RNDIS driver in Device Manager
	@echo   3. Configure RNDIS adapter:
	@echo      - IP: 192.168.7.100
	@echo      - Subnet: 255.255.255.0
	@echo   4. Run: $(SERVER)
else
	@echo Linux Platform Detected
	@echo.
	@echo Available targets:
	@echo   make          - Build client for Linux
	@echo   make client   - Build client explicitly
	@echo   make clean    - Remove Linux executables
	@echo   make test     - Build and run client
	@echo   make check-network - Check network config
	@echo   make help     - Show this help
	@echo.
	@echo Network Setup:
	@echo   1. Configure USB gadget RNDIS
	@echo   2. Set usb0 IP: sudo ifconfig usb0 192.168.7.1 netmask 255.255.255.0 up
	@echo   3. Test ping: ping 192.168.7.100
	@echo   4. Run: sudo ./$(CLIENT)
endif
	@echo ========================================

# Debug target - show variables
debug:
	@echo OS: $(OS)
	@echo CXX: $(CXX)
	@echo CXXFLAGS: $(CXXFLAGS)
	@echo LDFLAGS: $(LDFLAGS)
	@echo TARGET: $(TARGET)
	@echo SRC: $(SRC)
ifeq ($(OS),Windows_NT)
	@echo Building: $(SERVER)
else
	@echo Building: $(CLIENT)
endif

.PHONY: all both server client clean clean-all test check-network install help debug