#!/bin/sh
#
# S90meow_bootmenu - R3 Pro II boot selector (Boot Selector phase 0 · M2 接管 HibyOS)
#
# Design (BOOT_SELECTOR_DESIGN.md §2.1 timing / §9 M2):
#   * Placed AFTER S80_bt_init, BEFORE S92_03_start_music_player (S80 < S90 < S92),
#     so the selector runs before the official player is started.
#   * M2: meow_bootmenu is launched with --fork, so:
#       选 HibyOS -> bootmenu forks /usr/bin/hiby_player (最小启动契约),
#                   parent becomes supervisor/热键守护, blocks rcS.
#       选 MeowOS -> bootmenu exec /usr/bin/meow_launcherd (MeowOS 基础主屏),
#                   blocks rcS.
#     Either way rcS is blocked by S90 until整机重启/热键切换, so S92 (兜底 patch)
#     is normally never reached; SSH was moved to S84 so it is already online.
#   * rcS runs S??* (non-.sh) as fork-ed subprocesses, so `exec` here replaces
#     ONLY this script's process.
#   * 红线: 启动前清 /data/recovery_all、禁经 hiby_player.sh、禁 killall、
#     禁 SIGUSR1。回退 = 重刷不含选单固件 (M1 r3proii_m1.upt)。
#
case "$1" in
  start)
    echo -n "Starting meow_bootmenu: "
    # Boot mode marker (tmpfs is cleared each boot; rm once more to be sure).
    rm -f /tmp/meow_boot_mode
    # 红线：启动前清除 recovery_all，避免进入恢复流程。
    rm -f /data/recovery_all
    if [ -x /usr/bin/meow_bootmenu ]; then
        echo "OK (selector --fork)"
        exec /usr/bin/meow_bootmenu --fork
    else
        echo "MISSING /usr/bin/meow_bootmenu (fall through to S92)"
    fi
    ;;
  stop)
    echo -n "Stopping meow_bootmenu: "
    killall meow_bootmenu 2>/dev/null
    echo "OK"
    ;;
  restart|reload)
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
    ;;
esac

exit 0
