#!/bin/sh

############
# CHANGELOG
#===========
# 2010/04/06    CRMS00222507    Thanh lam NGUYEN
#               Fix a bug introduced by CRMS00216879 making auto to always be true...
#
# 2010/03/11    CRMS00216879    Thanh lam NGUYEN
#               Check that at least a speed or a duplex is given in 'set' mode
#
# 2010/03/05    CRMS00218043    Thanh lam NGUYEN
#               Change to new "eth" syntax
#
# 2010/02/04    CRMS00209271    Thanh lam
#               Change the display for it to match the required display syntax
################################


. /etc/init.d/rc.config
. $DEBUGBINPATH/.functions

RET=$OK

#
# function definition
help() {
# CRMS00216879
    echo "SYNTAX: ${0##*/} [lan|pc|set <lan|pc> [10|100|auto] [half|full|auto]]"
# CRMS00216879 END
    echo ""
    echo "The is no resulting display for the 'set' command."
    echo "For the other command a line describing the lan or pc port is displayed."
    echo "NOTE: if either the speed or the duplex mode is auto, both are set to auto"
    echo ""
}

#
# process command line options
# CRMS00216879
auto=
speed=
duplex=
arg=0
# CRMS00216879 END

while [ -n "$1" ]; do
    case "$1" in
        set) : "set mode"
            mode=set
        ;;
        [Pp][Cc]) : "PC"
            target="${target:+$target }PC"
        ;;
        [Ll][Aa][Nn]) : "LAN"
            target="${target:+$target }LAN"
        ;;
        10|100) : "speed"
            speed=$1
            auto=no
# CRMS00216879
            arg=1
# CRMS00216879 END
        ;;
        [Hh][Aa][Ll][Ff]|[Ff][Uu][Ll][Ll]) : "duplex"
            duplex=$1
            auto=no
# CRMS00216879
            arg=1
# CRMS00216879 END
        ;;
        [Aa][Uu][Tt][Oo]) : "auto"
            auto=yes
# CRMS00216879
            arg=1
# CRMS00216879 END
        ;;
        *) : "Error"
            help
            EXITNOK
        ;;
    esac
    shift
done

if [[ "${mode:-}" == "set" ]]; then
    # we are in set mode
    if [ -z "${target:-}" ]; then
        # a target has to be given
        echoerror "Either PC or LAN have to be declared to be set!!!"
        EXITNOK
    fi
    if [ "${target//[[:blank:]]}" != "$target" ]; then
        # we have several target, this is not autorised
        echoerror "We can only set one either PC or LAN at a time !!!"
        EXITNOK
    fi
# CRMS00216879
    if [[ $arg -ne 1 ]]; then
        # There should be at least 1 parameter to set
        echoerror "Neither speed nor duplex is changed !!!"
        EXITNOK
    fi
# CRMS00216879 END
    readCurrentConfig
# CRMS00216879
    cmd="ETHLCFG_${target}_AUTO=${auto}"
# CRMS00222507
    [[ -n "${speed}" ]] && cmd="${cmd:+$cmd }ETHLCFG_${target}_SPEED=${speed}"
# CRMS00222507 END
    [[ -n "${duplex}" ]] && cmd="${cmd:+$cmd }ETHLCFG_${target}_DUPLEX=${duplex}"
    eval "$cmd"
# CRMS00216879 END
    configEthHw
    RET=$?
else
    # we are in get status mode
    readCurrentConfig

    target="${target:-LAN PC}"
    for tgt in $target; do
        status="$tgt"
        eval "port=\${ETHLCFG_${tgt}}"
# CRMS00218043
        idle=$(eth link $port '?'| awk '/down/{print "1"}/up/{print "0"}')
        speed=$(eth speed $port '?')
# crms00209271
        duplex=$(eth duplex $port '?') duplex=${duplex% *}
# crms00209271 done
# CRMS00218043 END
        if [ $idle -eq 1 ]; then
            status="$status idle"
        else
            status="$status $speed/$duplex"
        fi
        echo "$status"
        RET=$OK
    done
fi
[[ $RET -eq $OK ]] && { EXITOK; } || { EXITKO; }
