#!/bin/sh
#
# S90meow_launcherd - R3 Pro II Plan B app-launcher daemon (zero patch)
#
# Design (ANALYSIS.md §18):
#   * PASSIVE daemon: never EVIOCGRABs, never injects events. Reads keys via
#     EVIOCGKEY state polling (immune to any grab the player holds), touch via
#     normal event reads on event1 (not grabbed). Zero interference with the
#     player's own key handling.
#   * Hotkey: hold PREVIOUS + VOL_UP together >=300ms -> app menu
#     (cross-device: PREVIOUS is a GPIO key on event0, VOL_UP is an ADC key
#     on event2. A two-key chord on event2 alone is impossible — its four
#     keys share ONE ADC channel, so the old dual-volume hotkey can never
#     register physically; see ANALYSIS.md §18.8).
#   * Starts BEFORE S92_03_start_music_player (S90 < S92) so it is up early;
#     the input devices are created by S11module_driver_default.
#   * Stop sends SIGTERM; the daemon SIGCONTs a frozen player before exiting.
#
case "$1" in
  start)
    echo -n "Starting meow_launcherd: "
    if [ -x /usr/bin/meow_launcherd ]; then
        /usr/bin/meow_launcherd >/dev/null 2>&1 &
        echo "OK"
    else
        echo "MISSING /usr/bin/meow_launcherd"
    fi
    ;;
  stop)
    echo -n "Stopping meow_launcherd: "
    if [ -f /usr/data/meow_launcherd.pid ]; then
        kill "$(cat /usr/data/meow_launcherd.pid)" 2>/dev/null
    fi
    killall meow_launcherd 2>/dev/null
    sleep 1
    echo "OK"
    ;;
  restart|reload)
    "$0" stop
    sleep 1
    "$0" start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
    ;;
esac

exit 0
