jansson: moved to github
[openwrt.org/packages.git] / utils / ups.init
1 #!/bin/sh /etc/rc.common
2 # Copyright © 2012 OpenWrt.org
3 #
4 # This is free software, licensed under the GNU General Public License v2.
5 # See /LICENSE for more information.
6 #
7 START=50
8
9 DEFAULT=/etc/default/nut
10 RUN_D=/var/run
11 PID_F=$RUN_D/upsd.pid
12 UPS_C=$RUN_D/ups.conf
13 USERS_C=$RUN_D/upsd.users
14
15 nut_user_add() {
16 local cfg="$1"
17 local a
18 local val
19
20 config_get val "$cfg" username "$1"
21 echo "[$val]" >> $USERS_C
22
23 config_get val "$cfg" password
24 echo " password = $val" >> $USERS_C
25
26 config_get val "$cfg" actions
27 for a in $val; do
28 echo " actions = $a" >> $USERS_C
29 done
30
31 config_get val "$cfg" instcmds
32 for a in $val; do
33 echo " instcmds = $a" >> $USERS_C
34 done
35
36 config_get val "$cfg" upsmon
37 if [ -n "$val" ]; then
38 echo " upsmon $val" >> $USERS_C
39 fi
40 }
41
42 start() {
43 local in_driver have_drivers
44 config_cb() {
45 if [ "$1" != "driver" ]; then
46 in_driver=
47 else
48 echo "[$2]" >> $UPS_C
49 in_driver=true
50 have_drivers=true
51 fi
52 }
53 option_cb() {
54 if [ "$in_driver" = "true" ]; then
55 echo " $1 = $2" >> $UPS_C
56 fi
57 }
58
59 [ -d $RUN_D ] || mkdir -p $RUN_D
60 echo "# Config file automatically generated from UCI config" > $UPS_C
61 echo "# Config file automatically generated from UCI config" > $USERS_C
62
63 config_load ups
64
65 config_foreach nut_user_add user
66
67 [ -f $DEFAULT ] && . $DEFAULT
68 if [ "$have_drivers" = "true" ]; then
69 $DEBUG /usr/sbin/upsd $OPTIONS
70 $DEBUG /lib/nut/upsdrvctl start
71 fi
72 }
73
74 nut_driver_stop() {
75 local cfg="$1"
76 local driver
77
78 config_get driver "$cfg" driver
79
80 [ -r /var/run/$driver-$cfg ] && /lib/nut/upsdrvctl stop $cfg
81 }
82
83 stop() {
84 [ -r $PID_F ] && /usr/sbin/upsd -c stop
85 config_load ups
86 config_foreach nut_driver_stop driver
87 }
88
comments