#!/bin/bash -e
### BEGIN INIT INFO
# Provides:          networking ifupdown
# Required-Start:    mountkernfs $local_fs urandom
# Required-Stop:     $local_fs
# Default-Start:     S
# Default-Stop:      0 6
# Short-Description: Raise network interfaces.
# Description:       Prepare /run/network directory, ifstate file and raise network interfaces, or take them down.
### END INIT INFO

# ajout sur le systeme, j'hérite de /proc/cmdline qui contient (grace à grub par exemple)
#   location=auto
#       aka dhcp (first)
#   location=whatersuffixfordir
#       domain rattached
# et ce qui doit correspondre à un /etc/network/loc-${location}/ avec dedans:
#  /etc/network/loc-${location}/interfaces:
#    eth1;192.168.1.2/24
#    dummy0;126.0.0.254/30
#  /etc/network/loc-${location}/gateways:
#    0.0.0.0/0;192.168.1.254
#  /etc/network/loc-${location}/resolver:
#    192.168.1.1

# TODO
#  - be IFSTATE compliant for ifup/down
#  - set the interface state at first, like with a loc-${location}/state
#  - clean the parts from debian networking skeleton

PATH="/sbin:/bin"
RUN_DIR="/run/network"
IFSTATE="$RUN_DIR/ifstate"

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

. /lib/lsb/init-functions

# this is lying:> do not use
CONFIGURE_INTERFACES=yes
EXCLUDE_INTERFACES=
VERBOSE=no

[ -f /etc/default/networking ] && . /etc/default/networking

[ "$VERBOSE" = yes ] && verbose=-v

check_ifstate() {
    if [ ! -d "$RUN_DIR" ] ; then
	if ! mkdir -p "$RUN_DIR" ; then
	    log_failure_msg "can't create $RUN_DIR"
	    exit 1
	fi
    fi
    if [ ! -r "$IFSTATE" ] ; then
	if ! :> "$IFSTATE" ; then
	    log_failure_msg "can't initialise $IFSTATE"
	    exit 1
	fi
    fi
}

set_location () {
    # either otherride anything up there, or smoothly cooperate with it
    # second option first, and * is a domain
    case "$1" in
        "auto")
            echo -----auto-----
            dhclient eth1
            ;;
        *)
            if [ ! -e "/etc/network/loc-$1" ]; then
                echo "location is not know!"
                return 1
            fi
            #### classical ipv4
            for ifacenet in $( cat /etc/network/loc-$1/interfaces ); do
                if [ -z "$ifacenet" ]; then
                    echo "not knowing anythin.." >/dev/stderr
                    return 1
                fi
                dev=${ifacenet/;*/}
                inet=${ifacenet/*;/}
                ifconfig $dev $inet up
            done
            #### and the gateway to outside
            for maskgate in $( cat /etc/network/loc-$1/gateways ); do
                if [ -z "$maskgate" ]; then
                    echo "not knowing anythin.." >/dev/stderr
                fi
                netmask=${maskgate/;*/}
                gateway=${maskgate/*;/}
                route add -net $netmask gw $gateway
            done
            #### then the names
            cp /etc/resolv.conf-$1 /etc/resolv.conf
            #### ipv6 needed could be too
            #net_set "/etc/network/loc-$1/ipv6" "ip addr add %s dev %s"
            #### special routing things
            #sys_set "/etc/network/loc-$1/systhings" write
            #### wifi could be
            #wifi_set "/etc/network/loc-$1/wifi"
            ;;
    esac
}

get_location () {
    for paramvalue in $( cat /proc/cmdline ); do
        param=${paramvalue/=*/}
        value=${paramvalue/*=/}
        if [ "$param" == "location" ]; then
            #set_location $value
            echo $value
            break
        fi
    done
}

general_start () {
    ifconfig lo inet 127.0.0.1/8 up
    ifconfig eth1 up
    location=$( get_location | sed -E 's/[\ \t].*$//' )
    if [ $? -ne 0 ]; then
        return 1
    fi
    echo $location > ${RUN_DIR}/location
    set_location $location
    #set_ifstate eth1 lo
}

general_stop () {
    location=$( cat ${RUN_DIR}/location )
    for maskgate in $( cat /etc/network/loc-${location}/gateways ); do
        if [ -z "$maskgate" ]; then
            return 1
        fi
        netmask=${maskgate/;*/}
        gateway=${maskgate/*;/}
        echo -n "del:$netmask,$gateway "
        route del -net $netmask gw $gateway
    done
    ifconfig lo down
    ifconfig eth1 down
    ifconfig wlan0 down
}

case "$1" in
start)
    check_ifstate
    log_action_begin_msg "Configuring network interfaces"
    general_start || exit
    log_action_end_msg $?
    ;;

stop)
    check_ifstate
    log_action_begin_msg "Deconfiguring network interfaces"
    general_stop || exit
    log_action_end_msg $?
    ;;

force-reload|restart)
    log_action_begin_msg "Reconfiguring network interfaces"
    check_ifstate
    general_stop
    echo -n .
    general_start
    log_action_end_msg $?
    ;;

status)
    ip addr show
    ;;
*)
    echo "Usage: /etc/init.d/networking {start|stop|status|restart|force-reload}"
    exit 1
    ;;
esac

exit 0

# vim: noet ts=8
