#!/bin/sh

. ../common/ajax_common


load_ipdesc_list()
{
	[ "${CGI_page}" = "" ] && CGI_page=1
	[ "${CGI_limit}" = "" ] && CGI_limit=100
	end=$(( ${CGI_page} * ${CGI_limit} ))
	start=$(( ${end} - ${CGI_limit} ))

	${FLOWEYE}  ipdb list type=custom | grep "${CGI_keyword}" | awk -F"," \
	-v start=${start} -v end=${end} \
    'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
    }{
		row++;
		if( row > end  || row < start ) next;

		printf "%s", dot;
		printf "{";
        printf "\"ip\":\"%s\",", $1;
        printf "\"desc\":\"%s\"", $2;
		printf "}";
        if(dot == "") dot = ",";  
    }END{
		printf "],\"total\":\"%s\"}", row;
    }'
}


add_ip_note()
{
	cmdargs="ip=${CGI_ip},${CGI_desc}"
	errmsg=`${FLOWEYE} ipdb add ${cmdargs}`

	if [ "$?" != "0" ]; then 
		retjson 1 ${errmsg}
	else
		sync_floweye "ipdb add ${cmdargs}"
		WEB_LOGGER "IPַ" "${cmdargs}"
		retjson 0 "ɹ"
	fi
}


remove_ip_note()
{
	for ip in `echo ${CGI_ip} | tr ";" " "`
    do
        errmsg=`${FLOWEYE} ipdb remove ip=${ip}`
        [ $? -ne 0 ] && retjson 1 "ʧ:${errmsg}"
		sync_floweye "ipdb remove ip=${ip}"
    done

	WEB_LOGGER "ɾIPַ"
    retjson 0 "ɹ"
}


export_ipnote()
{
	file_name="ipdesc.conf"
	file_path="${WEB_DOWNLOAD}/${file_name}"
	
	mkdir -p ${WEB_DOWNLOAD}

	${FLOWEYE} ipdb list type=custom  > ${file_path}

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


edit_ip_note()
{
	errmsg=`${FLOWEYE} ipdb remove ip=${CGI_ip}`
	[ $? -ne 0 ] && retjson 1 "${errmsg}"
	sync_floweye "ipdb remove ip=${CGI_ip}"

	cmdargs="ip=${CGI_ip},${CGI_desc}"
	errmsg1=`${FLOWEYE} ipdb add ${cmdargs}`
	[ $? -ne 0 ] && retjson 1 "${errmsg1}"
	
	sync_floweye "ipdb add ${cmdargs}"
	WEB_LOGGER "༭IPַ"
	retjson 0 "ɹ"
}


import_ipdesc()
{
	for line in `cat ${CGI_file} | tr -ds '\r' ''`
    do
        errmsg=`${FLOWEYE} ipdb add ip=${line}`
        if [ "$?" != "0" ]; then 
			[ "${errmsg}" = "OVERLAP" ] && continue
			retjson 1 "${errmsg}"

		fi
    done

	WEB_LOGGER "ԶIPַ"
    retjson 0 "ɹ"
}


get_version()
{
	curver=`${FLOWEYE} ipdb stat | grep "^version=" | cut -d'=' -f2`
	retjson 0 "OK" "{\"curver\":\"${curver}\"}"
}


import_system()
{
	errmsg=`${FLOWEYE} ipdb load file=${CGI_file}`

	if [ $? -ne 0 ]; then
		rm -rf ${CGI_file}
		retjson 1 "ʧ:${errmsg}"
	else
		mv ${CGI_file} ${PGETC}/ip.dat
		sync
		WEB_LOGGER "IPλÿ"
		retjson 0 "ɹ"
	fi
}


search_location()
{
	for tagval in `${FLOWEYE} ipdb get ip=${CGI_ip}`
	do
		eval "${tagval}"
	done

	if [ "${isp}" = "" ]; then
		result="${location}"
	else
		result="${location}|${isp}"
	fi

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


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

	"add_ip_note")
		add_ip_note
		;;

	"remove_ip_note")
		action_check
		remove_ip_note
		;;

	"edit_ip_note")
		action_check
		edit_ip_note
		;;
		
	"export_ipnote")
		action_check
		export_ipnote
		;;

	"import_ipdesc")
		action_check
		import_ipdesc
		;;

	"get_version")
		action_check
		get_version
		;;

	"import_system")
		action_check
		import_system
		;;

	"search_location")
		action_check
		search_location
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
