#!/bin/sh
# init=/usr/bin/desk-startmenu : compositor + pomelo-panel (wallpaper; the new taskbar will replace it) and the Start
# menu (pomelo-launcher).  The launcher is restarted a second after it exits (Esc, a program was started), so the test
# can open it again and again; POMELO_TASKBAR_H=48 is the height the menu leaves free at the bottom.
. /usr/bin/desk-env
export POMELO_TASKBAR_H=48
# DESK_FIXTURES=1 (the test): a writable home on tmpfs with files and a launch history of known ages, so the
# "Recommended" section shows "34 minutes ago" style times (the image itself is read-only ext2).
if [ -n "${DESK_FIXTURES:-}" ]; then
    export HOME=/tmp/home; mkdir -p $HOME/.local/share/pomelo; cd $HOME; now=$(date +%s)
    echo "Pomelo quarterly report" > quarterly-report.txt; touch -d "@$((now-2040))" quarterly-report.txt
    echo x > Screenshot-2026-11-22.png; touch -d "@$((now-10800))" Screenshot-2026-11-22.png
    echo x > todo.txt; touch -d "@$((now-172800))" todo.txt
    printf '%s\tpomelo-terminal\n%s\tpomelo-edit\n' $((now-720)) $((now-9000)) > $HOME/.local/share/pomelo/launcher-recent
    cd /
fi
/usr/bin/pomelo-compositor 2>/tmp/compositor.log &
cpid=$!
i=0
while [ $i -lt 100 ] && [ ! -S "$XDG_RUNTIME_DIR/wayland-0" ]; do sleep 0.2; i=$((i+1)); done
export WAYLAND_DISPLAY=wayland-0
export XDG_DATA_DIRS=/usr/share
/usr/bin/pomelo-panel 2>/tmp/panel.log &
ppid=$!
sleep ${DESK_LAUNCHER_AT:-8}
( while :; do /usr/bin/pomelo-launcher 2>>/tmp/launcher.log; echo "desk-startmenu: launcher exited"; if pidof pe_load >/dev/null; then echo "desk-startmenu: pe_load is running (started from the Start menu)"; break; fi; sleep ${DESK_REOPEN:-2}; done ) &
lpid=$!
N=${DESK_RUN_SECONDS:-85}; t=0
while [ $t -lt $N ] && kill -0 $cpid 2>/dev/null; do sleep 1; t=$((t+1)); done
echo "--- launcher log"; cat /tmp/launcher.log
echo "--- compositor log"; tail -6 /tmp/compositor.log
echo "--- processes"; ps 2>/dev/null | grep -E "pe_load|pomelo" | grep -v grep
kill $lpid 2>/dev/null; kill $(pidof pomelo-launcher pe_load) 2>/dev/null; kill $ppid $cpid 2>/dev/null
i=0; while [ $i -lt 20 ] && [ -n "$(pidof pe_load pomelo-panel pomelo-compositor pomelo-launcher)" ]; do sleep 0.5; i=$((i+1)); done
wait $lpid $ppid $cpid 2>/dev/null
exit 0
