ASR_BASE

Change-Id: Icf3719cc0afe3eeb3edc7fa80a2eb5199ca9dda1
diff --git a/external/routing/oonf-init-scripts/Makefile b/external/routing/oonf-init-scripts/Makefile
new file mode 100644
index 0000000..8bfbf6e
--- /dev/null
+++ b/external/routing/oonf-init-scripts/Makefile
@@ -0,0 +1,36 @@
+include $(TOPDIR)/rules.mk
+include $(INCLUDE_DIR)/kernel.mk
+
+PKG_NAME:=oonf-init-scripts
+PKG_VERSION:=0.9.1-r3
+PKG_RELEASE:=1
+PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)
+
+include $(INCLUDE_DIR)/package.mk
+
+define Package/oonf-init-scripts
+	SECTION:=net
+	CATEGORY:=Network
+	MAINTAINER:=Henning Rogge <hrogge@gmail.com>
+	SUBMENU:=OLSR.org network framework
+	URL:=http://www.olsr.org/
+	TITLE:= Common OONF startup scripts
+	VERSION:=$(PKG_VERSION)
+endef
+
+define Build/Prepare
+	mkdir -p $(PKG_BUILD_DIR)
+endef
+
+define Build/Configure
+endef
+
+define Build/Compile
+endef
+
+define Package/oonf-init-scripts/install
+	$(INSTALL_BIN)  -D ./files/oonf_init.sh            $(1)/lib/functions/oonf_init.sh
+	$(INSTALL_BIN)  -D ./files/oonf_hotplug.sh         $(1)/lib/functions/oonf_hotplug.sh
+endef
+
+$(eval $(call BuildPackage,oonf-init-scripts))
diff --git a/external/routing/oonf-init-scripts/files/oonf_hotplug.sh b/external/routing/oonf-init-scripts/files/oonf_hotplug.sh
new file mode 100755
index 0000000..e0002b3
--- /dev/null
+++ b/external/routing/oonf-init-scripts/files/oonf_hotplug.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+case "${ACTION}" in
+	ifup)
+		. /etc/rc.common /etc/init.d/${DAEMON} enabled && {
+			logger -t "${DAEMON}[hotplug]" -p daemon.info 'reloading configuration'
+			. /etc/rc.common /etc/init.d/${DAEMON} reload
+		}
+	;;
+esac
diff --git a/external/routing/oonf-init-scripts/files/oonf_init.sh b/external/routing/oonf-init-scripts/files/oonf_init.sh
new file mode 100755
index 0000000..8ab5b3b
--- /dev/null
+++ b/external/routing/oonf-init-scripts/files/oonf_init.sh
@@ -0,0 +1,120 @@
+#!/bin/sh
+
+. /usr/share/libubox/jshn.sh
+
+oonf_log()
+{
+  logger -s -t ${DAEMON} -p daemon.info "${1}"
+}
+
+oonf_get_layer3_device()
+{
+  local interface="${1}"  # e.g. 'mywifi'
+  local status dev proto
+  local query="{ \"interface\" : \"${interface}\" }"
+
+  status="$( ubus -S call network.interface status "${query}" )" && {
+    json_load "${status}"
+    json_get_var 'dev' l3_device
+    json_get_var 'proto' proto
+    case "${proto}" in
+      pppoe)
+        # TODO: otherwise it segfaults
+        oonf_log "refusing to add '$interface', because of proto '${proto}'"
+      ;;
+      *)
+        echo "${dev}" # e.g. 'wlan0-1'
+      ;;
+    esac
+  }
+}
+
+oonf_add_devices_to_configuration()
+{
+  local i=0
+  local device_name= section= interface= single_interface=
+
+  # make a copy of configuration and
+  # add a 'name' (physical name) for all
+  # 'interface-names' (e.g. mywifi)
+  #
+  # olsrd2.@interface[2]=interface
+  # olsrd2.@interface[2].ifname='wan lan wlanadhoc wlanadhocRADIO1'
+
+  # /var is in ramdisc/tmpfs
+  uci export ${DAEMON} >"/var/run/${DAEMON}_dev"
+
+  while section="$( uci -q -c /etc/config get "${DAEMON}.@[${i}]" )"; do {
+    echo "section: ${section}"
+
+    interface="$( uci -q -c /etc/config get "${DAEMON}.@[${i}].ifname" )" || {
+      i=$(( i + 1 ))
+      continue
+    }
+
+    case "$( uci -q get "${DAEMON}.@[${i}].ignore" )" in
+      1|on|true|enabled|yes)
+        oonf_log "removing/ignore section '$section'"
+        uci -q -c /var/run delete "${DAEMON}_dev.@[${j}]"
+        i=$(( i + 1 ))
+
+        continue
+      ;;
+    esac
+
+    for single_interface in ${interface}; do {
+      device_name="$( oonf_get_layer3_device "${single_interface}" )"
+
+      echo "Interface: ${single_interface} = ${device_name}"
+
+      if [ ! -z "${device_name}" ]
+      then
+        # add option 'name' for 'ifname' (e.g. 'mywifi')
+        uci -q -c /var/run add_list "${DAEMON}_dev.@[${i}].name=${device_name}"
+      fi
+    } done
+    i=$(( $i + 1 ))
+  } done
+
+  uci -q -c /var/run commit "${DAEMON}_dev"
+
+  oonf_log "wrote '/var/run/${DAEMON}_dev'"
+}
+
+oonf_reread_config()
+{
+  local pid
+  local pidfile="/var/run/${DAEMON}.pid"
+
+  if   [ -e "${pidfile}" ]; then
+    read pid <"${pidfile}"
+  elif pidfile="$( uci -q get "${DAEMON}.@global[0].pidfile" )"; then
+    read pid <"${pidfile}"
+  fi
+
+  # if empty, ask kernel
+  pid="${pid:-$( pidof ${DAEMON} )}"
+
+  [ -n "${pid}" ] && kill -SIGHUP ${pid}
+}
+
+start()
+{
+  oonf_add_devices_to_configuration
+
+  # produce coredumps
+  ulimit -c unlimited
+
+  service_start /usr/sbin/${DAEMON} --set global.fork=true --load uci:///var/run/${DAEMON}_dev
+}
+
+stop()
+{
+  service_stop /usr/sbin/${DAEMON}
+}
+
+reload()
+{
+  oonf_add_devices_to_configuration
+  oonf_reread_config
+}