export PATH=$PROLOGUE_PATH.:/usr/local/bin:/usr/local/sbin:/usr/local/usr/bin:/var:/bin:/sbin$EPILOGUE_PATH
export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib/public:/usr/local/lib/private:/usr/local/lib/gpl:/lib:/usr/local/bas

if [ -f "/proc/device-tree/bolt/chip" ]; then
    CHIP=$(sed -n 's/^BCM\(.*\)$/\1/p' /proc/device-tree/bolt/chip | tr -d '\0')
elif [ -f /etc/brcmstb.conf ] ; then
    CHIP=$(sed -n 's/^PLAT=\(.*\)$/\1/p' /etc/brcmstb.conf | cut -d '-' -f 1)
else
    CHIP="Unknown"
fi
export PROMPT=$CHIP
export PS1='[$PROMPT:RG]\$ '

export BP3ATA_PATH=$(mount | grep /dev/flash-atao | cut -f3 -d' ')
if [ -f $BP3ATA_PATH/astra_ta_bp3.ver ]; then
    BP3VER=$(sed -n 's/.*v\([0-9]*\).*/\1/p' $BP3ATA_PATH/astra_ta_bp3.ver)
else
    BP3VER=2
fi

if [ $BP3VER -ge 3 ]; then
    export BP3LIC_PATH=$(mount | grep /dev/flash-bp3lico | cut -f3 -d' ')
else
    export BP3LIC_PATH=/tmp
fi

if [ -d /tmp/python ]; then
    export PATH=$PATH:/tmp/python/bin
    export PYTHONHOME=/tmp/python
    export PYTHONPATH=/tmp/python/lib
fi

function setconsole() {
    if [ ! -f /var/.console ]; then
        echo "Console output device: /dev/console" > /var/.console
    fi
    if [ "$#" -ne 1 ]; then
        cat /var/.console
    else
        case "$1" in
            1 | set )
                echo "Console output switched to: $(tty)" > /dev/console
                setconsole $(tty)
                if [ $? -eq 0 ]; then
                    echo "Console output device: $(tty)" > /var/.console
                fi
            ;;
            0 | reset )
                setconsole -r
                echo "Console output switched to: /dev/console"
                echo "Console output device: /dev/console" > /var/.console
            ;;
            *)
            echo "setconsole - No arguments show current console device"
            echo "setconsole [1|set] - Set console output to shell TTY"
            echo "setconsole [0|reset]  - Reset console output to /dev/console"
            ;;
        esac
    fi
}

function flowmgr() {
    if [ "$#" -ne 1 ]; then
        echo show > /proc/driver/flowmgr/cmd
    else
        case "$1" in
            1 | [yYeE] | enable )
                echo 1 > /proc/sys/net/flowmgr/enable
            ;;
            0 | [nNdD] | disable )
                echo 0 > /proc/sys/net/flowmgr/enable
            ;;
            *)
            echo "flowmgr - No arguments show status"
            echo "flowmgr [0|n|disable] - Disable flowmgr"
            echo "flowmgr [1|y|enable]  - Enable  flowmgr"
            ;;
        esac
    fi
}

function brshowmacs() {
    for x in `ls /sys/class/net`; do
        ethtool -i $x 2>&1 | grep -i bridge > /dev/null 2>&1
        if [ "0" -eq "$?" ]; then
            echo -e "\n**** Show MACs on $x ****"
            brctl showmacs $x
        fi
    done
}

function brshowstp() {
    for x in `ls /sys/class/net`; do
        ethtool -i $x 2>&1 | grep -i bridge > /dev/null 2>&1
        if [ "0" -eq "$?" ]; then
            echo -e "\n**** Show STP on $x ****"
            brctl showstp $x
        fi
    done
}

function brflush() {
    for x in `ls /sys/class/net`; do
        ethtool -i $x 2>&1 | grep -i bridge > /dev/null 2>&1
        if [ "0" -eq "$?" ]; then
            echo 1 > /sys/class/net/%x/bridge/flush
        fi
    done
}

