#!/bin/sh
### BEGIN INIT INFO
# Provides:          networking
# Required-Start:    mountvirtfs $local_fs
# Required-Stop:     $local_fs
# Should-Start:      ifupdown
# Should-Stop:       ifupdown
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Raise network interfaces.
### END INIT INFO

PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"

[ -x /sbin/ifup ] || exit 0

check_network_file_systems() {
    [ -e /proc/mounts ] || return 0

    if [ -e /etc/iscsi/iscsi.initramfs ]; then
	echo "not deconfiguring network interfaces: iSCSI root is mounted."
	exit 0
    fi

    exec 9<&0 < /proc/mounts
    while read DEV MTPT FSTYPE REST; do
	case $DEV in
	/dev/nbd*|/dev/nd[a-z]*|/dev/etherd/e*)
	    echo "not deconfiguring network interfaces: network devices still mounted."
	    exit 0
	    ;;
	esac
	case $FSTYPE in
	nfs|nfs4|smbfs|ncp|ncpfs|cifs|coda|ocfs2|gfs|pvfs|pvfs2|fuse.httpfs|fuse.curlftpfs)
	    echo "not deconfiguring network interfaces: network file systems still mounted."
	    exit 0
	    ;;
	esac
    done
    exec 0<&9 9<&-
}

check_network_swap() {
    [ -e /proc/swaps ] || return 0

    exec 9<&0 < /proc/swaps
    while read DEV MTPT FSTYPE REST; do
	case $DEV in
	/dev/nbd*|/dev/nd[a-z]*|/dev/etherd/e*)
	    echo "not deconfiguring network interfaces: network swap still mounted."
	    exit 0
	    ;;
	esac
    done
    exec 0<&9 9<&-
}

function cfg_print_param() {
        if [ $# -lt 1 ] ; then echo "Config Error: cfg_print_value() called with too few parameters."; return 1 ; fi

        val=`"$UCI" $UCI_OPTS get "$1"`
        echo -n "$val"
}


UCI=/sbin/uci
UCI_OPTIONS="-q"

update_network_json() {
   echo "Checking JSON configuration ..."
   ETH0_INTERFACE=`$UCI $UCI_OPTIONS get network.eth0`
   if [ "$ETH0_INTERFACE" == "interface" ]; then
     ETH0_PROTO=`$UCI $UCI_OPTIONS get network.eth0.proto`
     if [ "$ETH0_PROTO" == "static" ]; then
       ETH0_ADDR=`$UCI $UCI_OPTIONS get network.eth0.ipaddr`
       ETH0_NETMASK=`$UCI $UCI_OPTIONS get network.eth0.netmask`
       ETH0_GATEWAY=`$UCI $UCI_OPTIONS get network.eth0.gateway`
       ETH0_DNS1=`$UCI $UCI_OPTIONS get network.eth0.dns1`
       ETH0_DNS2=`$UCI $UCI_OPTIONS get network.eth0.dns2`

       cat > /tmp/network.json <<EOF
{
    "ethernet_static_ip": "$ETH0_ADDR",
    "ethernet_static_netmask": "$ETH0_NETMASK",
    "ethernet_static_gateway": "$ETH0_GATEWAY",
    "ethernet_static_dns1": "$ETH0_DNS1",
    "ethernet_static_dns2": "$ETH0_DNS2"
}
EOF
       if ! diff /tmp/network.json /mnt/shadow/network.json 2>/dev/null; then
         echo "Updating JSON configuration ..."
         if mount -o remount,rw /mnt/shadow/ ; then
           cp /tmp/network.json /mnt/shadow/network.json; sync;
         fi
       fi
     elif [ -f /mnt/shadow/network.json ]; then
       echo "Removing JSON configuration ..."
       if mount -o remount,rw /mnt/shadow/ ; then
         rm -rf /mnt/shadow/network.json >/dev/null
       fi
     fi
   fi
}

case "$1" in
start)
	echo "Generating network configuration... "
        /lib/config/gen_config.sh network
        update_network_json 

	echo "Configuring network interfaces... "
	sysctl -e -p /etc/sysctl.conf >/dev/null 2>&1
	ifup -a
	echo "done."
	;;
stop)
	check_network_file_systems
	check_network_swap

	echo "Deconfiguring network interfaces... "
	ifdown -a
	echo "done."
	;;

force-reload|restart)
	echo "Running $0 $1 is deprecated because it may not enable again some interfaces"
	echo "Reconfiguring network interfaces... "
	ifdown -a || true
	echo "Generating network configuration... "
        /lib/config/gen_config.sh network
        update_network_json
	ifup -a
	echo "done."
	;;

*)
	echo "Usage: /etc/init.d/networking {start|stop}"
	exit 1
	;;
esac

exit 0

