#!/bin/sh
# init=/usr/bin/desk-glapps : compositor (renderer from glenv WLR_RENDERER_GL=gles2|pixman, default gles2) + EGL/Wayland GL clients.
#   glenv= keys: GLA=<seconds clients run, default 25>, GLAPPS=<a+b+c> subset of: glwayland es2gears eglgears calc  (default glwayland+es2gears+calc)
. /usr/bin/desk-env
export WLR_RENDERER=${WLR_RENDERER_GL:-gles2}
export POMELO_WLR_DEBUG=0 EGL_PLATFORM=wayland
up() { read u _ < /proc/uptime; echo $u; }
echo "desk-glapps: renderer=$WLR_RENDERER apps=${GLAPPS:-glwayland+es2gears+calc}"
/usr/bin/pomelo-compositor 2>/tmp/compositor.log &
cpid=$!
i=0
while [ $i -lt 600 ] && [ ! -S "$XDG_RUNTIME_DIR/wayland-0" ] && kill -0 $cpid 2>/dev/null; do sleep 0.1; i=$((i+1)); done
export WAYLAND_DISPLAY=wayland-0
SECS=${GLA:-25}
pids=""
for a in $(echo "${GLAPPS:-glwayland+es2gears+calc}" | tr + ' '); do
    case $a in
    glwayland) /usr/bin/glwayland $SECS ${GLW:-320} ${GLH:-240} & ;;
    es2gears)  /usr/bin/es2gears_wayland & ;;
    eglgears)  /usr/bin/eglgears_wayland & ;;
    calc)      /usr/bin/pomelo-calc 2>/tmp/calc.log & ;;
    esac
    pids="$pids $!"
    sleep 1
done
sleep $((SECS + 2))
echo "desk-glapps: compositor alive=$(kill -0 $cpid 2>/dev/null && echo yes || echo no)"
tail -4 /tmp/compositor.log
kill $pids $cpid 2>/dev/null
wait 2>/dev/null
exit 0
