#!/bin/sh /etc/rc.common

START=99

EXTRA_COMMANDS=" init_ipt "
EXTRA_COMMANDS=" $EXTRA_COMMANDS on off shutdown die restart "
EXTRA_COMMANDS=" $EXTRA_COMMANDS qos_flag status "
EXTRA_COMMANDS=" $EXTRA_COMMANDS on_limit off_limit set_limit reset_limit apply show_limit limit_flag "
EXTRA_COMMANDS=" $EXTRA_COMMANDS change_band show_band "
EXTRA_COMMANDS=" $EXTRA_COMMANDS set_type show_cfg "
EXTRA_COMMANDS=" $EXTRA_COMMANDS on_guest show_guest on_xq show_xq "
EXTRA_COMMANDS=" $EXTRA_COMMANDS set_seq get_seq "
EXTRA_COMMANDS=" $EXTRA_COMMANDS nprio reserve "
EXTRA_COMMANDS=" $EXTRA_COMMANDS supress_on supress_off supress_update "

#FIX-QOS only
EXTRA_COMMANDS=" $EXTRA_COMMANDS device_in device_out "


EXTRA_HELP="
--stat:
    on/off/shutdown/die/restart
    qos_flag/status
--limit:
    on_limit/set_limit min/max/all [MAC] MAX-UP MAX-DOWN MIN-UP MIN-DOWN
    off_limit/reset_limit [MAC]
    show_limit
    apply
    limit_flag [MAC] on/off
    show_limit_flag [MAC]
--band:
    change_band UP-RATE DOWN-RATE
    show_band
--type:
    set_type [auto/min/max/service]
    show_cfg
--guest & xq:
    on_guest UP DOWN
    show_guest
    on_xq UP DOWN
    show_xq
--seq:
    get_seq
    set_seq [auto/game/web/video]
--supress:
    supress_update/supress_on/supress_off
--others:
    nprio add/del IP HIGH_PRIO_WITHOUT_LIMIT/HIGH_PRIO_WITH_BANDLIMIT
    reserve add/del IP video
"

#only support 3 types, CTF/STD/FIX
QOS_VER="FIX"   #here, value would be replace by sed

if [ "$QOS_VER" == "CTF" ]; then
    EXECMD="/usr/bin/lua /usr/sbin/miqosd ctf"
    EXECMD_C="/usr/sbin/miqosc"
    export PROCLINE=$EXECMD
    export PROCFLAG=$PROCLINE
elif [ "$QOS_VER" == "STD" ]; then
    EXECMD="/usr/bin/lua /usr/sbin/miqosd std"
    EXECMD_C="/usr/sbin/miqosc"
    export PROCLINE=$EXECMD
    export PROCFLAG=$PROCLINE
elif [ "$QOS_VER" == "HWQOS" ]; then
    EXECMD="/usr/bin/lua /usr/sbin/miqosd hwqos"
    EXECMD_C="/usr/sbin/miqosc"
    export PROCLINE=$EXECMD
    export PROCFLAG=$PROCLINE
else
    EXECMD_C="/usr/sbin/miqosc"
fi

#not supported command check
ignore(){
    null_types=$1
    [ -z "$null_types" ] && return 0;

    for t in $null_types;
    do
        [ "$t" == "$QOS_VER" ] && echo "not supported for $QOS_VER." && exit 0;
    done
}

#-------------------------------
init_ipt(){
   /usr/sbin/miqosd_init.sh
   return $?
}

#-------------------------------
#0,ok; non-0, Nok
start() {
    if [ "$QOS_VER" == "FIX" ]; then
        #check NETMODE 1stly
        local mode
        mode=`uci -q get xiaoqiang.common.NETMODE`
        if [ "$mode" == "wifiapmode" -o "$mode" == "lanapmode" ]; then
            echo "In AP mode, disable QoS. exit!"
            $EXECMD_C off
            return 0
        fi

        local enabled
        enabled=`uci -q get miqos.settings.enabled`
        if [ "$enabled" = "1" ]; then
            $EXECMD_C on
        else
            $EXECMD_C off
        fi
        return 0
    else
        #check NETMODE 1stly
        local mode
        mode=`uci get xiaoqiang.common.NETMODE 2>/dev/null`
        if [ "$mode" == "wifiapmode" -o "$mode" == "lanapmode" ]; then
            echo "In AP mode, disable QoS. exit!"
            return 0
        fi
        /usr/sbin/supervisord start

        qos_flag
        if [ $? -ne "0" ]; then
                echo "miqos is not enabled, only keep top layer1 queue."
                running
                if [ $? -eq "0" ]; then
                    $EXECMD_C off
                fi
                return 0
        fi

        # enable QoS Service
        $EXECMD_C on

        return 0
    fi
}

