# shell-agent - UniFFI bridge + SwiftUI app
# Created by Vetcoders (c)2026 LibraxisAI

export MACOSX_DEPLOYMENT_TARGET ?= 14.0

.PHONY: help bindings xcode app app-debug dmg dmg-signed notarize release

HELP_C_CYAN   := \033[36m
HELP_C_GREEN  := \033[32m
HELP_C_YELLOW := \033[33m
HELP_C_RESET  := \033[0m

bindings:
	@echo "Building vibecrafted-shell-ffi (release)..."
	@# cargo's default release strip (rust-objcopy) corrupts the .rustc metadata
	@# section of proc-macro dylibs when MACOSX_DEPLOYMENT_TARGET is below the SDK
	@# default on the Xcode-beta toolchain: the dylib signs fine but every consumer
	@# dies with E0463 "can't find crate". The release pipeline strips payloads
	@# itself (scripts/lib/macho-signing.sh), so cargo-level strip is redundant.
	@CARGO_PROFILE_RELEASE_STRIP=false cargo build -p vibecrafted-shell-ffi --release
	@./scripts/fix-dylib-install-names.sh
	@echo "Generating Swift bindings..."
	@cargo run -p vibecrafted-uniffi-bindgen -- generate --library ../target/release/libvibecrafted_shell_ffi.dylib --language swift --out-dir app/Vibecrafted/Bridge/
	@./scripts/normalize-bindings.sh app/Vibecrafted/Bridge
	@echo "Bindings ready: app/Vibecrafted/Bridge/"

xcode:
	@cd app && xcodegen generate
	@echo "Xcode project generated: app/Vibecrafted.xcodeproj"

app: bindings xcode
	@echo "Building Vibecrafted.app..."
	@mkdir -p build
	@rm -rf build/Vibecrafted.app build/VibecraftedDev.app
	@set -o pipefail && xcodebuild -project app/Vibecrafted.xcodeproj -scheme Vibecrafted -configuration Debug -derivedDataPath build/DerivedData build 2>&1 | tail -20
	@BUILT_APP=$$(find build/DerivedData \( -name VibecraftedDev.app -o -name 'Vibecrafted Dev.app' -o -name Vibecrafted.app \) -type d | head -1); \
		if [ -z "$$BUILT_APP" ]; then \
			echo "ERROR: debug App bundle not found in build/DerivedData"; \
			exit 1; \
		fi; \
		rm -rf build/VibecraftedDev.app build/Vibecrafted.app; \
		cp -R "$$BUILT_APP" build/VibecraftedDev.app
	@/usr/libexec/PlistBuddy -c "Set :CFBundleIconFile Vibecrafted.icns" build/VibecraftedDev.app/Contents/Info.plist 2>/dev/null || /usr/libexec/PlistBuddy -c "Add :CFBundleIconFile string Vibecrafted.icns" build/VibecraftedDev.app/Contents/Info.plist
	@mkdir -p build/VibecraftedDev.app/Contents/Resources
	@../../scripts/build-vibecrafted-icon.sh ../../docs/presence/logo-master.png build/VibecraftedDev.app/Contents/Resources/Vibecrafted.icns
	@! find build/VibecraftedDev.app/Contents/Resources -maxdepth 1 -type f -name '*.icns' ! -name 'Vibecrafted.icns' -print -quit | grep -q .
	@./scripts/fix-dylib-install-names.sh build/VibecraftedDev.app
	@codesign --force --deep --options runtime --sign - build/VibecraftedDev.app
	@codesign --force --options runtime --entitlements app/Vibecrafted/Vibecrafted.entitlements --sign - build/VibecraftedDev.app
	@echo "App built: build/VibecraftedDev.app (io.vetcoders.vibecrafted.dev)"

dmg:
	@$(MAKE) -C ../.. dmg

dmg-signed:
	@$(MAKE) -C ../.. dmg-signed

notarize:
	@$(MAKE) -C ../.. notarize

release:
	@$(MAKE) -C ../.. release

app-debug: app

help:
	@printf "\n$(HELP_C_CYAN)shell-agent$(HELP_C_RESET) - UniFFI bridge + SwiftUI app\n\n"
	@printf "  $(HELP_C_YELLOW)BUILD$(HELP_C_RESET)\n"
	@printf "    $(HELP_C_GREEN)%-14s$(HELP_C_RESET) %s\n" "bindings" "Build Rust FFI and generate Swift bindings"
	@printf "    $(HELP_C_GREEN)%-14s$(HELP_C_RESET) %s\n" "xcode" "Generate app/Vibecrafted.xcodeproj"
	@printf "    $(HELP_C_GREEN)%-14s$(HELP_C_RESET) %s\n" "app" "Build and ad-hoc codesign Vibecrafted.app"
	@printf "    $(HELP_C_GREEN)%-14s$(HELP_C_RESET) %s\n" "app-debug" "Build the same native App debug target"
	@printf "\n  $(HELP_C_YELLOW)PACKAGE$(HELP_C_RESET)\n"
	@printf "    $(HELP_C_GREEN)%-14s$(HELP_C_RESET) %s\n" "dmg" "Build unsigned/ad-hoc DMG"
	@printf "    $(HELP_C_GREEN)%-14s$(HELP_C_RESET) %s\n" "dmg-signed" "Build signed DMG"
	@printf "    $(HELP_C_GREEN)%-14s$(HELP_C_RESET) %s\n" "notarize" "Delegate notarization to the product owner"
	@printf "    $(HELP_C_GREEN)%-14s$(HELP_C_RESET) %s\n" "release" "Delegate the single public release to the product owner"
