#!/bin/sh
# If a DNS IP address is passed to the kernel via the command line, set 
# resolve.conf to use it. The 8th colon separated element in the
# "ip="  linux boot command line argument contains the first
# DNS server IP address, if specified, according to the spec at 
#
# https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt
# (it appears that some other Roku platforms don't use this spec)
#
# In order for this to function properly, /etc/resolv.conf must be a
# symbolic link to /tmp/resolv.conf, ie:
#
#  ln -s /tmp/resolv.conf /etc/resolv.conf
#
# because /etc is a read only file system on our platforms, while
# /tmp is RW.

cmdline=`cat /proc/cmdline`
for arg in $cmdline ; do
   if [ "${arg##ip=}" != "${arg}" ]; then
      # the expr in busybox doesn't support expr properly so use awk instead
      ip=`echo $arg | awk -F : '{ print $8 }'`
      if [ -n "$ip" ]; then
         echo "-------------- using $ip for DNS -----------------"
         echo "# generated by $0" > /tmp/resolv.conf
         echo "# using DNS ip passed to kernel cmdline" >> /tmp/resolv.conf
         echo "nameserver $ip" >> /tmp/resolv.conf
	 exit
      fi
   fi
done

# if we're here, assume DHCP was used instead...
echo "-------------- using DHCP discovery for DNS -----------------"
if [ ! -s /tmp/resolv.conf ] ; then
   cp /proc/net/pnp /tmp/resolv.conf
   echo "# copied by S64dnsip" >> /tmp/resolv.conf
   chown app:app /tmp/resolv.conf
   chmod ugo+w /tmp/resolv.conf
fi

