# Sourced by the desk-* scripts: the environment the Pomelo desktop needs.
# SPDX-License-Identifier: MIT
# Copyright (C) 2026 pake <pake@fedora.tw> - https://pomelo-os.org
export PATH=/usr/bin:/bin
if [ -z "${DESK_USER_SESSION:-}" ]; then   # (a logged-in user's session, docs/LOGIN.md, keeps the HOME / XDG_* pomelo-login gave it)
export HOME=/root
export XDG_RUNTIME_DIR=/tmp/xdg-runtime
export XDG_CONFIG_HOME=/root/.config
export XDG_CACHE_HOME=/tmp/cache
export LANG=zh_TW.UTF-8
fi
export LANG=${LANG:-zh_TW.UTF-8}
# wlroots: software rendering, no GPU; the session/seat without logind or seatd.
export WLR_RENDERER=pixman
export WLR_RENDERER_ALLOW_SOFTWARE=1
export LIBSEAT_BACKEND=${LIBSEAT_BACKEND:-builtin}
export SEATD_VTBOUND=0
export WLR_DRM_NO_ATOMIC=1
export WLR_NO_HARDWARE_CURSORS=1
export POMELO_WLR_DEBUG=1
export DESK_LOG_LINES=12
export XKB_DEFAULT_LAYOUT=us
if [ -z "${DESK_USER_SESSION:-}" ]; then
mkdir -p "$XDG_RUNTIME_DIR" "$XDG_CONFIG_HOME" "$XDG_CACHE_HOME"
chmod 700 "$XDG_RUNTIME_DIR"
fi
# udev database for libinput (evdev devices exist only with virtio-input or USB input attached).
# pomelo-udevd (main tree, in the initramfs) does coldplug, then keeps handling hot-plug events;
# desk-udev is a minimal fallback that only writes the input tags once.
if [ -z "${DESK_USER_SESSION:-}" ]; then   # root only: device database
{ echo "mark udev-coldplug-start" > /proc/pomelo_splash; } 2>/dev/null
# (pomelo-udevd lives in /usr/sbin, which is not on this script's PATH: `command -v` missed it on the deb-based image and the shell fallback ran,
#  36 s on a one-CPU emulated machine, and it also mis-tagged USB/VirtualBox tablets)
UDEVD=; for d in /usr/sbin /sbin /usr/bin /bin; do [ -x $d/pomelo-udevd ] && { UDEVD=$d/pomelo-udevd; break; }; done
if [ -n "$UDEVD" ]; then "$UDEVD" --coldplug-only; else /usr/bin/desk-udev; fi
{ echo "mark udev-coldplug-done" > /proc/pomelo_splash; } 2>/dev/null
fi
# TLS engine of the Windows layer (docs/TLS.md in the kernel tree): winhttp / wininet / schannel (winsys.dll) talk to pomelo-tlsd over
# /run/pomelo-tlsd.sock (or /tmp/pomelo-tlsd.sock when /run is read-only).  The binary comes with the kernel's initramfs (/bin), the Mozilla
# CA bundle with this image (/etc/ssl/certs/ca-certificates.crt, MPL-2.0).  Linux programs (Mono, .NET, curl) find the same bundle.
if command -v pomelo-tlsd >/dev/null 2>&1 && ! [ -S /run/pomelo-tlsd.sock -o -S /tmp/pomelo-tlsd.sock ]; then
    pomelo-tlsd >/dev/null 2>&1 &
fi
[ -f /etc/ssl/certs/ca-certificates.crt ] && export SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
# With WLR_LIBINPUT_NO_DEVICES set, wlroots skips its first look at the input devices and only notices
# them when the first key arrives (which is then lost).  Set it only when there really is none.
[ -e /dev/input/event0 ] || export WLR_LIBINPUT_NO_DEVICES=1
# How long the desk-* scripts keep the desktop up before shutting down (seconds).
export DESK_RUN_SECONDS=${DESK_RUN_SECONDS:-900}
# Mesa software GL (only used when the image was built WITH_MESA=1): the root file system is read-only, so every shader-cache
# write attempt is wasted work (measured: first context 0.84 s -> 0.78 s, first draw sequence -0.25 s).
export MESA_SHADER_CACHE_DISABLE=true
export LIBGL_ALWAYS_SOFTWARE=1
# glenv=A=1,B=2 on the kernel command line exports extra environment variables (used by the OpenGL tests).
read -r _cmdline < /proc/cmdline     # (builtin: no fork)
for w in $_cmdline; do
    case "$w" in glenv=*) IFS=,; for kv in ${w#glenv=}; do export "$kv"; done; unset IFS;; esac
done
# Web browser (pomelo-browser = GTK4 + WebKitGTK 6.0, see docs/BROWSER.md).  The guest has no user namespaces/seccomp (bubblewrap
# sandbox off), a W^X kernel (JavaScriptCore's JIT needs RWX pages: interpreter only), no GPU (software drawing), no D-Bus daemon.
if [ -x /usr/bin/pomelo-browser ]; then
    export WEBKIT_DISABLE_SANDBOX_THIS_IS_DANGEROUS=1
    # JavaScriptCore's JIT: decided by the WebKitWebProcess launcher (src/wkproc.c) from the browser setting browser.jit; an explicit
    # JSC_useJIT=... in the environment (glenv=) wins.  The kernel is W^X unless a process opts in with prctl(PR_POMELO_ALLOW_WX).
    export WEBKIT_DISABLE_COMPOSITING_MODE=${WEBKIT_DISABLE_COMPOSITING_MODE:-1}
    export WEBKIT_DISABLE_DMABUF_RENDERER=${WEBKIT_DISABLE_DMABUF_RENDERER:-1}
    export GSK_RENDERER=${GSK_RENDERER:-cairo}
    export WEBKIT_SKIA_ENABLE_CPU_RENDERING=${WEBKIT_SKIA_ENABLE_CPU_RENDERING:-1}
    export GDK_BACKEND=wayland
    export LC_CTYPE=C.UTF-8
    export GSETTINGS_BACKEND=memory
    export GIO_USE_VFS=local
    export GIO_MODULE_DIR=/usr/lib/pomelo/ubuntu/gio/modules
    export NO_AT_BRIDGE=1
    export GTK_A11Y=none
    export WEBKIT_EXEC_PATH=/usr/lib/x86_64-linux-gnu/webkitgtk-6.0
fi

# --- package-based image (docs/APT-DESKTOP.md): the Ubuntu closure lives in /usr/lib/pomelo/ubuntu, a few libraries override the base's ---
export LIBGL_DRIVERS_PATH=/usr/lib/pomelo/ubuntu/dri GBM_BACKENDS_PATH=/usr/lib/pomelo/ubuntu/gbm
