# Sizzle for iOS/iPadOS — build entrypoint (separate from the Android Makefile).
#
# For now this tree holds only the TrollStore AUv3 spike (spike/): a host app plus
# one sine-wave AUv3 instrument and an App Group, built to answer "do hosts see
# AUv3 plugins from a TrollStore-installed app?" before any real porting.
#
#   make help        what each target does
#   make project     generate spike/SizzleSpike.xcodeproj from spike/project.yml
#   make sim         build the spike for the iOS Simulator
#   make sim-run     install + launch it on a booted simulator (self-test runs at launch)
#   make ipa         unsigned device build, ad-hoc signed with entitlements, packed as
#                    dist/cc.tiredsysadmin.ios.sizzlespike_<build>.ipa for TrollStore
#   make clean       remove build/ and dist/
#
# Needs Xcode and XcodeGen (brew install xcodegen). Builds stay local (CLAUDE.md).

SHELL      := /bin/sh
SPIKE      := spike
PROJECT    := $(SPIKE)/SizzleSpike.xcodeproj
SCHEME     := SizzleSpike
APP_ID     := cc.tiredsysadmin.ios.sizzlespike
BUILD_NO   := $(shell sed -n 's/^ *CURRENT_PROJECT_VERSION: *"\(.*\)"/\1/p' $(SPIKE)/project.yml)
SOURCES    := $(SPIKE)/project.yml $(wildcard $(SPIKE)/*/*.swift $(SPIKE)/*/*.plist $(SPIKE)/*/*.entitlements)

SIM_APP    := build/sim/Build/Products/Debug-iphonesimulator/$(SCHEME).app
DEV_APP    := build/dev/Build/Products/Release-iphoneos/$(SCHEME).app
IPA        := dist/$(APP_ID)_$(BUILD_NO).ipa

.DEFAULT_GOAL := help

help:
	@sed -n 's/^#   //p' Makefile

project: $(PROJECT)/project.pbxproj
$(PROJECT)/project.pbxproj: $(SPIKE)/project.yml
	cd $(SPIKE) && xcodegen generate --spec project.yml --quiet

# Simulator: Xcode's "sign to run locally" (-) embeds the entitlements, so the
# App Group works there too.
sim: $(SIM_APP)
$(SIM_APP): $(PROJECT)/project.pbxproj $(SOURCES)
	xcodebuild -quiet -project $(PROJECT) -scheme $(SCHEME) -configuration Debug \
		-sdk iphonesimulator -derivedDataPath build/sim \
		CODE_SIGN_IDENTITY=- CODE_SIGN_STYLE=Manual build

sim-run: $(SIM_APP)
	xcrun simctl install booted $(SIM_APP)
	xcrun simctl launch booted $(APP_ID)

# Device: build without signing, then ad-hoc sign inside-out (extension first)
# with each target's entitlements. TrollStore re-signs at install time but keeps
# the entitlements it finds, which is how the App Group survives.
ipa: $(IPA)
$(IPA): $(PROJECT)/project.pbxproj $(SOURCES)
	xcodebuild -quiet -project $(PROJECT) -scheme $(SCHEME) -configuration Release \
		-sdk iphoneos -derivedDataPath build/dev CODE_SIGNING_ALLOWED=NO build
	rm -rf build/Payload && mkdir -p build/Payload dist
	cp -R $(DEV_APP) build/Payload/
	codesign -f -s - --entitlements $(SPIKE)/AU/AU.entitlements \
		build/Payload/$(SCHEME).app/PlugIns/SizzleSpikeAU.appex
	codesign -f -s - --entitlements $(SPIKE)/App/App.entitlements \
		build/Payload/$(SCHEME).app
	rm -f $@ && cd build && zip -qr ../$@ Payload
	@echo "wrote $@"

clean:
	rm -rf build dist

.PHONY: help project sim sim-run ipa clean
