#!/bin/sh
#
# S92_03_start_music_player - Boot Selector phase 0 · M2 兜底 patch
#
# 正常路径：rcS 被 S90meow_bootmenu 阻塞（fork 播放器后父进程监督，或 exec
#            meow_launcherd），本脚本**不会执行**。
# 兜底路径：S90 崩溃/选单 fork 失败导致 rcS 推进到本脚本时，读 /tmp/meow_boot_mode
#           + pidof 判断，安全拉起播放器或跳过。
#
# 红线：* 仅经 pidof 判断，不 killall hiby_player（触发 sh 看门狗 reboot）；
#       * 禁 SIGUSR1；* 兜底拉起才用 hiby_player.sh（官方行为，含 sleep 1; reboot
#         看门狗——只在播放器无人接管时兜底，正常 HibyOS 由 bootmenu 直拉二进制）。
#

PL01=/usr/bin/hiby_player.sh

case "$1" in
  start)
	echo -n "Starting $0 : begin "

	# 若 S90 已把模式写成 /tmp/meow_boot_mode，优先按其决策兜底
	if [ -f /tmp/meow_boot_mode ]; then
		case "$(cat /tmp/meow_boot_mode)" in
		  meow)
			echo "MeowOS mode: skip player (launcher should have taken over)"
			exit 0
			;;
		  hiby)
			if pidof hiby_player >/dev/null 2>&1; then
				echo "player already up (bootmenu fork), skip"
				exit 0
			fi
			[ -f $PL01 ] && ( $PL01 & ) || echo " file <$PL01> don't exist."
			;;
		  *)
			# 未知 mode：按官方兜底
			if pidof hiby_player >/dev/null 2>&1; then
				echo "unknown mode, player already up, skip"
				exit 0
			fi
			[ -f $PL01 ] && ( $PL01 & ) || echo " file <$PL01> don't exist."
			;;
		esac
	else
		# 无模式标记：选单崩/未写，官方兜底（先 pidof 防双拉竞态）
		if pidof hiby_player >/dev/null 2>&1; then
			echo "no mode file, player already up, skip"
			exit 0
		fi
		[ -f $PL01 ] && ( $PL01 & ) || echo " file <$PL01> don't exist."
	fi

	echo -n "Starting $0 : end "
	;;
  stop)
	echo -n "Stopping $0 : "
	echo "OK"
	;;
  restart|reload)
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?
