# AudioAnalysis MP3 Player Library Makefile
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2 -pthread -fPIC
LIBS = -lSDL2 -lmpg123 -lm

# Library and demo targets
LIBRARY = libaudioanalysis.a
SOURCES = mp3_player.cpp
HEADERS = mp3_player.hpp

# Object files
LIB_OBJECTS = mp3_player.o
DEMO_OBJECTS = demo.o

# Default target
all: $(LIBRARY) $(DEMO)

# Build the static library
$(LIBRARY): $(LIB_OBJECTS)
	ar rcs $@ $^
	@echo "✅ Library $(LIBRARY) built successfully!"

# Build the demo executable
$(DEMO): $(DEMO_OBJECTS) $(LIBRARY)
	$(CXX) $(CXXFLAGS) -o $@ $(DEMO_OBJECTS) -L. -laudioanalysis $(LIBS)
	@echo "✅ Demo $(DEMO) built successfully!"

# Build library object files
mp3_player.o: mp3_player.cpp mp3_player.hpp
	$(CXX) $(CXXFLAGS) -c mp3_player.cpp

# Clean build files
clean:
	rm -f $(LIB_OBJECTS) $(DEMO_OBJECTS) $(LIBRARY) $(DEMO)
	@echo "🧹 Cleaned build files"

# Install library and headers
install: $(LIBRARY) $(HEADERS)
	sudo mkdir -p /usr/local/lib
	sudo mkdir -p /usr/local/include/audioanalysis
	sudo cp $(LIBRARY) /usr/local/lib/
	sudo cp $(HEADERS) /usr/local/include/audioanalysis/
	@echo "📦 Library installed to /usr/local/"

# Uninstall
uninstall:
	sudo rm -f /usr/local/lib/$(LIBRARY)
	sudo rm -rf /usr/local/include/audioanalysis
	@echo "🗑️  Library uninstalled"

# Check dependencies
check-deps:
	@echo "🔍 Checking for required dependencies..."
	@pkg-config --exists sdl2 || (echo "❌ SDL2 not found. Install with: sudo apt-get install libsdl2-dev" && exit 1)
	@pkg-config --exists libmpg123 || (echo "❌ libmpg123 not found. Install with: sudo apt-get install libmpg123-dev" && exit 1)
	@echo "✅ All dependencies found!"

# Package creation
package: $(LIBRARY) $(HEADERS)
	mkdir -p audioanalysis-package/lib
	mkdir -p audioanalysis-package/include
	mkdir -p audioanalysis-package/examples
	cp $(LIBRARY) audioanalysis-package/lib/
	cp $(HEADERS) audioanalysis-package/include/
	cp $(DEMO).cpp audioanalysis-package/examples/
	cp README_MP3_Player.md audioanalysis-package/
	tar -czf audioanalysis-library.tar.gz audioanalysis-package/
	rm -rf audioanalysis-package/
	@echo "📦 Package created: audioanalysis-library.tar.gz"

# Development targets
debug: CXXFLAGS += -g -DDEBUG
debug: clean all

# Run the demo with a test file
test: $(DEMO)
	@echo "🎵 To test the library, run: ./$(DEMO) your_song.mp3"

# Show library information
info:
	@echo "AudioAnalysis MP3 Player Library"
	@echo "================================"
	@echo "Library: $(LIBRARY)"
	@echo "Demo: $(DEMO)"
	@echo "Sources: $(SOURCES)"
	@echo "Headers: $(HEADERS)"
	@echo ""
	@echo "Usage Examples:"
	@echo "  Build library: make"
	@echo "  Install: make install"
	@echo "  Test demo: ./$(DEMO) song.mp3"
	@echo "  Create package: make package"

# Example of how to use the library in other projects
example-usage:
	@echo "📚 How to use this library in your projects:"
	@echo ""
	@echo "1. Include the header:"
	@echo "   #include \"audioanalysis/mp3_player.hpp\""
	@echo ""
	@echo "2. Link against the library:"
	@echo "   g++ -std=c++17 your_code.cpp -laudioanalysis -lSDL2 -lmpg123 -lm"
	@echo ""
	@echo "3. Use the namespace:"
	@echo "   AudioAnalysis::MP3Player player;"
	@echo ""
	@echo "4. See demo.cpp for complete examples!"

# Help target
help:
	@echo "Available targets:"
	@echo "  all         - Build library and demo (default)"
	@echo "  $(LIBRARY)  - Build only the static library"
	@echo "  $(DEMO)     - Build only the demo program"
	@echo "  clean       - Remove build files"
	@echo "  install     - Install library to system"
	@echo "  uninstall   - Remove library from system"
	@echo "  package     - Create distribution package"
	@echo "  check-deps  - Check for required dependencies"
	@echo "  debug       - Build with debug symbols"
	@echo "  test        - Show how to test the demo"
	@echo "  info        - Show library information"
	@echo "  example-usage - Show how to use in other projects"
	@echo "  help        - Show this help message"

.PHONY: all clean install uninstall check-deps package debug test info example-usage help 
