net: mosquitto: overhaul uci conversion script
[feed/packages.git] / net / mosquitto / files / mosquitto.uci.convert
1 #!/bin/sh
2 # Converts a uci config file into an appropriate mosquitto.conf snippet
3 # expected to be used in an init file to generate a config file to run from
4 # Karl Palsson <karlp@remake.is> 2012.
5 # Considered to be released into the public domain
6
7 [ -f $IPKG_INSTROOT/lib/functions.sh ] && . $IPKG_INSTROOT/lib/functions.sh
8
9 TCONF=/tmp/mosquitto.generated.$$.conf
10 while getopts "f:" o; do
11 case $o in
12 f)
13 TCONF=$OPTARG
14 ;;
15 esac
16 done
17
18 if [ -e $TCONF ]; then
19 echo "Odd, same temporary generated config file already existed: $TCONF"
20 exit 1
21 fi
22
23 echo "Generating mosquitto config file in $TCONF"
24 NOW=$(date)
25 echo "# mosquitto.conf file generated from UCI config." >>$TCONF
26 echo "# Config snippet generated by $0 on $NOW" >>$TCONF
27 echo "#" >> $TCONF
28
29 # Usage: append_if cfg uci_name output_name
30 # add a config line of the form "output_name <value>"
31 # if the "uci_name" was found.
32 # output_name defaults to uci_name if not specified.
33 append_if() {
34 local cfg="$1"
35 local uci_name="$2"
36 local out_name="$3"
37 if [ -z "$out_name" ]; then
38 out_name=$uci_name
39 fi
40 config_get val $cfg $uci_name
41 if [ -n "$val" ]; then
42 echo "$out_name $val" >> $TCONF
43 fi
44 }
45
46 # mosquitto uses true/false, uci uses 1/0
47 # note that this is not shell truthy, but equality with 1!
48 append_bool() {
49 if [ $2 -eq 1 ]; then
50 echo "$1 true" >> $TCONF
51 else
52 echo "$1 false" >> $TCONF
53 fi
54 }
55
56 # as per append_if, but gets the value as a uci bool, not raw
57 append_optional_bool() {
58 local cfg="$1"
59 local uci_name="$2"
60 local out_name="$3"
61 config_get val $cfg $uci_name
62 if [ -n "$val" ]; then
63 config_get_bool real $cfg $uci_name
64 append_bool $out_name $real
65 fi
66 }
67
68 mosq_general() {
69 config_get destinations "$1" log_dest
70 for dest in $destinations; do
71 echo "log_dest $dest" >> $TCONF
72 done
73
74 config_get_bool no_remote "$1" no_remote_access 0
75 if [ "$no_remote" -eq 1 ]; then
76 echo "bind_address 127.0.0.1" >> $TCONF
77 fi
78
79 config_get port "$1" port 1883
80 echo "port $port" >> $TCONF
81 append_if "$1" protocol
82 append_if "$1" max_inflight_messages
83 append_if "$1" max_queued_messages
84
85 }
86
87 add_listener() {
88 echo "" >> $TCONF
89 config_get port "$1" port
90 if [ -z "$port" ]; then
91 echo "Ignoring listener section without port"
92 return
93 fi
94 config_get_bool no_remote "$1" no_remote_access 0
95 if [ "$no_remote" -eq 1 ]; then
96 echo "listener $port 127.0.0.1" >> $TCONF
97 else
98 echo "listener $port" >> $TCONF
99 fi
100
101 append_if "$1" protocol
102 }
103
104 add_topic() {
105 echo "topic $1" >> $TCONF
106 }
107
108 add_bridge() {
109 config_get conn "$1" connection
110 config_get addr "$1" address
111 if [ -z "$conn" -o -z "$addr" ]; then
112 echo "Ignoring bridge section, misisng connection/address"
113 return
114 fi
115 echo "" >> $TCONF
116 echo "# Bridge connection from UCI section" >> $TCONF
117 append_if "$1" connection
118 append_if "$1" address
119
120 config_list_foreach "$1" topic add_topic
121 append_optional_bool "$1" cleansession cleansession
122 append_optional_bool "$1" try_private try_private
123
124 append_if "$1" clientid
125 append_if "$1" identity bridge_identity
126 append_if "$1" psk bridge_psk
127 append_if "$1" tls_version bridge_tls_version
128 }
129
130
131 config_load "mosquitto"
132 config_foreach mosq_general "mosquitto"
133 config_foreach add_listener "listener"
134 config_foreach add_bridge "bridge"