# Additional Useful Aliases for debugging
alias lsnf='cat /proc/net/stat/nf_conntrack && cat /proc/net/nf_conntrack'
alias lsflownf='cat /proc/net/nf_conntrack_offload'
alias lsflowmgr='echo show > /proc/driver/flowmgr/cmd'
alias lsflow='lsflowmgr && lsflownf'
alias lsflowdb='echo "**** CientDB ****" && cat /proc/driver/flowmgr/db && echo "**** MulticastDB ****" && cat /proc/driver/flowmgr/mdb && echo "**** TunnelDB ****" && cat /proc/driver/flowmgr/tdb'
alias lsiptf='echo -e "\n**** FILTER TABLE ****" && iptables -t filter -n -L -v'
alias lsiptn='echo -e "\n****    NAT TABLE ****" && iptables -t nat -n -L -v'
alias lsiptm='echo -e "\n**** MANGLE TABLE ****" && iptables -t mangle -n -L -v'
alias lsiptr='echo -e "\n****    RAW TABLE ****" && iptables -t raw -n -L -v'
alias lsipt='lsiptf && lsiptn && lsiptm && lsiptr'
alias lsiptf6='echo -e "\n**** IPv6 FILTER TABLE ****" && ip6tables -t filter -n -L -v'
alias lsiptm6='echo -e "\n**** IPv6 MANGLE TABLE ****" && ip6tables -t mangle -n -L -v'
alias lsiptr6='echo -e "\n****    IPv6 RAW TABLE ****" && ip6tables -t raw -n -L -v'
alias lsipt6='lsiptf6 && lsiptm6 && lsiptr6'
alias lsebtf='echo -e "\n**** FILTER TABLE ****" && ebtables -t filter -L --Lc'
alias lsebtn='echo -e "\n****    NAT TABLE ****" && ebtables -t nat -L --Lc'
alias lsebtb='echo -e "\n**** BROUTE TABLE ****" && ebtables -t broute -L --Lc'
alias lsebt='lsebtf && lsebtn && lsebtb'
alias lsversion='latticecli -n "version"'
alias swversion='lsversion'
alias lsnetstat='cat /proc/net/dev && cat /proc/net/dev_eth'
alias lsnet='ip addr'
alias lsneigh='ip neigh && ip -6 neigh'
alias lsbr='brctl show && brshowmacs'
alias lsdhcp4cfg='cat /var/udhcpd/udhcpd.conf'
alias lsdhcp6cfg='cat /var/dhcp6s*.conf'
alias lsradvdcfg='cat /var/radvd*.conf'
alias dhdarm='dhd'
alias wl0='wl -i wl0'
alias wl1='wl -i wl1'
alias wl2='wl -i wl2'

if [ -e /proc/driver/wler/pkt_stats ]; then
    alias lswl='cat /proc/driver/wler/pkt_stats'
fi

if [ -e /proc/driver/gfap/cmd ]; then
    alias gfapcfg='echo show > /proc/driver/gfap/cmd && echo rgshow > /proc/driver/gfap/rgcmd'
    alias gfapcounter='cat /proc/driver/gfap/stats/*'
    alias showgfap='gfapcfg && gfapcounter'
    alias lsgfap='showgfap'
elif [ -e /proc/driver/gfap ]; then
    alias lsflowgfap='cat /proc/driver/gfap/show_flows'
    alias gfapcfg='cat /proc/driver/gfap/gfap_config'
    alias gfapdqm='cat /proc/driver/gfap/dqm_stats'
    alias gfapcounter='(cd /proc/driver/gfap/ && cat show_hwcounters && cat show_misccounters && cat show_msgcounters && cat show_txrxcounters)'
    alias showgfap='gfapcfg && gfapcounter'
    alias lsgfap='showgfap'
fi

function runner_bbh_counter() {
    echo -e "\n\n*** Runner BBH Counters for Interface" $1 "***"
    bs /d/b rgc1 $1
    bs /d/b tgc1 $1
}

