#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"		#ʧ
	LANG002="Success"	#ɹ
	LANG003="Set_Default_Group"		#Ĭϲ
	LANG004="Add_Policy_Plan"		#ӲԵȼƻ
	LANG005="Del_Policy_Plan"		#ɾԵȼƻ
	LANG006="Del_Policy_Plan"		#ɾȼƻ
	LANG007="Disable_Policy_Plan"	#òԵȼƻ
	LANG008="Enable_Policy_Plan"	#òԵȼƻ
fi


list_policy_sch()
{
    ${FLOWEYE} ${CGI_module} listtime | awk \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        col = 1;
        printf "%s{", dot;
        printf "\"id\":\"%s\",", $(col++);
        printf "\"active\":\"%s\",", $(col++);
        printf "\"disable\":\"%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 "\"polid\":\"%s\",", $(col++);
        printf "\"polname\":\"%s\",", $(col++);
        printf "\"ipmin\":\"%s\",", $(col++);
        printf "\"ipmax\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}


set_default_group()
{
    [ "${CGI_group}" = "" ] && retjson 1 "INV_GROUP"

    errmsg=`${FLOWEYE} ${CGI_module} setdefgrp name=${CGI_group}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        sync_floweye "${CGI_module} setdefgrp name=${CGI_group}"
        WEB_LOGGER "${LANG003:=Ĭϲ}" "module=${CGI_module} group=${CGI_group}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


add_policy_sch()
{
    status=1
    [ "${CGI_status}" = "on" ] && status=0
    [ "${CGI_acttype}" = "" ] && CGI_acttype="addtime"

    cmdargs="${CGI_module} ${CGI_acttype} id=${CGI_id} month=${CGI_month} startday=${CGI_startday} endday=${CGI_endday}"
    cmdargs="${cmdargs} start=${CGI_start_time} end=${CGI_end_time} group=${CGI_polid} disable=${status} ipmin=${CGI_ipmin} ipmax=${CGI_ipmax}"

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

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        sync_floweye "${cmdargs}"
        WEB_LOGGER "${LANG004:=ӲԵȼƻ}" "${cmdargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


rmv_policy_sch()
{
    errmsg=`${FLOWEYE} ${CGI_module} rmvtime id=${CGI_id}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        sync_floweye "${CGI_module} rmvtime id=${CGI_id}"
        WEB_LOGGER "${LANG005:=ɾԵȼƻ}" "module=${CGI_module} id=${CGI_id}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


btn_remove_sch()
{
    for id in `echo ${CGI_items} | tr ";" " "`
    do
        errmsg=`${FLOWEYE} ${CGI_module} rmvtime id=${id}`
        [ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}${errmsg}"
        sync_floweye "${CGI_module} rmvtime id=${id}"
    done

    WEB_LOGGER "${LANG006:=ɾȼƻ}" "module=${CGI_module} ids=${CGI_items}"
	retjson 0 "${LANG002:=ɹ}"
}


disable_policy_sch()
{
    cmdargs="${CGI_module} settime id=${CGI_id} month=${CGI_month} startday=${CGI_startday} endday=${CGI_endday} \
    start=${CGI_starthour}:${CGI_startmin}:${CGI_startsec} end=${CGI_endhour}:${CGI_endmin}:${CGI_endsec} \
    group=${CGI_polid} disable=1 ipmin=${CGI_ipmin} ipmax=${CGI_ipmax}"

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

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        sync_floweye "${cmdargs}"
        WEB_LOGGER "${LANG007:=òԵȼƻ}" "${cmdargs}"
		retjson 0 "${LANG002:=ɹ}"
    fi
}


enable_policy_sch()
{
    cmdargs="${CGI_module} settime id=${CGI_id} month=${CGI_month} startday=${CGI_startday} endday=${CGI_endday} \
    start=${CGI_starthour}:${CGI_startmin}:${CGI_startsec} end=${CGI_endhour}:${CGI_endmin}:${CGI_endsec} \
    group=${CGI_polid} disable=0 ipmin=${CGI_ipmin} ipmax=${CGI_ipmax}"

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

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        sync_floweye "${cmdargs}"
        WEB_LOGGER "${LANG008:=òԵȼƻ}" "${cmdargs}"
		retjson 0 "${LANG002:=ɹ}"
    fi
}


[ "${CGI_module}" = "" ] && retjson 1 "INV_MODULE"

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

    "set_default_group")
        action_check
        set_default_group
        ;;

    "add_policy_sch")
        action_check
        add_policy_sch
        ;;

    "rmv_policy_sch")
        action_check
        rmv_policy_sch
        ;;
    
    "btn_remove_sch")
        action_check
        btn_remove_sch
        ;;

    "disable_policy_sch")
        action_check
        disable_policy_sch
        ;;
    
    "enable_policy_sch")
        action_check
        enable_policy_sch
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
