* luci/contrib/addons: add luci_hosts infrastructure
[project/luci.git] / contrib / package / luci-addons / dist / etc / init.d / luci_hosts
diff --git a/contrib/package/luci-addons/dist/etc/init.d/luci_hosts b/contrib/package/luci-addons/dist/etc/init.d/luci_hosts
new file mode 100644 (file)
index 0000000..42ccff4
--- /dev/null
@@ -0,0 +1,30 @@
+#!/bin/sh /etc/rc.common
+START=60
+
+apply_host() {
+       local cfg="$1"
+
+       config_get hostname "$cfg" hostname
+       config_get ipaddr   "$cfg" ipaddr
+
+       [ -n "$hostname" -a -n "$ipaddr" ] || return 0
+       
+       echo "$ipaddr $hostname" >> /var/etc/hosts
+}
+
+start() {
+       if [ ! -L /etc/hosts ]; then
+               test -f /etc/hosts && mv /etc/hosts /etc/hosts.local
+               ln -s /var/etc/hosts /etc/hosts
+       fi
+
+       test -d /var/etc || mkdir -p /var/etc
+       test -f /etc/hosts.local && cat /etc/hosts.local >> /var/etc/hosts
+       
+       config_load luci_hosts
+       config_foreach apply_host host
+}
+
+stop() {
+       test -f /var/etc/hosts && rm -f /var/etc/hosts
+}