# Makefile - MontaukOS ports tree
#
#   make                 Build every port into out/apps/<name>/
#   make doom            Build a single port (shorthand for its category path)
#   make games-fps/doom  Same, by full path
#   make install         Copy built bundles into the MontaukOS ramdisk tree
#   make clean           Remove build artifacts
#
# Ports build against a MontaukOS checkout; override its location with
# MONTAUKOS=/path/to/MontaukOS (default ~/dev/MontaukOS).

MAKEFLAGS += -rR
.SUFFIXES:

MTKPORTS_ROOT := $(CURDIR)
export MTKPORTS_ROOT

MONTAUKOS ?= $(HOME)/dev/MontaukOS
export MONTAUKOS

OUTDIR := $(MTKPORTS_ROOT)/out
export OUTDIR

# Ports to build, as <category>/<port>, in dependency order: library ports
# come before the applications that link them. Categories follow Gentoo
# naming (dev-libs, media-libs, net-libs, www-client, games-fps, ...).
PORTS := \
    dev-libs/zlib \
    dev-libs/libwapcaplet \
    dev-libs/libparserutils \
    games-fps/doom

# Bundle directories are keyed by port name, not category path.
PORT_NAMES := $(notdir $(PORTS))

.PHONY: all clean install $(PORTS) $(PORT_NAMES)

all: $(PORTS)

$(PORTS):
	$(MAKE) -C $@

# Shorthand target per port name, so `make doom` works as well as
# `make games-fps/doom`.
define PORT_ALIAS
$(notdir $(1)): $(1)
endef
$(foreach p,$(PORTS),$(eval $(call PORT_ALIAS,$(p))))

# Drop finished bundles into the OS ramdisk staging tree. install_apps.sh
# builds in-tree app bundles at the same path; ports simply add to it.
install: all
	@mkdir -p $(MONTAUKOS)/programs/bin/apps
	@for p in $(PORTS); do \
	    n=$$(basename $$p); \
	    if [ -d "$(OUTDIR)/apps/$$n" ]; then \
	        cp -r "$(OUTDIR)/apps/$$n" "$(MONTAUKOS)/programs/bin/apps/"; \
	        echo "installed port: $$p"; \
	    fi; \
	done
	@echo "ports: installed into $(MONTAUKOS)/programs/bin/apps/"
	@echo "ports: run 'make ramdisk' (or 'make') in $(MONTAUKOS) to rebuild the image"

clean:
	@for p in $(PORTS); do $(MAKE) -C $$p clean; done
	rm -rf $(OUTDIR)
