#!/bin/sh
# init=/usr/bin/desk-gl : compositor with the GLES2 renderer on Mesa software rendering (llvmpipe), then pomelo-calc.
# Kernel command line  glenv=A=1,B=2  adds environment (read by desk-env), e.g. WLR_RENDERER=pixman, GALLIUM_DRIVER=softpipe,
# GL_DEBUG=1 (verbose EGL/Mesa/wlroots logging: slow, every serial line costs ~90 ms).
. /usr/bin/desk-env
export WLR_RENDERER=${WLR_RENDERER_GL:-gles2}
export LIBGL_ALWAYS_SOFTWARE=1
if [ -n "${GL_DEBUG:-}" ]; then export EGL_LOG_LEVEL=debug MESA_DEBUG=1 LIBGL_DEBUG=verbose WLR_DEBUG=1; else export POMELO_WLR_DEBUG=0; fi
up() { read u _ < /proc/uptime; echo $u; }
echo "desk-gl: WLR_RENDERER=$WLR_RENDERER GALLIUM_DRIVER=${GALLIUM_DRIVER:-auto} LP_NUM_THREADS=${LP_NUM_THREADS:-auto} uptime=$(up)"
/usr/bin/pomelo-compositor 2>/tmp/compositor.log &
cpid=$!
t0=$(up)
i=0
while [ $i -lt 1500 ] && [ ! -S "$XDG_RUNTIME_DIR/wayland-0" ] && kill -0 $cpid 2>/dev/null; do sleep 0.1; i=$((i+1)); done
echo "desk-gl: compositor start at $t0, wayland socket up at $(up) (uptime seconds)"
if [ -S "$XDG_RUNTIME_DIR/wayland-0" ]; then
    export WAYLAND_DISPLAY=wayland-0
    /usr/bin/pomelo-calc 2>/tmp/calc.log &
    kpid=$!
    sleep ${DESK_RUN_SECONDS:-15}
    echo "desk-gl: compositor alive=$(kill -0 $cpid 2>/dev/null && echo yes || echo no) uptime=$(up)"
    tail -5 /tmp/compositor.log
    kill $kpid 2>/dev/null; kill $cpid 2>/dev/null
fi
wait $cpid; echo "desk-gl: pomelo-compositor exit status $?"
exit 0
