#!/bin/sh

. ../common/ajax_common


get_group_detail()
{
    ${FLOWEYE} rtgroup get id=${CGI_id} | awk -F "=" \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        printf "%s", dot;
        printf "\"%s\":\"%s\"", $1, $2;
        if (dot == "") dot = ",";
    }END{
        printf "}";
    }'
}


get_group_schtime()
{
    ${FLOWEYE} rtgroup get id=${CGI_id} time=1 | awk \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        col = 1;

        printf "%s{", dot;
        printf "\"month\":\"%s\"", $(col++);
        printf ",\"start_day\":\"%s\"", $(col++);
        printf ",\"end_day\":\"%s\"", $(col++);
        printf ",\"start_time\":\"%s\"", $(col++);
        printf ",\"end_time\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot =",";
    }END{
        printf "]";
    }'
}


rmv_group_schtime()
{
    cmdargs="${CGI_month},${CGI_start_day}-${CGI_end_day},${CGI_start_time}-${CGI_end_time}"
    errmsg=`${FLOWEYE} rtgroup config group=${CGI_name} rmvtime=${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "rtgroup config group=${CGI_name} rmvtime=${cmdargs}"
        WEB_LOGGER "ɾʱ" "group=${CGI_name} rmvtime=${cmdargs}"
        retjson 0 "ɹ"
    fi
}


add_group_schtime()
{
    cmdargs="${CGI_month},${CGI_start_day}-${CGI_end_day},${CGI_time}"
    errmsg=`${FLOWEYE} rtgroup config group=${CGI_name} time=${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "rtgroup config group=${CGI_name} time=${cmdargs}"
        WEB_LOGGER "ʱ" "group=${CGI_name} time=${cmdargs}"
        retjson 0 "ɹ"
    fi
}


load_addgroup_env()
{
    . ../common/ajax_object_lib

    printf "{"
    printf "\"interface_list\":`lib_interface_list`"
    printf ",\"proxy_list\":`lib_proxy_list`"

    if [ "${CGI_id}" != "" ]; then
        printf ",\"policy\":`get_group_detail`"
        printf ",\"schtime\":`get_group_schtime`"
    fi

    printf "}"
}


add_group()
{
    [ "${CGI_inip}" = "" ] && CGI_inip="any"
    [ "${CGI_outip}" = "" ] && CGI_outip="any"
    [ "${CGI_ifname}" = "" ] && CGI_ifname="NULL"

    cmdargs="name=${CGI_name} src=${CGI_submit_srcip} dst=${CGI_submit_dstip} srcif=${CGI_srcif} vlan=${CGI_vlan}"
    cmdargs="${cmdargs} month=${CGI_month} startday=${CGI_startday} endday=${CGI_endday}"
    cmdargs="${cmdargs} start=${CGI_start_time} end=${CGI_end_time} disable=${CGI_disable}"

    if [ "${CGI_action}" = "copy_group" ]; then
        cmdargs="clonerules=${CGI_clonerules} ${cmdargs}"
    fi

    if [ "${CGI_action}" = "edit_group" ]; then
        cmdargs="set id=${CGI_id} newname=${CGI_name} ${cmdargs}"
    else
        cmdargs="add ${cmdargs}"
    fi

    errmsg=`${FLOWEYE} rtgroup ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "rtgroup ${cmdargs}"
        WEB_LOGGER "ز"
        retjson 0 "ɹ"
    fi
}


remove_group()
{
    errmsg=`${FLOWEYE} rtgroup remove id=${CGI_id}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "rtgroup remove id=${CGI_id}"
        WEB_LOGGER "ɾز" "id=${CGI_id}"
        retjson 0 "ɹ"
    fi
}


move_group()
{
    cmdargs="id=${CGI_moveup} moveto=${CGI_movedown}"

    errmsg=`${FLOWEYE} rtgroup set ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}"
    else
        sync_floweye "rtgroup set ${cmdargs}"
        WEB_LOGGER "ز˳" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


export_group()
{
    group_name=`${FLOWEYE} rtgroup get id=${CGI_id} | grep "^name=" | cut -d"=" -f2`

    file_name="flowct_${group_name}.conf"
    file_path="${WEB_DOWNLOAD}/${file_name}"

    mkdir -p ${WEB_DOWNLOAD}

    grep "newrule group=${group_name} " "${PGETC}/panabit.conf" | cut -d" " -f3- > ${file_path}

    retjson 0 "OK" "{\"file_name\":\"${file_name}\"}"
}


import_group()
{
    # ɾԭ
    for pid in `${FLOWEYE} rtgroup list group=${CGI_group}`
    do
        ${FLOWEYE} rtgroup remove group=${CGI_group} id=${pid}
    done

    cat ${CGI_file} | while read cfg
    do
        [ "${cfg}" = "" ] && continue
        errmsg=`${FLOWEYE} rtgroup add group=${CGI_group} ${cfg}`
        [ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
    done

    WEB_LOGGER "${LANG017:=Ʋ}"
    retjson 0 "ɹ"
}


set_enable_group()
{
    cmdargs="id=${CGI_id} disable=${CGI_disable}"
    errmsg=`${FLOWEYE} rtgroup set ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}"
    else
        sync_floweye "rtgroup set ${cmdargs}"
        WEB_LOGGER "ز" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


set_stop_group()
{
    cmdargs="id=${CGI_id} stop=${CGI_stop}"
    errmsg=`${FLOWEYE} rtgroup set ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}"
    else
        sync_floweye "rtgroup set ${cmdargs}"
        WEB_LOGGER "ز" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


list_group()
{
	defult_schtime=`${FLOWEYE} route stat | grep "defult_schtime" | cut -d"=" -f2`

    ${FLOWEYE} rtgroup list | awk \
    -v id=${CGI_id} -v active=${CGI_active} \
    -v defult_schtime="${defult_schtime}" \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        if(defult_schtime != 0 && $1 == 0) next;
        if(id != "" && $1 != id) next;
        if(active != "" && $4 != active) next;

        col = 1;

        printf "%s", dot;
        printf "{";
        printf "\"id\":\"%s\",", $(col++);
        printf "\"name\":\"%s\",", $(col++);
        printf "\"rulecnt\":\"%s\",", $(col++);
        printf "\"active\":\"%s\",", $(col++);
        printf "\"disable\":\"%s\",", $(col++);
        printf "\"srcif\":\"%s\",", $(col++);
        printf "\"vlan\":\"%s\",", $(col++);
        printf "\"src\":\"%s\",", $(col++);
        printf "\"dst\":\"%s\",", $(col++);
        printf "\"month\":\"%s\",", $(col++);
        printf "\"startday\":\"%s\",", $(col++);
        printf "\"endday\":\"%s\",", $(col++);
        printf "\"starthour\":\"%s\",", $(col++);
        printf "\"startmin\":\"%s\",", $(col++);
        printf "\"startsec\":\"%s\",", $(col++);
        printf "\"endhour\":\"%s\",", $(col++);
        printf "\"endmin\":\"%s\",", $(col++);
        printf "\"endsec\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}


search_group()
{
    if [ ! -f ${PGETC}/panabit.conf ]; then
        printf "{}"
        return
    fi
    
    awk -v keyword="${CGI_keyword}" -v disable="${CGI_disable}" '{
        if($1 == "rtgroup") {
            if(disable != "" && $0 !~ " disable="disable )
                next;
                
            if(keyword != "" && $0 !~ keyword)
                next;

            split($2, name, "=");
            group[name[2]] = 0;
        } else if($1 == "newrule") {
            if(disable != "" && $0 !~ " disable="disable )
                next;

            if(keyword != ""){
                find = 0;

                for(i = 3; i <= NF; i++){
                    if($i ~ keyword)
                    find = 1;
                }

                if(find == 0) next;
            }

            split($2, name, "=");
            group[name[2]]++;
        }
    }END{
        dot = "";
        printf "{";

        for(key in group){
            printf "%s\"%s\":\"%s\"", dot, key, group[key];
            if(dot == "") dot = ",";
        }

        printf "}";
    }' ${PGETC}/panabit.conf
}


load_group_list()
{
    printf "{";
    printf "\"all_group\":`list_group`"

    if [ "${CGI_keyword}" != "" -o "${CGI_disable}" != "" ]; then
        printf ",\"need_show\":`search_group`"
    fi

    printf "}";
}


case "${CGI_action}" in
    "get_group_schtime")
        retjson 0 "OK" "`get_group_schtime`"
        ;;

    "rmv_group_schtime")
        action_check
        rmv_group_schtime
        ;;

    "add_group_schtime")
        action_check
        add_group_schtime
        ;;

    "load_addgroup_env")
        retjson 0 "OK" "`load_addgroup_env`"
        ;;

    "add_group"|"edit_group"|"copy_group")
        action_check
        add_group
        ;;

    "remove_group")
        action_check
        remove_group
        ;;
        
    "list_group")
        retjson 0 "OK" "`list_group`"
        ;;

    "load_group_list")
        retjson 0 "OK" "`load_group_list`"
        ;;

    "move_group")
        action_check
        move_group
        ;;

    "export_group")
        action_check
        export_group
        ;;
    
    "import_group")
        action_check
        import_group
        ;;

    "set_enable_group")
        action_check
        set_enable_group
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
