#!/bin/sh
#
# S45meow_ntpsync — 开机网络校时（M22）
#
# 只负责把同步器丢到后台并**立即返回**，开机关键路径增加 0 秒。
# 真正的「等网络就绪 + 有限重试 + 总超时」全在 /usr/bin/meow_ntpsync 里。
#
# 为什么排 S45：S40network 只 ifup 了 lo，WiFi 由 S43 之后才起来，租约更晚，
# 所以本脚本启动时大概率还没有网络——同步器自己轮询默认路由（最多 -t 秒）。
# WiFi 在更晚（用户手动连）才就绪的情况，由 udhcpc 钩子
# /usr/share/udhcpc/default.script.d/50meow_ntpsync 在拿到租约时再触发一次。
#
# 失败不阻断 init：所有分支都 exit 0（除非参数错误）。
#
case "$1" in
  start)
	printf "Starting meow_ntpsync: "
	[ -x /usr/bin/meow_ntpsync ] || { echo "SKIP (not installed)"; exit 0; }
	setsid /usr/bin/meow_ntpsync -b -t 120 >/dev/null 2>&1 &
	echo "OK"
	;;
  stop)
	printf "Stopping meow_ntpsync: "
	if [ -f /tmp/meow_ntpsync.lock/pid ]; then
		kill "$(cat /tmp/meow_ntpsync.lock/pid 2>/dev/null)" 2>/dev/null
	fi
	rm -rf /tmp/meow_ntpsync.lock 2>/dev/null
	echo "OK"
	;;
  restart|reload)
	"$0" stop
	"$0" start
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
	;;
esac

exit 0
