From 6d4631ffaea0e7432423659a07dfade7bf2eadb8 Mon Sep 17 00:00:00 2001
From: Alessandro Rubini <rubini@gnudd.com>
Date: Thu, 27 Nov 2014 16:57:36 +0100
Subject: [PATCH] build: apply some dot-config choices at run-time, not
 build-time

We are aiming at a system where build-time configuration is irrelevant,
as every configuration happens at run-time, based on dot-config.
This is the first step.

Please note that this breaks the web interface, because it edits
wr_date.conf. A later commit will fix the web interface.

Signed-off-by: Alessandro Rubini <rubini@gnudd.com>
---
 build/scripts/wrs_build_wraprootfs            | 12 -----
 .../rootfs_override/etc/init.d/S20dot-config  |  8 +++
 .../rootfs_override/wr/bin/apply_dot-config   | 51 +++++++++++++++++++
 3 files changed, 59 insertions(+), 12 deletions(-)
 create mode 100755 userspace/rootfs_override/etc/init.d/S20dot-config
 create mode 100755 userspace/rootfs_override/wr/bin/apply_dot-config

diff --git a/build/scripts/wrs_build_wraprootfs b/build/scripts/wrs_build_wraprootfs
index dc8e53986..28309ab4c 100755
--- a/build/scripts/wrs_build_wraprootfs
+++ b/build/scripts/wrs_build_wraprootfs
@@ -47,18 +47,6 @@ rm -rf $TMPFS/dev
 (cd $TMPFS && tar xzf $DEVTAR)
 (cd $TMPFS && ln -fs sbin/init .)
 
-if [ ! -z "$CONFIG_NTP_SERVER" ]; then
-    echo "ntpserver $CONFIG_NTP_SERVER" > $TMPFS/wr/etc/wr_date.conf
-fi
-
-if [ ! -z "$CONFIG_DNS_SERVER" ]; then
-    rm -f $TMPFS/etc/resolv.conf
-    echo "nameserver $CONFIG_DNS_SERVER" > $TMPFS/etc/resolv.conf
-    if [ ! -z "$CONFIG_DNS_DOMAIN" ]; then
-        echo "domain $CONFIG_DNS_DOMAIN" >> $TMPFS/etc/resolv.conf
-    fi
-fi
-
 if [ "$CONFIG_REMOTE_SYSLOG_UDP" = "y" ]; then
     sed -i 's/@@remote-host/@remote-host/' $TMPFS/etc/rsyslog.conf
 fi
diff --git a/userspace/rootfs_override/etc/init.d/S20dot-config b/userspace/rootfs_override/etc/init.d/S20dot-config
new file mode 100755
index 000000000..fbacbe48c
--- /dev/null
+++ b/userspace/rootfs_override/etc/init.d/S20dot-config
@@ -0,0 +1,8 @@
+#!/bin/sh
+
+# This script applies the dot-config. It is a boot script, but actual
+# functionality is moved to a separate binary, so the web interface
+# could edit dot-config and run /wr/bin/apply_dot-config like we do here,
+# without the need to rember wheter this is S20 or S10 during boot.
+
+. /wr/bin/apply_dot-config
\ No newline at end of file
diff --git a/userspace/rootfs_override/wr/bin/apply_dot-config b/userspace/rootfs_override/wr/bin/apply_dot-config
new file mode 100755
index 000000000..811d4e509
--- /dev/null
+++ b/userspace/rootfs_override/wr/bin/apply_dot-config
@@ -0,0 +1,51 @@
+#!/bin/sh
+
+# This script applies the current dot-config to make
+# the choices users wanted.  You can change the dot-config on flash,
+# and call this script to apply changes (please note that some changes
+# require restarting running processes).  The script is called at
+# every boot by /etc/init.d/S20dot-config
+
+# We create a temporary file in /tmp, to avoid wearing flash if not
+# needed. Then we replace the real file if different.
+
+T=$(mktemp /tmp/config-XXXXXX)
+
+copy_conf() {
+    # busybox cmp exits 1 or 2 according to GNU man page
+    for dest in $*; do
+	cmp -s $T $1 || cp $T $1
+    done
+}
+
+# Check and complain, but we need to edit some files even if unconfigured.
+if [ -f /wr/etc/dot-config ]; then
+    . /wr/etc/dot-config
+    configured=true
+else
+    echo "No /wr/etc/dot-config to use" >& 2
+    configured=false
+fi
+
+##### Actual configuration actions start here.
+
+# A non-existent wr_date.conf means no NTP. So "rm" if unconfigured
+if [ ! -z "$CONFIG_NTP_SERVER" ]; then
+    echo "ntpserver $CONFIG_NTP_SERVER" > $T
+    copy_conf /wr/etc/wr_date.conf
+else
+    rm -f /wr/etc/wr_date.conf
+fi
+
+# /etc/resolv.conf can be empty, so start empty
+> $T
+if [ ! -z "$CONFIG_DNS_SERVER" ]; then
+    echo "nameserver $CONFIG_DNS_SERVER" >> $T
+    if [ ! -z "$CONFIG_DNS_DOMAIN" ]; then
+        echo "domain $CONFIG_DNS_DOMAIN" >> $T
+    fi
+fi
+copy_conf /etc/resolv.conf /usr/etc/resolv.conf
+
+
+
-- 
GitLab