# NetSurf for MontaukOS -- monkey frontend
#
# Upstream: https://github.com/netsurf-browser/netsurf (official mirror of
# git.netsurf-browser.org; submodule at upstream/, pinned).
#
# monkey is NetSurf's headless, script-driven frontend: it drives the full
# browser core (HTML parse -> CSS cascade -> layout) and reports through a
# line protocol on stdout instead of drawing. That makes it the right first
# target -- no graphics, no fonts, and no fetcher required.
#
# CONFIGURATION
#   NETSURF_USE_CURL=NO      no libcurl port; the built-in file/about/data
#                            fetchers remain, which is enough for file:// URLs.
#                            fetch_montauk will register alongside them later.
#   NETSURF_USE_OPENSSL=NO   reached only through curl. BearSSL is the eventual
#                            answer, via the fetcher rather than through here.
#   NETSURF_USE_DUKTAPE=NO   no JavaScript; also avoids the nsgenbind host tool.
#   NETSURF_USE_PNG/JPEG/    no ports for these codecs yet. GIF and BMP are on,
#     JPEGXL/WEBP=NO         since libnsgif and libnsbmp are in the sysroot.
#   NETSURF_FS_BACKING_STORE=YES  NOT optional for monkey: frontends/monkey/
#                            main.c references filesystem_llcache_table
#                            unconditionally, and that lives in
#                            content/fs_backing_store.c. The config.h patch
#                            already tells NetSurf we lack dirfd/unlinkat/
#                            fstatat, so it uses its portable fallbacks.
#   NETSURF_USE_LIBICONV_PLUG=YES means "iconv is already available, do not
#                            link -liconv" -- true here, since compat/iconv.c
#                            provides it inside libnscompat.a.
#
# COMPAT SHIMS (compat/) stand in for headers MontaukOS has no equivalent of:
# sockets, select, inet, and a stub <curl/curl.h> that exists only because
# content/fetch.c includes curl's header unguarded. See those files -- each
# says what it does and does not do.
#
# HOST TOOLS: perl, python3. (gperf is needed by libhubbub, not here.)

UPSTREAM := upstream
# Absolute: the sub-make runs with -C upstream, so a relative include path
# would resolve against the checkout rather than this port.
COMPAT   := $(CURDIR)/compat

PORT_NAME := netsurf

include ../../mk/port.mk

# NetSurf's own Makefile derives HOST from uname and picks its own build
# directory, so BUILDDIR/HOST from port.mk do not apply here. The binary is
# dropped at the top of the checkout, not inside build/.
NS_BUILD_OUT := $(UPSTREAM)/nsmonkey

# Where the binary and its resources land on MontaukOS. NetSurf cannot start
# without its resources (Messages, default.css, quirks.css ...), so the
# resource path is compiled in and the tree is installed alongside.
NS_RESPATH  := 0:/os/netsurf/
NS_INSTALL  := $(MONTAUKOS)/programs/bin/os

# libutf8proc installs its header to include/libutf8proc/ but its .pc
# advertises only -I${includedir}, while utils/idna.c includes <utf8proc.h>.
# Add the subdirectory here rather than staging a duplicate copy of the header.
NETSURF_CFLAGS := \
    $(PORT_CFLAGS_COMMON) \
    -I $(COMPAT) \
    -I $(PORT_SYSROOT_INC)/libutf8proc

NETSURF_ARGS := \
    TARGET=monkey \
    NSSHARED=$(NS_BUILDSYSTEM) \
    CC=$(CC) \
    AR=$(AR) \
    NETSURF_USE_CURL=NO \
    NETSURF_USE_OPENSSL=NO \
    NETSURF_USE_DUKTAPE=NO \
    NETSURF_USE_PNG=NO \
    NETSURF_USE_JPEG=NO \
    NETSURF_USE_JPEGXL=NO \
    NETSURF_USE_WEBP=NO \
    NETSURF_USE_VIDEO=NO \
    NETSURF_USE_LIBICONV_PLUG=YES \
    NETSURF_MONKEY_RESOURCES=$(NS_RESPATH) \
    NETSURF_FS_BACKING_STORE=YES

# compat/iconv.c is the only shim needing a translation unit of its own (the
# rest are header-only). It goes into a small archive placed FIRST in LDFLAGS:
# NetSurf appends its own pkg-config libraries after the environment's value,
# so -lparserutils lands to the right of this archive and resolves the symbols
# it references.
COMPAT_LIB := $(CURDIR)/compat/libnscompat.a

# NetSurf formats floats (page load time in the status bar, plot coordinates).
# The Montauk libc keeps %e/%f/%g in a separate archive member reached by a
# WEAK reference, and a weak undefined ref does not extract a member -- without
# this the status bar reads "Done (%fs)". See programs/lib/libc/printf_float.c.
NS_LDFLAGS := $(COMPAT_LIB) -Wl,-u,_pf_putfloat

$(COMPAT_LIB): compat/iconv.c compat/iconv.h
	$(CC) $(NETSURF_CFLAGS) -std=gnu99 -c compat/iconv.c -o compat/iconv.o
	$(AR) rcs $@ compat/iconv.o
	@echo "Built: $@"

.PHONY: all clean netsurf-build
all: netsurf-build

netsurf-build: $(COMPAT_LIB)
	@$(call ns_patch,$(UPSTREAM))
	CFLAGS="$(NETSURF_CFLAGS)" LDFLAGS="$(NS_LDFLAGS)" $(NS_ENV) \
	    $(NS_MAKE) -C $(UPSTREAM) $(NETSURF_ARGS)
	@test -f $(NS_BUILD_OUT) || { echo "netsurf: expected $(NS_BUILD_OUT)" >&2; exit 1; }
	@echo "Built: $(NS_BUILD_OUT) ($$(stat -c%s $(NS_BUILD_OUT)) bytes)"

# Install as a MontaukOS system program plus its resource tree. Not an app
# bundle: monkey is a console REPL with no GUI, so it carries no manifest and
# no .port marker -- remember to uninstall before cutting a release ISO.
.PHONY: install uninstall
install: netsurf-build
	mkdir -p $(NS_INSTALL)/netsurf
	cp $(NS_BUILD_OUT) $(NS_INSTALL)/nsmonkey.elf
	# -L, not -r alone: nearly everything in a NetSurf frontend res/ dir is a
	# symlink into ../../../resources/. Copied as links they dangle outside
	# the installed tree, and the ramdisk turns a symlink into a ZERO-LENGTH
	# FILE (Fs/Ramdisk.cpp only handles typeflag '5'), so default.css and
	# welcome.html arrive empty and every page lays out to nothing.
	cp -rL $(UPSTREAM)/frontends/monkey/res/. $(NS_INSTALL)/netsurf/
	@echo "installed: 0:/os/nsmonkey.elf + 0:/os/netsurf/ resources"

uninstall:
	rm -rf $(NS_INSTALL)/nsmonkey.elf $(NS_INSTALL)/netsurf
	@echo "removed nsmonkey.elf and its resources"

clean:
	@$(NS_ENV) $(NS_MAKE) -C $(UPSTREAM) $(NETSURF_ARGS) clean >/dev/null 2>&1 || true
	@git -C $(UPSTREAM) checkout -- . 2>/dev/null || true
	rm -rf $(UPSTREAM)/build-Linux-monkey
