#!/sbin/svc

# Core networking

MYLISTING="Networking core system"
CONTROL_GROUP="netconf"
NETCONFDIR="/etc/network"

start() {
    local f
    local intrf

    # Load and configure the VLAN support module if required
    if [ "$(echo ${NETCONFDIR}/vlan*)" != "${NETCONFDIR}/vlan*" ]
    then
        [ ! -e /proc/net/vlan/config ] && modprobe 8021q
        vconfig set_name_type vlan_plus_vid
    fi

    cd "${INITDIR2}"
    # All the regular interfaces and then the VLANs
    for f in "${NETCONFDIR}/eth"* "${NETCONFDIR}/vlan"*
    do
        enable="False"
        onboot="False"

        [ -r "${f}" ] && source "${f}"
        if gcs_truefalse "${enable}" && gcs_truefalse "${onboot}"
        then
            intrf="$(basename "$f")"

            if [ ! -e "net_${intrf}" ]
            then
                # Create a new service script for the interface
                sed -e "s/^INTRF=.*/INTRF=${intrf}/" \
                                < "/usr/share/svc_net/net.generic" > "net_${intrf}"
                chmod 0755 "net_${intrf}"
            fi

            svc $svc_modeflag net_$intrf running \
                || svc $svc_modeflag net_$intrf start
        fi
    done

    # Now wireless
    for f in "${NETCONFDIR}/wlan"*/dev
    do
        [ ! -f "${f}" ] && continue
        enable="True"
        source "${f}"
        gcs_truefalse "${enable}" || continue

        # create new service scripts, if necessary
        if [ ! -e "net_${device}.link" ]
        then
            sed -e "s/^INTRF=.*/INTRF=${device}/" \
                < "/usr/share/svc_net/wlan_link.generic" \
                > "net_${device}.link"
            chmod 0755 "net_${device}.link"
        fi

        if [ ! -e "net_${device}.ip" ]
        then
            sed -e "s/^INTRF=.*/INTRF=${device}/" \
                < "/usr/share/svc_net/wlan_cli.generic" \
                > "net_${device}.ip"
            chmod 0755 "net_${device}.ip"
        fi

        svc ${svc_modeflag} "net_${device}.link" start
        wait_for_file "/var/run/wpa_supplicant/$device"
        svc ${svc_modeflag} "net_${device}.ip" start
    done

    # Enable forwarding between interfaces
    echo 1 > /proc/sys/net/ipv4/ip_forward
}

stop() {
    local s

    cd "${INITDIR2}"
    for s in net_*
    do
        if [ "${s}" != "net_base" -a "${s}" != "net_generic" ]
        then
            svc $svc_modeflag $s running && svc $svc_modeflag $s stop
        fi
    done

    # Harmless if we didn't load it
    rmmod 8021q > /dev/null 2>&1 || true
}

# vim: ts=4:sw=4:expandtab:syntax=sh
