#!/bin/sh
# init=/usr/bin/desk-shoot : generic scenario for scripts/shoot-all.sh.  Configured through glenv= on the kernel command line
# (desk-env exports the keys):  SH_PANEL=1 SH_DESKTOP=1  SH_DELAY=<s before the plan>  SH_GAP=<s between items>  SH_RUN=<s>
#   SH_RENDERER=pixman|gles2   SH_ARGS=<args for win: items, '+' = space>   SH_PLAN=item+item+...
# items: files filespt calc edit term theme settings launcher notes notes32 status setup glserver win:<exe>
# Markers on the serial console tell the harness where the scenario is:  SHOOT: base up / launched <item> / ready
. /usr/bin/desk-env
[ -n "${SH_RENDERER:-}" ] && export WLR_RENDERER=$SH_RENDERER
export LIBGL_ALWAYS_SOFTWARE=1 LP_NUM_THREADS=0 EGL_PLATFORM=${EGL_PLATFORM:-}
/usr/bin/pomelo-compositor 2>/tmp/compositor.log &
cpid=$!
i=0
while [ $i -lt 300 ] && [ ! -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
export XDG_DATA_DIRS=/usr/share
pids=""
if [ "${SH_PANEL:-1}" = 1 ]; then /usr/bin/pomelo-panel 2>/tmp/panel.log & pids="$pids $!"; sleep 1; fi
if [ "${SH_DESKTOP:-1}" = 1 ]; then /usr/bin/pomelo-desktop 2>/tmp/desktop.log & pids="$pids $!"; fi
echo "SHOOT: base up"
sleep ${SH_DELAY:-6}
ARGS=$(echo "${SH_ARGS:-}" | tr + ' ')
for it in $(echo "${SH_PLAN:-}" | tr + ' '); do
    case $it in
    files)     /usr/bin/pomelo-files /c 2>/tmp/files.log & pids="$pids $!" ;;
    filespt)   /usr/bin/pomelo-files "/c/Program Files/PomeloTest" 2>/tmp/filespt.log & pids="$pids $!" ;;
    calc)      /usr/bin/pomelo-calc 2>/tmp/calc.log & pids="$pids $!" ;;
    edit)      /usr/bin/pomelo-edit 2>/tmp/edit.log & pids="$pids $!" ;;
    term)      /usr/bin/pomelo-terminal 2>/tmp/term.log & pids="$pids $!" ;;
    theme)     /usr/bin/pomelo-theme 2>/tmp/theme.log & pids="$pids $!" ;;
    settings)  /usr/bin/pomelo-control-center 2>/tmp/settings.log & pids="$pids $!" ;;
    launcher)  /usr/bin/pomelo-launcher 2>/tmp/launcher.log & pids="$pids $!" ;;
    notes)     /usr/bin/pe_load "/c/Program Files/Pomelo Notes/Notes.exe" 2>/tmp/notes.log & pids="$pids $!" ;;
    notes32)   /usr/bin/pe_load /usr/bin/ct32_gui.exe 2>/tmp/notes32.log & pids="$pids $!" ;;
    status)    /usr/bin/pe_load /usr/bin/hello10.exe 2>/tmp/status.log & pids="$pids $!" ;;
    setup)     /usr/bin/pe_load /usr/bin/pomelo-setup.exe /S; echo "SHOOT: setup rc=$?"; ls "/c/Program Files/PomeloTest" ;;
    glserver)  /usr/bin/pomelo-glserver >/tmp/glserver.log 2>&1 & gp=$!; pids="$pids $gp"
               n=0; while [ $n -lt 1500 ] && [ ! -S /tmp/pomelo-glserver.sock ] && kill -0 $gp 2>/dev/null; do sleep 0.2; n=$((n+1)); done ;;
    win:*)     /usr/bin/pe_load -v /usr/bin/${it#win:} $ARGS >/tmp/win.log 2>&1 & pids="$pids $!" ;;
    esac
    echo "SHOOT: launched $it"
    sleep ${SH_GAP:-3}
done
echo "SHOOT: ready"
N=${SH_RUN:-600}; t=0
while [ $t -lt $N ] && kill -0 $cpid 2>/dev/null; do sleep 1; t=$((t+1)); done
echo "SHOOT: end compositor=$(kill -0 $cpid 2>/dev/null && echo up || echo down)"
tail -3 /tmp/compositor.log
kill $pids $cpid 2>/dev/null; wait
exit 0
