#!/opt/sum/bin/bash

source ./cmd_utils.sh
source ./kernel_config_utils.sh
source ./net_utils.sh

tc_check() {
    is_error=false
    if ! kernel_config_check CONFIG_NET_SCH_FIFO; then
        is_error=true
    fi
    if ! kernel_config_check CONFIG_NET_SCH_HTB sch_htb; then
        is_error=true
    fi
    if ! kernel_config_check CONFIG_NET_SCH_NETEM sch_netem; then
        is_error=true
    fi
    if ! kernel_config_check CONFIG_NET_SCH_SFQ sch_sfq; then
        is_error=true
    fi
    if ! kernel_config_check CONFIG_NET_SCH_TBF sch_tbf; then
        is_error=true
    fi
    if [ "$is_error" == "true" ]; then
        return 1
    fi

    local ifname="dodo"

    if ! netif_new $ifname; then
        return 1
    fi

    is_error=false
    if ! tc_rule_check $ifname bfifo limit 1Mb; then
        is_error=true
    fi
    if ! tc_rule_check $ifname handle 1: htb default 20; then
        is_error=true
    fi
    if ! tc_rule_check $ifname netem loss 10%; then
        is_error=true
    fi
    if ! tc_rule_check $ifname netem delay 10ms; then
        is_error=true
    fi
    if ! tc_rule_check $ifname sfq perturb 10; then
        is_error=true
    fi
    if ! tc_rule_check $ifname tbf rate 10Mbit latency 1000ms burst 100kb; then
        is_error=true
    fi
    netif_delete $ifname
    if [ "$is_error" == "true" ]; then
        return 1
    fi

    return 0
}

