#!/bin/sh
# ports/chromium/crstart — the Exec= entry of Chromium.aop (#1141). Installed
# into the bundle as Contents/AOS/crstart by ports/chromium/package.sh, beside
# content_shell and its resources.
#
# WHAT THIS BROWSER IS. Chromium 152.0.7977.82's content_shell -- Blink, V8,
# the network stack and Chromium's own views toolbar (Back/Forward/Refresh/
# Stop and an address field) -- on the NATIVE Fasadas Ozone backend
# (ports/chromium/ozone/fasadas), with GPU output through ANGLE on the system
# Mesa EGL when the machine has a driver for it (#1330: iris on the
# MacBook10,1) and the software canvas otherwise. It is NOT the full `chrome`
# target: no tabs, bookmarks, history UI, password manager or extensions
# (#1141's successor issue). It identifies as AnotherOS (patches 0003, 0024),
# never Linux, and its audio goes through Garsas via libgarsas (patch 0019);
# nothing here opens /dev/dsp.
#
# WHY A LAUNCHER, AND WHY IT EXECS. The dock, Launchpad and the Finder start a
# bundle through aoprun with no environment beyond LD_LIBRARY_PATH/AOP_BUNDLE.
# The switches below are what the browser needs on this OS, and every one is
# a measured requirement, not a preference:
#   --ozone-platform=fasadas  the native backend. It is also the compiled-in
#                         default since the 2026-10-12 build (gen-aos.sh
#                         OZONE_DEFAULT), but a bundle must not depend on
#                         which build it carries: an older binary defaulted to
#                         HEADLESS and would start with no window at all.
#   --no-sandbox          the kernel has no seccomp-bpf and no namespaces
#                         (#345, #1150): the Linux sandbox cannot exist here,
#                         and Chromium refuses to start without either it or
#                         this switch. That is a stated security property of
#                         this build, not a hidden one -- #345 tracks it.
#   --no-zygote           AnotherOS launches children by fork+exec (patch
#                         0010); there is no zygote to talk to.
#   --in-process-gpu      the Fasadas surface belongs to the compositor
#                         connection that created it, which is the BROWSER's
#                         (#1143 stage A). The GPU-process buffer handoff
#                         (stage B) does not exist yet, so viz must run in the
#                         browser process; every hardware arm of #1330 ran so.
# `exec` keeps the PID: the dock tracks the process aoprun started, and a
# shell left between the dock and the browser would break its pid tracking
# and launch feedback (the reason aop-package.sh prefers qt.conf to wrappers).
#
# The window size is in DIPs: 1280x800 fits the MacBook10,1's 1440x878-point
# work area at scale 2 (content_shell's own default is 800x600). A smaller
# panel sets CHROMIUM_WINDOW_SIZE. content_shell keeps its profile in
# $XDG_CONFIG_HOME/content_shell (~/.config/content_shell; shell_paths.cc,
# patch 0012), so cookies and logins persist across launches.
set -u

HERE=$(cd "$(dirname "$0")" && pwd)

export FASADAS_SOCKET="${FASADAS_SOCKET:-/run/fasadas-0}"
export XDG_RUNTIME_DIR="${XDG_RUNTIME_DIR:-/run}"
export FONTCONFIG_PATH="${FONTCONFIG_PATH:-/etc/fonts}"
: "${HOME:=/Users/$(id -un 2>/dev/null || echo root)}"
export HOME
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"

# One log per launch, kept beside the profile; the previous one is kept once,
# so a crash that the user reports after relaunching still has its evidence.
LOGDIR="$XDG_CACHE_HOME/chromium"
mkdir -p "$LOGDIR" 2>/dev/null || LOGDIR=/tmp
[ -f "$LOGDIR/chromium.log" ] && mv -f "$LOGDIR/chromium.log" "$LOGDIR/chromium.log.1" 2>/dev/null

# The start page ships in the bundle: no network fetch decides what the first
# window shows, and an offline machine still opens a page that works.
START="${CHROMIUM_HOMEPAGE:-file://$HERE/start.html}"
[ $# -gt 0 ] && START=

exec "$HERE/content_shell" \
    --ozone-platform=fasadas \
    --no-sandbox \
    --no-zygote \
    --in-process-gpu \
    --content-shell-host-window-size="${CHROMIUM_WINDOW_SIZE:-1280x800}" \
    --enable-logging=stderr --v=0 \
    ${CHROMIUM_FLAGS:-} \
    ${START:+"$START"} "$@" >>"$LOGDIR/chromium.log" 2>&1