stop() {
    shutdown
}

shutdown(){
    $EXECMD_C shutdown
}

restart(){
    stop &>/dev/null
    start &>/dev/null
    return 0
}

on(){
    uci set miqos.settings.enabled=1
    uci commit miqos
    if [ -f "/etc/init.d/qca-nss-ecm" ]; then
        /etc/init.d/qca-nss-ecm restart 1 &>/dev/null
    fi

    if [ "$QOS_VER" == "STD" ]; then
        uci set hwnat.switch.miqos=1
        uci commit hwnat

        /etc/init.d/hwnat stop &>/dev/null
    fi

    start
}

off(){
    #set -x
    uci set miqos.settings.enabled=0
    uci commit miqos
    if [ -f "/etc/init.d/qca-nss-ecm" ]; then
        /etc/init.d/qca-nss-ecm restart 0 &>/dev/null
    fi

    if [ "$QOS_VER" == "FIX" ]; then
        stop #clean directly
    elif [ "$QOS_VER" == "STD" ]; then
        uci set hwnat.switch.miqos=0
        uci commit hwnat

        running
        if [ $? == 1 ]; then
            start        # if not running, invoke start, and then off it
        fi

        $EXECMD_C off     #close htb rules

        #try to start hwnat whatever enough memory or not
        /etc/init.d/hwnat start &>/dev/null
    else
        running
        if [ $? == 1 ]; then
            start        # if not running, invoke start, and then off it
        fi
        $EXECMD_C off     #close htb rules
    fi

    return 0
}

die(){
    #
    $EXECMD_C die &
    /usr/sbin/supervisord stop
    return 0
}

#-------------------------------
#return: 0,enabled; 1 disabled
qos_flag(){
    #config_load "miqos"
    local st
    st=`uci get miqos.settings.enabled 2>/dev/null`
    if [ $st -eq "0" ]; then
            return 1
    fi
    return 0
}

# status: 0, actived, 1, inactived
status(){
    qos_flag
    if [ $? == 1 ]; then
        return 1
    fi

    if [ "$QOS_VER" == "FIX" ]; then
        return 0;
    else
        /usr/sbin/supervisord status
        if [ $? == 1 ]; then
            return 1
        fi
        return 0
    fi
}

running(){
    ignore "FIX"
    /usr/sbin/supervisord status
    if [ $? == 1 ]; then
        return 1
    fi
    return 0
}

#-------------------------------
on_limit(){
    $EXECMD_C on_limit $@
}

off_limit(){
    $EXECMD_C off_limit $@
}

set_limit(){
    $EXECMD_C set_limit $@
}

reset_limit(){
    $EXECMD_C reset_limit $@
}

apply(){
    $EXECMD_C apply $@
}

show_limit(){
    $EXECMD_C show_limit $@
}

limit_flag(){
    $EXECMD_C limit_flag $@
}


#-------------------------------
change_band(){
    $EXECMD_C change_band $@
}

show_band(){
    $EXECMD_C show_band $@
}

#-------------------------------

set_type(){
    ignore "FIX"
    $EXECMD_C set_type $@
}

show_cfg(){
    $EXECMD_C show_cfg $@
}

#-------------------------------

on_guest(){
    $EXECMD_C on_guest $@
}


show_guest(){
    $EXECMD_C show_guest $@
}

on_xq(){
    $EXECMD_C on_xq $@
}


show_xq(){
    $EXECMD_C show_xq $@
}

#-------------------------------

nprio(){
    ignore "FIX"
    $EXECMD_C nprio $@
}

reserve(){
    ignore "FIX"
    $EXECMD_C reserve $@
}

#-------------------------------
get_seq(){
    $EXECMD_C get_seq $@
}

set_seq(){
    $EXECMD_C set_seq $@
}

#-------------------------------

supress_on(){
    ignore "FIX"
    $EXECMD_C supress_host on
}

supress_off(){
    ignore "FIX"
    $EXECMD_C supress_host off
}

supress_update(){
    ignore "FIX"
    local app_name="2"  # xunyou
    [ -f /proc/flowmark_app ] && {
        ret=`cat /proc/flowmark_app | awk -v app_name=$app_name -F, '{ if($5!="" && $2==app_name){print($5) } }'`
        if [ "$ret" != "" ]; then
            echo [$ret]
            $EXECMD_C supress_host on
        else
            $EXECMD_C supress_host off
        fi
    }
}

#-------------------------------

device_in(){
    ignore "CTF STD"
    $EXECMD_C device_in $@
}

device_out(){
    ignore "CTF STD"
    $EXECMD_C device_out $@
}
