#!/bin/sh /etc/rc.common
START=95
STOP=0
USE_PROCD=1

start_service() {
	:
}

stop_service() {
	procd_set_param timeout 30
	config_load qmodem
	config_foreach _soft_reboot modem-device
}

_soft_reboot() {
	config_get soft_reboot $1 soft_reboot
	[ "$soft_reboot" != "1" ] && return 0

	config_get at_port $1 at_port
	[ -z "$at_port" ] && return 0

	for i in $(seq 1 3); do
		response=$(tom_modem -d "$at_port" -o a -c "AT+CFUN=1,1")
		echo "$response" | grep -qi "ok" && break
		sleep 1
	done

	for i in $(seq 1 15); do
		[ ! -e "$at_port" ] && {
			echo "Device disconnected after $i seconds"
			return 0
		}
		sleep 1
	done
	
	echo "Device did not disconnect within timeout period"
	return 0
}