aboutsummaryrefslogtreecommitdiff
path: root/net/travelmate/files/travelmate.init
blob: c78524211a621c51bce76d150f0bd5efc68c83cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/sh /etc/rc.common

START=25
USE_PROCD=1

EXTRA_COMMANDS="status"
EXTRA_HELP="	status	Print runtime information"

trm_init="/etc/init.d/travelmate"
trm_script="/usr/bin/travelmate.sh"
trm_pidfile="/var/run/travelmate.pid"

boot()
{
	ubus -t 30 wait_for network.interface network.wireless 2>/dev/null
	rc_procd start_service
}

start_service()
{
	if [ $("${trm_init}" enabled; printf "%u" ${?}) -eq 0 ]
	then
		procd_open_instance "travelmate"
		procd_set_param command "${trm_script}" "${@}"
		procd_set_param pidfile "${trm_pidfile}"
		procd_set_param stdout 1
		procd_set_param stderr 1
		procd_close_instance
	fi
}

reload_service()
{
	local ppid pid timeout="$(uci_get travelmate global trm_timeout)"

	if [ -s "${trm_pidfile}" ]
	then
		ppid="$(cat "${trm_pidfile}" 2>/dev/null)"
		if [ -n "${ppid}" ]
		then
			pid="$(pgrep -xnf "sleep ${timeout:-60} 0" -P ${ppid} 2>/dev/null)"
			if [ -n "${pid}" ]
			then
				kill -INT ${pid} 2>/dev/null
			fi
		fi
	fi
}

stop_service()
{
	rc_procd "${trm_script}" stop
}

status_service()
{
	local key keylist value rtfile="$(uci_get travelmate global trm_rtfile)"

	rtfile="${rtfile:-"/tmp/trm_runtime.json"}"
	json_load_file "${rtfile}" >/dev/null 2>&1
	json_select data >/dev/null 2>&1
	if [ ${?} -eq 0 ]
	then
		printf "%s\n" "::: travelmate runtime information"
		json_get_keys keylist
		for key in ${keylist}
		do
			json_get_var value "${key}"
			printf "  + %-18s : %s\n" "${key}" "${value}"
		done
	else
		printf "%s\n" "::: no travelmate runtime information available"
	fi
}

service_triggers()
{
	local trigger="$(uci_get travelmate global trm_iface)"
	local delay="$(uci_get travelmate global trm_triggerdelay)"

	PROCD_RELOAD_DELAY=$((${delay:-2} * 1000))
	if [ -n "${trigger}" ]
	then
		procd_add_interface_trigger "interface.*.down" "${trigger}" "${trm_init}" reload
	else
		procd_add_raw_trigger "interface.*.down" ${PROCD_RELOAD_DELAY} "${trm_init}" reload
	fi
	procd_add_config_trigger "config.change" "wireless" "${trm_init}" reload
	procd_add_config_trigger "config.change" "travelmate" "${trm_init}" restart
}