#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"		#ʧ
	LANG002="Success"	#ɹ
	LANG003="Add_App_URL"	#Э
	LANG004="Del_App_URL"	#ɾЭ
	LANG005="Del_App_URL"	#ɾЭ
	LANG006="num"			#ɾ
	LANG007="Import_App_URL"	#Э
fi


list_user_host()
{
    [ "${CGI_page}" = "" ] && CGI_page=1
    [ "${CGI_limit}" = "" ] && CGI_limit=100
    [ "${CGI_appname}" = "any" ] && CGI_appname=""
    [ "${CGI_appname}" != "" ] && CGI_type=""

	end=$(( ${CGI_page} * ${CGI_limit} ))
	start=$(( ${end} - ${CGI_limit} ))

    ${FLOWEYE} axpdns list | grep -i "${CGI_keyword}" | awk \
    -v appname=${CGI_appname} -v type=${CGI_type} \
    -v start=${start} -v end=${end} \
    'BEGIN{
        row = 0;
        dot = "";
        printf "{\"data\":[";
    }{
        if($1 != "usr") next;
        if(appname != "" && $2 != appname) next;
        if(type != "" && $4 != type) next;

		row++;
		if( row > end  || row < start ) next;

        col = 1;
        printf "%s", dot;
        printf "{";
        printf "\"host_type\":\"%s\",", $(col++);
        printf "\"appname\":\"%s\",", $(col++);
        printf "\"appcname\":\"%s\",", $(col++);
        printf "\"apptype\":\"%s\",", $(col++);
        printf "\"host\":\"%s\",", $(col++);
        printf "\"port1\":\"%s\",", $(col++);
        printf "\"port2\":\"%s\",", $(col++);
        printf "\"trackdns\":\"%s\",", $(col++);
        printf "\"trackhost\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "],\"total\":%s}", row;
    }'
}


load_user_dns_node()
{
    ${FLOWEYE} axpdns get dns=${CGI_host} shownode=1 | awk \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        col = 1;
        printf "%s", dot;
        printf "{";
        printf "\"ip\":\"%s\",", $(col++);
        printf "\"port\":\"%s\",", $(col++);
        printf "\"ttl\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "]";  
    }'
}


add_user_dns()
{
    for item in ${CGI_items}
    do
        dns=`echo ${item} | cut -d":" -f1`
        port1=`echo ${item} | cut -d":" -f2`
        port2=`echo ${item} | cut -d":" -f3`
        trackdns=`echo ${item} | cut -d":" -f4`
        trackhost=`echo ${item} | cut -d":" -f5`

        cmdargs="axpdns add app=${CGI_appname} dns=${dns} port1=${port1} port2=${port2} trackdns=${trackdns} trackhost=${trackhost}"
        errmsg=`${FLOWEYE} ${cmdargs}`

        [ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}${errmsg}"
        sync_floweye "${cmdargs}"
    done

    WEB_LOGGER "${LANG003:=Э}" app=${CGI_appname}
	retjson 0 "${LANG002:=ɹ}"
}


rmv_user_dns()
{
    errmsg=`${FLOWEYE} axpdns remove dns="${CGI_host}"`

    if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        sync_floweye "axpdns remove dns=${CGI_host}"
        WEB_LOGGER "${LANG004:=ɾЭ}" "appname=${CGI_appcname} dns=${CGI_host}"
		retjson 0 "${LANG002:=ɹ}"
    fi
}


btnrmv_user_dns()
{
    num=0

    for url in `echo "${CGI_items}" | tr ";" " "`
    do
        errmsg=`${FLOWEYE} axpdns remove dns=${url}`
        [ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}${errmsg}"
        sync_floweye "axpdns remove dns=${url}"
        num=$((${num} + 1))
    done

    WEB_LOGGER "${LANG005:=ɾЭ}" "${LANG006:=ɾ}=${num},${CGI_items}" 
    retjson 0 "${LANG002:=ɹ}"
}


export_user_dns()
{
    [ "${CGI_appname}" = "" ] && retjson 1 "INV_APP"

	file_name="APPURL_${CGI_appname}.conf"
	file_path="${WEB_DOWNLOAD}/${file_name}"

	mkdir -p ${WEB_DOWNLOAD}

    ${FLOWEYE} axpdns list | awk \
    -v appname="${CGI_appname}" \
    '{
        if($2==appname)
            printf "%s %s %s\n", $5, $6, $7;
    }' > ${file_path}

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


import_user_dns()
{
    [ ! -f "${CGI_file}" ] && retjson 1 "INV_FILE"

    ttfile="${TMPDIR}/`date +%s`.$$"
    cat ${CGI_file} | tr -ds '\r' '' > ${ttfile}

    [ "${CGI_trackdns}"  = "on" ] && trackdns=1  || trackdns=0
    [ "${CGI_trackhost}" = "on" ] && trackhost=1 || trackhost=0

    while read dns port1 port2
    do
        [ "${port1}" = "" ] && port1=80
        [ "${port2}" = "" ] && port2=443

        errmsg=`${FLOWEYE} axpdns add app=${CGI_appname} dns=${dns} port1=${port1} port2=${port2} trackdns=${trackdns} trackhost=${trackhost}`
        [ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}${errmsg}"
    done <<EOF
`cat ${ttfile}`
EOF

    rm -rf "${CGI_file}"
    rm -rf "${ttfile}"
    
    WEB_LOGGER "${LANG007:=Э}" app=${CGI_appname}
    retjson 0 "${LANG002:=ɹ}"
}


case "${CGI_action}" in
    "list_user_host")
        retjson 0 "OK" "`list_user_host`"
        ;;
    
    "load_user_dns_node")
        retjson 0 "OK" "`load_user_dns_node`"
        ;;
    
    "add_user_dns")
        action_check
        add_user_dns
        ;;
    
    "rmv_user_dns")
        action_check
        rmv_user_dns
        ;;

    "btnrmv_user_dns")
        action_check
        btnrmv_user_dns
        ;;

    "export_user_dns")
        action_check
        export_user_dns
        ;;
    
    "import_user_dns")
        action_check
        import_user_dns
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
