# ================================================================
# Makefile for ip_geolocate
#
# Targets:
#   make           — build release binary
#   make debug     — build with AddressSanitizer + debug symbols
#   make clean     — remove build artefacts
#   make run       — build and run
# ================================================================

CC      = gcc
TARGET  = ip_geolocate
SRC     = ip_geolocate.c

CFLAGS  = -O2 -Wall -Wextra -std=c11
LDFLAGS = -lmaxminddb

# macOS: Homebrew puts libmaxminddb here
UNAME := $(shell uname 2>/dev/null)
ifeq ($(UNAME), Darwin)
    CFLAGS  += -I/opt/homebrew/include -I/usr/local/include
    LDFLAGS += -L/opt/homebrew/lib     -L/usr/local/lib
endif

.PHONY: all debug clean run

all: $(TARGET)

$(TARGET): $(SRC)
	$(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
	@echo "Build successful → ./$(TARGET)"
	@echo "Make sure GeoLite2-City.mmdb is in this directory."

debug: $(SRC)
	$(CC) -g -O0 -fsanitize=address,undefined -Wall -Wextra \
	      -o $(TARGET)_debug $< $(LDFLAGS)
	@echo "Debug build → ./$(TARGET)_debug"

run: all
	./$(TARGET)

clean:
	rm -f $(TARGET) $(TARGET)_debug
