#!/bin/sh 
# Network configuration for SMLinux
# For network bridges, see /etc/rc.d/rc.bridge
if grep -q nfsroot /proc/cmdline ; then
	# We are booting from an NFS share already, so disable networking 
  	NETWORKING=no
else
	NETWORKING=yes
fi

PATH=/bin
DHCP=yes
IFACE="eth0"
STATICIP=""	# in 192.168.0.2/24 CIDR format
GATEWAY=""
ROUTER=no
ROUTERIFACE=""

if [ "$NETWORKING" = "yes" ];  then
  if [ "$(uname -m)" != "x86_64" ]; then
	  DHCP=yes
  else
	  DHCP=no
  fi

  echo "Initializing network..."

  if [ "$DHCP" =  "yes" ]; then
	  echo "Running DHCP client..."
	  dhclient "$IFACE"

  elif [ "$DHCP" = "no" ]; then
	  if [ -z "$STATICIP" ]; then
		  echo "STATIC IP NOT SET!"
		  echo "Set it in /etc/rc.d/rc.network"
	  elif [ -z "$GATEWAY" ]; then
		  echo "GATEWAY NOT SET!"
		  echo "Set it in /etc/rc.d/rc.network"
	  else
		  echo "Setting static IPs"
		  ip link set "$IFACE" up
		  ip addr add "$STATICIP" dev "$IFACE"
		  ip route add default via "$STATICGW"
	  fi
  fi

  if [ "$ROUTER" = "yes" ]; then
	  echo "Setting up router configuration"
	  echo 1 > /proc/sys/net/ipv4/ip_forward
	  iptables -t nat -A POSTROUTING -o "$ROUTERIFACE" -j MASQUERADE
  fi
fi

# For devices without an RTC
if [ ! -e /dev/rtc0 ]; then
	echo "Setting time from busybox NTP"
	# run at a higher frequency
	busybox ntpd -N
	sleep 5
fi

exit 0