function runner_br_counter() {
    case $1 in
        0)
            echo -e "\n\n*** Runner Bridge Counters for Port" $1 "(WAN-0) ***"
            ;;
        1)
            echo -e "\n\n*** Runner Bridge Counters for Port" $1 "(LAN) ***"
            ;;
        2)
            echo -e "\n\n*** Runner Bridge Counters for Port" $1 "(LAN) ***"
            ;;
        3)
            echo -e "\n\n*** Runner Bridge Counters for Port" $1 "(LAN) ***"
            ;;
        8)
            echo -e "\n\n*** Runner Bridge Counters for Port" $1 "(WIFI) ***"
            ;;
        9)
            echo -e "\n\n*** Runner Bridge Counters for Port" $1 "(WAN-1) ***"
            ;;
        *)
            echo -e "\n\n*** Runner Bridge Counters for Port" $1 "***"
            ;;
    esac
    bs /d/r pbpc $1
}

ls /proc/device-tree | grep runner > /dev/null
if [ $? -eq 0 ]; then
    if [ ! -f /var/bdmf_sh_id ]; then
        . /usr/local/etc/bdmf_init.sh
    fi
    alias lsflowrunner='bs /b/e ip_class;echo -e "\n\n";bs /b/e iptv'
    alias runnercfg='bs /b/e system'
    alias runnercounter='runner_bbh_counter DOCSIS && runner_bbh_counter EMAC_0 && runner_bbh_counter EMAC_1 && runner_bbh_counter EMAC_2'
    alias runnerbrcounter='runner_br_counter 0 && runner_br_counter 1 && runner_br_counter 2 && runner_br_counter 3'
fi

if [ -e /proc/driver/dqm ]; then
    alias dqm='cat /proc/driver/dqm/mib'
    alias showdqm='cat /proc/driver/dqm/status'
    alias lsdqm='showdqm'
fi

if [ -e /proc/driver/fpm ]; then
    alias fpm='cat /proc/driver/fpm/stats'
    alias showfpm='cat /proc/driver/fpm/stats'
    alias lsfpm='showfpm'
fi

if [ -e /proc/driver/dqnet ]; then
    alias showdqnet='echo stats > /proc/driver/dqnet/cmd && cat /proc/driver/dqnet/nethooks'
    alias lsdqnet='showdqnet'
fi

function ethswmib()
{
    echo -e "\n\n"
    echo $1 $2 > /proc/driver/ethsw/mib
    cat /proc/driver/ethsw/mib
}

if [ -e /proc/driver/ethsw/mib ]; then
    alias ethswmiball='ethswmib a 0 && ethswmib a 1 && ethswmib a 2 && ethswmib a 3 && ethswmib a 4 && ethswmib a 5 && ethswmib a 7 && ethswmib a 8'
    alias ethswmibtx='ethswmib t 0 && ethswmib t 1 && ethswmib t 2 && ethswmib t 3 && ethswmib t 4 && ethswmib t 5 && ethswmib t 7 && ethswmib t 8'
    alias ethswmibrx='ethswmib r 0 && ethswmib r 1 && ethswmib r 2 && ethswmib r 3 && ethswmib r 4 && ethswmib r 5 && ethswmib r 7 && ethswmib r 8'
fi

function ddb()
{
    if [ "$#" -eq 0 ]; then
        echo "ddb <key> <match> <0|1>"
        echo "key: func, file or module"
        echo "match: For key module: Module name - flowmgr, dqnet, fpm etc"
        echo "match: For key file: File name - flowmgr_core.c, dqnet_core.c etc"
        echo "match: For key func: Function name"
        echo "0|1: 0-Disable 1-Enable. Display all match lines if this argument is omitted"
    else
        case "$3" in
            1 | [yYeE] | enable )
                echo "$1 $2 +pmfl" > /sys/kernel/debug/dynamic_debug/control
            ;;
            0 | [nNdD] | disable )
                echo "$1 $2 =_" > /sys/kernel/debug/dynamic_debug/control
            ;;
            *)
                if [ "$#" -eq 2 ]; then
                    cat /sys/kernel/debug/dynamic_debug/control | grep $2
                else
                    echo "ddb <key> <match> <0|1>"
                    echo "key: func, file or module"
                    echo "match: For key module: Module name - flowmgr, dqnet, fpm etc"
                    echo "match: For key file: File name - flowmgr_core.c, dqnet_core.c etc"
                    echo "match: For key func: Function name"
                    echo "0|1: 0-Disable 1-Enable. Display all match lines if this argument is omitted"
                fi
            ;;
        esac
    fi
}
