PLUGIN_NAME := Just a Sample
BUILD_DIR   := out/build/macos
CMAKE_FLAGS := -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release \
               -DCMAKE_OSX_ARCHITECTURES="arm64" \
               -DCMAKE_OSX_DEPLOYMENT_TARGET="10.13" \
               -DJAS_ENABLE_AVX2=OFF

NPROC := $(shell sysctl -n hw.ncpu 2>/dev/null || echo 4)

VST3_SRC := $(BUILD_DIR)/JustASample_artefacts/Release/VST3/$(PLUGIN_NAME).vst3
AU_SRC   := $(BUILD_DIR)/JustASample_artefacts/Release/AU/$(PLUGIN_NAME).component

VST3_DST := $(HOME)/Library/Audio/Plug-Ins/VST3/$(PLUGIN_NAME).vst3
AU_DST   := $(HOME)/Library/Audio/Plug-Ins/Components/$(PLUGIN_NAME).component

# ---------- phony targets ----------
.PHONY: all install uninstall clean clear-cache help configure build build-au build-all

all: install

configure: $(BUILD_DIR)/Makefile

$(BUILD_DIR)/Makefile:
	cmake -B $(BUILD_DIR) $(CMAKE_FLAGS)

build: configure
	cmake --build $(BUILD_DIR) --target JustASample_VST3 -j$(NPROC)

build-au: configure
	cmake --build $(BUILD_DIR) --target JustASample_AU -j$(NPROC)

build-all: configure
	cmake --build $(BUILD_DIR) --target JustASample_All -j$(NPROC)

FL_CACHE := $(HOME)/Library/Caches/com.image-line.flstudio

install: install-vst3 install-au
	@rm -rf "$(FL_CACHE)"
	@echo "Cleared FL Studio plugin cache"

install-vst3: build
	@mkdir -p "$(VST3_DST)"
	rm -rf "$(VST3_DST)"
	cp -R "$(VST3_SRC)" "$(VST3_DST)"
	@echo "Installed VST3 to $(VST3_DST)"

install-au: build-au
	@mkdir -p "$(AU_DST)"
	rm -rf "$(AU_DST)"
	cp -R "$(AU_SRC)" "$(AU_DST)"
	@echo "Installed AU to $(AU_DST)"

install-all: build-all install-vst3 install-au

uninstall:
	rm -rf "$(VST3_DST)" "$(AU_DST)"
	@rm -rf "$(FL_CACHE)"
	@echo "Removed plugin and cleared FL Studio cache"

clean:
	rm -rf $(BUILD_DIR) .deps
	@rm -rf "$(FL_CACHE)"

clear-cache:
	@rm -rf "$(FL_CACHE)"
	@echo "Cleared FL Studio plugin cache"

help:
	@echo "Targets:"
	@echo "  make            - build and install VST3 + AU (default)"
	@echo "  make build      - build VST3 only"
	@echo "  make build-au   - build AU only"
	@echo "  make build-all  - build all plugin formats"
	@echo "  make install    - install VST3 + AU"
	@echo "  make install-vst3 - install VST3 only"
	@echo "  make install-au - install AU only"
	@echo "  make install-all - install all formats"
	@echo "  make uninstall  - remove all installed copies"
	@echo "  make clean      - remove build directory and FL Studio cache"
	@echo "  make clear-cache - clear FL Studio plugin cache"
	@echo "  make help       - show this message"
