#!/bin/sh
# Переезд с rc-shell на rc-actions (D63): перенос настроек в новую схему, остановка старого демона,
# переименование endpoint mavp2p (rc_shell → rc_actions), loopback-адрес как в 60-rc-shell-passive (D28).
old=/etc/config/rc-shell
if [ -f "$old" ] && [ "$(uci -q get rc-actions.main.migrated)" != "1" ]; then
	for k in enabled mavlink_host mavlink_port dry_run statustext; do
		v=$(uci -q get rc-shell.main.$k); [ -n "$v" ] && uci set rc-actions.main.$k="$v"
	done
	uci -q get rc-actions.sim >/dev/null || { uci set rc-actions.sim=action; uci set rc-actions.sim.kind=sim; uci set rc-actions.sim.enabled=1; }
	v=$(uci -q get rc-shell.main.rc_channel); [ -n "$v" ] && uci set rc-actions.sim.channel="$v"
	v=$(uci -q get rc-shell.main.threshold); [ -n "$v" ] && uci set rc-actions.sim.threshold="$v"
	uci set rc-actions.main.migrated=1
	uci commit rc-actions
	# старый демон, если пакет luci-app-uavpcb-rc-shell ещё стоит (обновление через opkg без образа)
	[ -x /etc/init.d/rc-shell ] && { /etc/init.d/rc-shell stop; /etc/init.d/rc-shell disable; } 2>/dev/null
	mv "$old" "$old.migrated"
	logger -t rc-actions "config migrated from rc-shell (old copy: $old.migrated)"
fi
if uci -q get mavp2p.rc_shell >/dev/null && ! uci -q get mavp2p.rc_actions >/dev/null; then
	uci rename mavp2p.rc_shell=rc_actions && uci commit mavp2p
fi
h=$(uci -q get rc-actions.main.mavlink_host)
case "$h" in ""|127.0.0.1|0.0.0.0) ;; *) uci set rc-actions.main.mavlink_host=127.0.0.1; uci commit rc-actions ;; esac
exit 0
