#! /bin/bash
. /usr/lib/network/network

report_iproute()
{
    report_fail "$*"
    ip addr flush $INTERFACE &>/dev/null
    quirk "nodown" || ip link set $INTERFACE down &>/dev/null 
    exit 1
}

ethernet_up() {
    load_profile $1

    if [[ ! -e /sys/class/net/$INTERFACE ]]; then
        if ! echo "$INTERFACE"|grep ":"; then
            report_iproute "Interface $INTERFACE does not exist"
        fi
    fi  

    report_debug ethernet_iproute_up ifup
    ip link set $INTERFACE up
    sleep 2

    if ip link show $INTERFACE|grep -Fq "NO-CARRIER"; then
       report_fail "No connection"
       return 1
    fi
     
    if checkyesno ${AUTH8021X:-no}; then 
        . ${SUBR_DIR}/8021x
        [[ -z "$WPA_CONF" ]] && WPA_CONF="/etc/wpa_supplicant.conf"
        [[ -z "$WPA_OPTS" ]] && WPA_OPTS="-Dwired"

        if ! start_wpa $INTERFACE $WPA_CONF $WPA_OPTS; then
            report_fail "wpa_supplicant did not start, possible configuration error"
            return 1
        fi
        
        if ! wpa_check "$INTERFACE"; then
            ip link set $INTERFACE down
            return 1
        fi 
    fi

    case $IP in
    dhcp)
        # Clear remaining pid files. 
        rm -f /var/run/dhcpcd-${INTERFACE}.{pid,cache} >/dev/null 2>&1

        # If using own dns, tell dhcpcd to NOT replace resolv.conf
        [[ -n "$DNS" ]] && DHCP_OPTIONS="-C resolv.conf $DHCP_OPTIONS"
        
        report_debug ethernet_iproute_up dhcpcd -qL -t "${DHCP_TIMEOUT:-10}" $DHCP_OPTIONS "$INTERFACE"       
        if ! dhcpcd -qL -t ${DHCP_TIMEOUT:-10} $DHCP_OPTIONS $INTERFACE; then
            report_iproute "DHCP IP lease attempt failed"
        fi
        ;;
    static)
        if [[ -n "$ADDR" ]]; then
            report_debug ethernet_iproute_up ip addr add "$ADDR/24" brd + dev "$INTERFACE"
            if ! ip addr add ${ADDR}/24 brd + dev $INTERFACE; then
                report_iproute "Could not configure interface"
            fi
        fi
        if [[ -n "$GATEWAY" ]]; then
            report_debug ethernet_iproute_up ip route add default via "$GATEWAY"
            if ! ip route add default via $GATEWAY; then
                report_iproute "Adding gateway failed"
            fi
        fi
        ;;
      *)
        report_iproute "Profile report_iproute: IP must be either 'dhcp' or 'static'"
        ;;
    esac

    if [[ -n "$IPCFG" ]]; then
        for line in "${IPCFG[@]}"; do
        
            report_debug ethernet_iproute_up ip "$line"            
            if ! ip $line; then
                report_iproute "Could not configure interface"
            fi
        done
    fi

    # Set hostname
    if [[ -n "$HOSTNAME" ]]; then
        report_debug ethernet_iproute_up hostname "$HOSTNAME"
        if ! hostname $HOSTNAME; then
            report_iproute "Cannot set hostname"
        fi
    fi

    # Generate a new resolv.conf
    if [[ -n "$DNS" ]]; then
        : >/etc/resolv.conf 
        [[ -n "$DOMAIN" ]] && echo "domain $DOMAIN"   >>/etc/resolv.conf
        [[ -n "$SEARCH" ]] && echo "search $SEARCH"   >>/etc/resolv.conf

        if [[ -n "$DNS" ]]; then
            for dns in ${DNS[@]}; do
                echo "nameserver $dns" >>/etc/resolv.conf
            done
        fi
    fi
    return 0
}

ethernet_down() {
    load_profile $1

    if [[ "$IP" == "dhcp" ]]; then
        if [[ -f /var/run/dhcpcd-${INTERFACE}.pid ]]; then
            report_debug ethernet_iproute_down dhcpcd -qx "$INTERFACE"
            dhcpcd -qx $INTERFACE
        fi
    fi
    
    report_debug ethernet_iproute_down if_down
    ip addr flush $INTERFACE
    quirk "nodown" || ip link set $INTERFACE down

}

# Returns status of profile - is it still functional?
ethernet_status() {
    if ! ip link show dev ra0|grep -q "state UP"; then
        return 1
    fi
}

ethernet_$1 $2
exit $?
# vim: set ts=4 et sw=4:
