# Makefile for dmsdiz - DMS file description utility
# Cross-platform compatible Makefile

# Compiler settings
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -g -O2
LDFLAGS = 
TARGET = dmsdiz
SOURCES = dmsdiz.c
OBJECTS = $(SOURCES:.c=.o)

# Default target
all: $(TARGET)

# Build the main executable
$(TARGET): $(OBJECTS)
	$(CC) $(OBJECTS) -o $(TARGET) $(LDFLAGS)

# Compile source files to object files
%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

# Clean build artifacts
clean:
	rm -f $(OBJECTS) $(TARGET)

# Install (optional - adjust paths as needed)
install: $(TARGET)
	install -d $(DESTDIR)/usr/local/bin
	install -m 755 $(TARGET) $(DESTDIR)/usr/local/bin/

# Uninstall
uninstall:
	rm -f $(DESTDIR)/usr/local/bin/$(TARGET)

# Test compilation without optimization for debugging
debug: CFLAGS = -Wall -Wextra -std=c99 -g -DDEBUG
debug: $(TARGET)

# Release build with optimizations
release: CFLAGS = -Wall -Wextra -std=c99 -O2 -DNDEBUG
release: clean $(TARGET)

# Check for memory leaks (requires valgrind)
memcheck: debug
	@echo "Running memory check (requires valgrind)..."
	@if command -v valgrind >/dev/null 2>&1; then \
		echo "Usage: valgrind --leak-check=full ./$(TARGET) [options] <dms-file>"; \
	else \
		echo "valgrind not found. Install valgrind to run memory checks."; \
	fi

# Show help
help:
	@echo "Available targets:"
	@echo "  all      - Build the program (default)"
	@echo "  clean    - Remove build artifacts"
	@echo "  debug    - Build with debug symbols and no optimization"
	@echo "  release  - Build optimized release version"
	@echo "  install  - Install to system (requires root/sudo)"
	@echo "  uninstall- Remove from system (requires root/sudo)"
	@echo "  memcheck - Show how to run memory leak check"
	@echo "  help     - Show this help message"

# Declare phony targets
.PHONY: all clean install uninstall debug release memcheck help
