RUST_LIB = eagle-core/target/release/libeagle_core.a
SWIFT_SOURCES = $(wildcard Eagle/Sources/Eagle/*.swift)
BUILD_DIR = build
APP = $(BUILD_DIR)/Eagle.app
APP_BINARY = $(APP)/Contents/MacOS/Eagle

.PHONY: all clean rust swift run test

all: $(APP)

rust: $(RUST_LIB)

$(RUST_LIB): eagle-core/src/*.rs eagle-core/Cargo.toml
	cd eagle-core && cargo build --release

$(APP): $(RUST_LIB) $(SWIFT_SOURCES) Eagle/BridgingHeader.h Eagle/Info.plist
	@mkdir -p $(APP)/Contents/MacOS
	@mkdir -p $(APP)/Contents/Resources
	@cp Eagle/Info.plist $(APP)/Contents/Info.plist
	swiftc \
		-O \
		-import-objc-header Eagle/BridgingHeader.h \
		-L eagle-core/target/release \
		-leagle_core \
		-framework AppKit \
		-framework SwiftUI \
		-framework UniformTypeIdentifiers \
		$(SWIFT_SOURCES) \
		-o $(APP_BINARY)

run: $(APP)
	open $(APP)

test: test-swift test-rust

test-swift:
	swiftc Eagle/Sources/Eagle/ContentExtraction.swift Eagle/Tests/ContentExtractionTests.swift -o $(BUILD_DIR)/tests
	$(BUILD_DIR)/tests

test-rust:
	cd eagle-core && cargo test

clean:
	rm -rf $(BUILD_DIR)
	cd eagle-core && cargo clean
