#!/usr/bin/env sh


PARAMS="RA_stage RaeEnable LoggingLevel RaecheckLoggingLevel"


function usage()
{
    echo "Usage: raetest [METHOD] param{=value}"
    echo "   eg: raetest get {param}"
    echo "   eg: raetest set {param}={value}"
    echo "   eg: raetest set {param} {value}"
    echo "   eg: raetest commit"
    echo "   eg: raetest init_report_time [value]"
    echo "   eg: raetest restart"
    echo ""
    echo "METHOD"
    echo "    set                     Set value"
    echo "    get                     Get value"
    echo ""
    echo "param                       value"
    echo "    RA_stage                'prod/qa/dev'"
    echo "    RaeEnable               'true/false/1/0' means enable, other value mean disable"
    echo "    LoggingLevel            'Debug/Notice/Error' means open Rae log, other value mean close log"
    echo "    RaecheckLoggingLevel    'Debug/Notice/Error' means open RaeCheck log, other value mean close log"
    echo "    init_report_time        '1~99999/random/null' means how long time to wait for reporting the first period data after rae start"
    echo ""
}

function get_param_RA_stage()
{
    echo "RA_stage is `uci get rae.@rae[0].RaeStage`"
}


function set_param_RA_stage()
{
    raestage=`uci get rae.@rae[0].RaeStage`
    if [ $1 != "dev" ] && [ $1 != "qa" ] && [ $1 != "prod" ]; then
        echo "raetest error: RA_Stage value must be [dev/qa/prod]!"
        exit 1
    fi

    uci set rae.@rae[0].RaeStage="$1"
    uci commit rae
    echo "raestage=$raestage 1=$1"
    if [ "$raestage" != "$1" ];then
        /etc/init.d/rae restart
    fi
}


function get_param_RaeEnable()
{
    uci get rae.@rae[0].RaeEnable
}


function set_param_RaeEnable()
{
    raeEnable=`uci get rae.@rae[0].RaeEnable`
    if [ $1 = "true" ] || [ $1 = "True" ] || [ $1 = "TRUE" ] || [ $1 = "1" ]; then
        uci set rae.@rae[0].RaeEnable='1'
        uci commit rae
    else
        uci set rae.@rae[0].RaeEnable='0'
        uci commit rae
    fi
    echo "raeEnable=$raeEnable 1=$1"
    if [ "$raeEnable" != "$1" ];then
        /etc/init.d/rae restart
    fi
}


function get_param_LoggingLevel()
{ 
    pid_err=`ps | grep 'grep \-E user.err rae'| awk '{print $1}'`
    pid_info=`ps | grep 'grep \-E user.info rae' | awk '{print $1}'`
    pid_debug=`ps | grep 'rae\\\\\[' | awk '{print $1}'`

    if [ "$pid_err" != "" ]; then
        echo "LoggingLevel is Error"
    elif [ "$pid_info" != "" ]; then
        echo "LoggingLevel is info"
    elif [ "$pid_debug" != "" ]; then
        echo "LoggingLevel is debug"
    else
        echo "not open rae log"
    fi
}


function set_param_LoggingLevel()
{
    id=`ps | grep 'rae\\\\\[' | awk '{print $1}'`

    if [ "$id" != "" ];then
        gid=`cat /proc/$id/stat | awk '{print $5}'`
        pids=`grep -h "$gid" /proc/*/stat 2>/dev/null | awk '{print $1}'`
        for pid in $pids; do
            kill -9 $pid 2>/dev/null
        done
    fi

    if [ $1 = "Debug" ] || [ $1 = "debug" ] || [ $1 = "DEBUG" ]; then
        touch /tmp/logdbg;logread -f | grep " rae\[" &
    elif [ $1 = "Notice" ] || [ $1 = "notice" ] || [ $1 = "NOTICE" ]; then
        touch /tmp/logdbg;logread -f | grep -E "user.info rae\[|user.err rae\["&
    elif [ $1 = "Error" ] || [ $1 = "error" ] || [ $1 = "ERROR" ]; then
        touch /tmp/logdbg;logread -f | grep -E "user.err rae\["&
    fi
}


function get_param_RaecheckLoggingLevel()
{
    pid_err=`ps | grep 'grep \-E user.err rae_check'| awk '{print $1}'`
    pid_info=`ps | grep 'grep \-E user.info rae_check' | awk '{print $1}'`
    pid_debug=`ps | grep 'rae_check\\\\\[' | awk '{print $1}'`

    if [ "$pid_err" != "" ]; then
        echo "RaecheckLoggingLevel is Error"
    elif [ "$pid_info" != "" ]; then
        echo "RaecheckLoggingLevel is info"
    elif [ "$pid_debug" != "" ]; then
        echo "RaecheckLoggingLevel is debug"
    else
        echo "not open Raecheck log"
    fi
}


function set_param_RaecheckLoggingLevel()
{
    id=`ps | grep 'rae_check\\\\\[' | awk '{print $1}'`
    if [ "$id" != "" ];then
        gid=`cat /proc/$id/stat | awk '{print $5}'`
        pids=`grep -h "$gid" /proc/*/stat 2>/dev/null | awk '{print $1}'`
        for pid in $pids; do
            kill -9 $pid 2>/dev/null
        done
    fi

    if [ $1 = "Debug" ] || [ $1 = "debug" ] || [ $1 = "DEBUG" ]; then
        touch /tmp/logdbg;logread -f | grep " rae_check\[" &
    elif [ $1 = "Notice" ] || [ $1 = "notice" ] || [ $1 = "NOTICE" ]; then
        touch /tmp/logdbg;logread -f | grep -E "user.info rae_check\[|user.err rae_check\["&
    elif [ $1 = "Error" ] || [ $1 = "error" ] || [ $1 = "ERROR" ]; then
        touch /tmp/logdbg;logread -f | grep -E "user.err rae_check\["&
    fi
}


function get_param()
{
    local find=0

    for param in $PARAMS; do
        if [ $param != $1 ]; then
            continue
        fi

        find=1
        eval "get_param_"$param
    done

    if [ $find -eq 0 ]; then
        echo "raetest error: not support param [$1]!"
        exit 1
    fi
}

function init_report_time()
{
    echo "$1"
    if [ "random" == "$1" ]; then
        rm /rae/config/norandom
    else
        echo $1 > /rae/config/norandom
    fi
}

function set_param()
{
    local find=0
    local input_param=""
    local input_value=""

    if echo $1 | grep -q "="; then
        input_param=${1%%=*}
        input_value=${1##*=}
    else
        if [ $# -lt 2 ]; then
            echo "raetest error: missing value!"
            exit 1
        fi

        input_param=$1
        input_value=$2
    fi

    for param in $PARAMS; do
        if [ $input_param != $param ]; then
            continue
        fi

        find=1
        eval "set_param_"$input_param $input_value
    done

    if [ $find -eq 0 ]; then
        echo "raetest error: not support param [$input_param]!"
        exit 1
    fi
}

function commit()
{
    echo "commit"
    uci commit rae
}

function restart()
{
    /etc/init.d/rae restart
}

case $1 in
    get)
        shift
        get_param $@
        ;;
    set)
        shift
        set_param $@
        ;;
    commit)
        shift
        commit
        ;;
    init_report_time)
        shift
        init_report_time $@
        ;;
    restart)
        shift
        restart
        ;;
    *)
        usage
esac
