#!/bin/sh /etc/rc.common
# Keep the control panel's SMS pool in step with the SIM (see /opt/mu300/bin/mu300-smsd). openwrt-luci only (D11).
#
# The pool is the panel's sms_pool (uci unisoc_modem.main.sms_pool, checked as the panel's backend checks it), or
# /etc/mu300/sms-pool: /etc, not /var (tmpfs), so the pool outlives a reboot. Never /etc/mu300/sms, which is the
# sms command's own state. A changed sms_pool takes effect when this service restarts.
#
# No wait for the modem tty here: mu300-smsd waits for mu300-atd's command fifo itself (ten minutes at most, then
# procd starts it again).
START=95
USE_PROCD=1
export PATH="$PATH:/opt/mu300/busybox-bin"

start_service() {
	local pool=/etc/mu300/sms-pool
	if [ -f /usr/share/unisoc-modem/lib.sh ]; then
		. /usr/share/unisoc-modem/lib.sh
		pool=$(safe_state_dir "$(uci_get sms_pool)" "$pool")
	fi
	case $pool in /etc/mu300/sms|/etc/mu300/sms/*) pool=/etc/mu300/sms-pool ;; esac
	procd_open_instance smsd
	procd_set_param command /opt/mu300/bin/mu300-smsd
	procd_set_param env MU300_SMS_POOL="$pool"
	procd_set_param respawn 3600 10 0
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_close_instance
}
