#!/bin/sh

. ../common/ajax_common


if [ "${PALANG}" = "en" ]; then
	LANG_001="Set Error"			#ʧ
	LANG_002="Set_DHCP_Server"		#DHCP
	LANG_003="Set Success"			#óɹ
	LANG_004="Enable DHCP Server"	#·DHCP
	LANG_005="Success"				#ɹ
	LANG_006="Disable_DHCP_Server"	#·DHCP
	LANG_007="Enable_DHCP_Server"	#DHCP
	LANG_008="Disable_DHCP_Server"	#DHCP
	LANG_009="Set_DHCP_Server"		#DHCP
	LANG_010="Error"				#ʧ
	LANG_011="Add_DHCP_Bind"		#DHCP̬
	LANG_012="Edit_DHCP_Bind"		#༭DHCP̬
	LANG_013="Delete_DHCP_Bind"		#ɾDHCP̬
	LANG_014="Delete_Num"			#ɾ
	LANG_015="Delete_DHCP_Bind"		#ɾDHCP̬
	LANG_016="Import_DHCP_Bind"		#DHCP̬
	LANG_017="-"
	LANG_018="Import_Success"		#ɹ
	LANG_019="Import_Error"			#ʧ
	LANG_020="Import_Done."			#
	LANG_021="Force_DHCP_user_offline"	#ǿDHCPû
	LANG_022="Add DHCP address" #DHCPַ
	LANG_023="Delete DHCP address" #ɾDHCPַ
fi


set_dhcpsvr()
{
	enable=0
	[ "${CGI_dhcp_enable}" = "on" ] && enable=1
	
	cmdargs="name=${CGI_name} id=${CGI_id} dhcp_pool=${CGI_dhcp_pool}" 
	cmdargs="${cmdargs} dns0=${CGI_dns0} dns1=${CGI_dns1}"
	cmdargs="${cmdargs} leasettl=${CGI_leasettl} dhcp_enable=${enable}"

	if [ "${CGI_dhcp_gateway}" != "" -a "${CGI_dhcp_gateway}" != "0.0.0.0" ]; then
		cmdargs="${cmdargs} dhcp_gateway=${CGI_dhcp_gateway}"
	fi

	if [ "${CGI_dhcp_vlan}" != "" ]; then
		cmdargs="${cmdargs} dhcp_vlan=${CGI_dhcp_vlan}"
	else
		cmdargs="${cmdargs} dhcp_vlan=0"
	fi

	dhcpmask="${CGI_dhcp_mask}"
	[ "${dhcpmask}" = "" ] && dhcpmask="0.0.0.0"
	cmdargs="${cmdargs} dhcp_mask=${dhcpmask}"

	[ "${CGI_dhcp_acaddr}" = "" ] && CGI_dhcp_acaddr="0.0.0.0"
	cmdargs="${cmdargs} dhcp_acaddr=${CGI_dhcp_acaddr} dhcp_domain=${CGI_dhcp_domain}"

	for opt in `${FLOWEYE} nat getproxy ${CGI_name} | grep dhcp_option | cut -d"=" -f2 | cut -d"," -f1`
	do
		${FLOWEYE} nat setrtif name=${CGI_name} dhcp_option=${opt},NULL
		sync_floweye "nat setrtif name=${CGI_name} dhcp_option=${opt},NULL"
	done

	for opt in `echo ${CGI_dhcpsvr_option_list} | tr ";" " "`
	do
		cmdargs="${cmdargs} dhcp_option=${opt}"
	done
	
	errmsg=`${FLOWEYE} nat setrtif ${cmdargs}`

	if [ "$?" != "0" ]; then
		retjson 1 "${LANG_001:=ʧ}${errmsg}"
	else
		sync_floweye "nat setrtif ${cmdargs}"
		WEB_LOGGER "${LANG_002:=DHCP}" "${cmdargs}"
		retjson 0 "${LANG_003:=óɹ}"
	fi
}


list_dhcpsvr()
{
	${FLOWEYE} dhcpsvr list | grep "${CGI_keyword}" | awk -v state="${CGI_state}" \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		if( state != "" && state != $5 ) next;

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"id\":\"%s\",", $(col++);
		printf "\"num\":\"%s\",", NR;
		printf "\"name\":\"%s\",", $(col++);
		printf "\"ttl\":\"%s\",", $(col++);
		printf "\"vlan\":\"%s\",", $(col++);
		printf "\"enable\":\"%s\",", $(col++);
		printf "\"pool\":\"%s\",", $(col++);
		printf "\"dns0\":\"%s\",", $(col++);
		printf "\"dns1\":\"%s\",", $(col++);
		printf "\"gateway\":\"%s\",", $(col++);
		printf "\"clntcnt\":\"%s\"", $(col++);
		printf "}";
		if( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


enable_dhcpsvr()
{
	errmsg=`${FLOWEYE} nat setrtif name=${CGI_name} dhcp_enable=1`
	
	if [ $? -ne 0 ]; then
		retjson 1 "${errmsg}"
	else
		sync_floweye "nat setrtif name=${CGI_name} dhcp_enable=1"
		WEB_LOGGER "${LANG_004:=·DHCP}" "name=${CGI_name}"
		retjson 0 "${LANG_005:=ɹ}"
	fi
}


disable_dhcpsvr()
{
	errmsg=`${FLOWEYE} nat setrtif name=${CGI_name} dhcp_enable=0`
	
	if [ $? -ne 0 ]; then
		retjson 1 "${errmsg}"
	else
		sync_floweye "nat setrtif name=${CGI_name} dhcp_enable=0"
		WEB_LOGGER "${LANG_006:=·DHCP}" "name=${CGI_name}"
		retjson 0 "${LANG_005:=ɹ}"
	fi
}


btn_enable_dhcpsvr()
{
	num=0

	for name in ${CGI_names}
	do
		${FLOWEYE} nat setrtif name=${name} dhcp_enable=1
		sync_floweye "nat setrtif name=${name} dhcp_enable=1"
		num=$((${num}+1))
	done

	WEB_LOGGER "${LANG_007:=DHCP}" "num=${num}"
	retjson 0 "${LANG_005:=ɹ}"
}


btn_disable_dhcpsvr()
{
	num=0

	for name in ${CGI_names}
	do
		${FLOWEYE} nat setrtif name=${name} dhcp_enable=0
		sync_floweye "nat setrtif name=${name} dhcp_enable=0"
		num=$((${num}+1))
	done

	WEB_LOGGER "${LANG_008:=DHCP}" "num=${num} ${CGI_names}"
	retjson 0 "${LANG_005:=ɹ}"
}


list_dhcpsta()
{
	${FLOWEYE} dhcpsta list | grep -i "${CGI_keyword}" | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		desc = $5 == "NULL" ? "" : $5;
		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"mac\":\"%s\",", $(col++);
		printf "\"vlan\":\"%s\",", $(col++);
		printf "\"ip\":\"%s\",", $(col++);
		printf "\"ipn\":\"%s\",", $(col++);
		printf "\"desc\":\"%s\"", desc;
		printf "}";
		if( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


add_dhcpsta()
{
	[ "${CGI_vlan}" = "" ] && CGI_vlan="0-4095"
	[ "${CGI_desc}" != "" -a "${PALANG}" = "en" ] && CGI_desc=`echo ${CGI_desc} | sed -r 's/ /\%20/g'`

	errmsg=`${FLOWEYE} dhcpsta add mac=${CGI_mac} ip=${CGI_ip} vlan=${CGI_vlan} desc=${CGI_desc}`

	if [ "$?" != "0" ]; then
		retjson 1 "${LANG_010:=ʧ}${errmsg}"
	else
		sync_floweye "dhcpsta add mac=${CGI_mac} ip=${CGI_ip} vlan=${CGI_vlan} desc=${CGI_desc}"
		WEB_LOGGER "${LANG_011:=DHCP̬}" "mac=${CGI_mac} ip=${CGI_ip} vlan=${CGI_vlan} desc=${CGI_desc}"
		retjson 0 "${LANG_005:=ɹ}"
	fi
}


edit_dhcpsta()
{
	[ "${CGI_vlan}" = "" ] && CGI_vlan="0-4095"
	[ "${CGI_desc}" != "" -a "${PALANG}" = "en" ] && CGI_desc=`echo ${CGI_desc} | sed -r 's/ /\%20/g'`

	errmsg=`${FLOWEYE} dhcpsta set mac=${CGI_mac} ip=${CGI_ip} vlan=${CGI_vlan} desc=${CGI_desc}`

	if [ "$?" != "0" ]; then
		retjson 1 "${LANG_010:=ʧ}${errmsg}"
	else
		sync_floweye "dhcpsta set mac=${CGI_mac} ip=${CGI_ip} vlan=${CGI_vlan} desc=${CGI_desc}"
		WEB_LOGGER "${LANG_012:=༭DHCP̬}" "mac=${CGI_mac} ip=${CGI_ip} vlan=${CGI_vlan} desc=${CGI_desc}"
		retjson 0 "${LANG_005:=ɹ}"
	fi
}


delete_dhcpsta()
{
	errmsg=`${FLOWEYE} dhcpsta remove ${CGI_macs}`
	
	if [ "$?" != "0" ]; then
		retjson 1 "${LANG_010:=ʧ}${errmsg}"
	else
		sync_floweye "dhcpsta remove ${CGI_macs}"
		if [ "${CGI_num}" != "" ]; then
			WEB_LOGGER "${LANG_013:=ɾDHCP̬}" "${LANG_014:=ɾ}=${CGI_num}"
		else
			WEB_LOGGER "${LANG_015:=ɾDHCP̬}" "mac=${CGI_macs}"
		fi
		retjson 0 "${LANG_005:=ɹ}"
	fi
}


export_dhcpsta()
{
	file_name="dhcpsta.conf"
	file_path="${WEB_DOWNLOAD}/${file_name}"
	
	mkdir -p ${WEB_DOWNLOAD}
	
	${FLOWEYE} dhcpsta list | awk -v file=${file_path} \
	'{
		printf "%s %s %s  %s\n", $1, $2, $3, $5 > file;
	}'

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


import_dhcpsta()
{
	err=0
	ok=0

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

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

	while read mac vlan ip desc
	do
		cmdargs="mac=${mac} vlan=${vlan} ip=${ip} desc=${desc}"
		errmsg=`${FLOWEYE} dhcpsta add ${cmdargs}`

		if [ $? -ne 0 ]; then
			err=$((${err}+1))
		else
			ok=$((${ok}+1))
		fi
	done << EOF
`cat "${ttfile}"`
EOF

    rm -rf "${CGI_file}"
    rm -rf "${ttfile}"

	WEB_DOWNLOAD "${LANG_016:=DHCP̬}" "${LANG_018:=ɹ}${ok}${LANG_019:=ʧ}${err}"
	retjson 0 "${LANG_020:=}${LANG_018:=ɹ}${ok}${LANG_019:=ʧ}${err}"
}


list_dhcpuser()
{
	[ "${CGI_page}" = "" ] && CGI_page=1
	[ "${CGI_limit}" = "" ] && CGI_limit=100
	[ "${CGI_id}" != "" ] && cmdargs="id=${CGI_id}"

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

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

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"lanif\":\"%s\",", $(col++);
		printf "\"mac\":\"%s\",", $(col++);
		printf "\"ip\":\"%s\",", $(col++);
		printf "\"uip\":\"%s\",", $(col++);
		printf "\"desc\":\"%s\",", $(col++);
		printf "\"vlan\":\"%s\",", $(col++);
		printf "\"state\":\"%s\",", $(col++);
		printf "\"type\":\"%s\",", $(col++);
		printf "\"ltime\":\"%s\",", $(col++);
		printf "\"ttl\":\"%s\"", $(col++);
		printf "}";
		if( dot == "" ) dot = ",";
	}END{
        printf "],\"total\":%s}", row;
	}'
}


rmv_dhcpuser()
{
	errmsg=`${FLOWEYE} dhcplease remove mac=${CGI_mac}`

	if [ "$?" != "0" ]; then
		retjson 1 "${LANG_010:=ʧ}${errmsg}"
	else
		WEB_LOGGER "${LANG_023:=ɾDHCPַ}" "id=${CGI_id}"
		retjson 0 "${LANG_005:=ɹ}"
	fi
}


get_dhcpsvr_stat()
{
	${FLOWEYE} dhcpsvr stat | awk -F "=" \
	'BEGIN{
		dot = "";
		printf "{";
	}{
		printf "%s\"%s\":\"%s\"", dot, $1, $2;
		if (dot == "") dot = ",";
	}END{
		printf "}";
	}'
}


get_dhcpreplay_config()
{
	${FLOWEYE} dhcprelay stat | awk -F "=" \
	'BEGIN{
		dot = "";
		printf "{";
	}{
		printf "%s\"%s\":\"%s\"", dot, $1, $2;
		if (dot == "") dot = ",";
	}END{
		printf "}";
	}'
}


load_dhcpsvr_config()
{
	. ../common/ajax_object_lib

	printf "{"
	printf "\"lanif\":`lib_proxy_list rtif`"
	printf ",\"lanif6\":`lib_proxy_list rtif6`"
	printf ",\"iptable\":`lib_iptable_list`"
	printf ",\"dhcprelay\":`get_dhcpreplay_config`"
	printf ",\"dhcpsvr\":`get_dhcpsvr_stat`"
	printf "}"
}


dhcp_server_list()
{
	printf "{"
	printf "\"server\":`list_dhcpsvr`"
	printf ",\"replay\":`get_dhcpreplay_config`"
	printf "}"
}


set_dhcpsvr_config()
{
	cmdargs="ipmac_enable=${CGI_ipmac_enable} checkrelayip=${CGI_checkrelayip} bypassipgrp=${CGI_bypassipgrp}"
	errmsg=`${FLOWEYE} dhcpsvr config ${cmdargs}`
	[ $? -ne 0 ] && retjson 1 "DHCPʧ:${errmsg}"

	sync_floweye "dhcpsvr config ipmac_enable=${CGI_ipmac_enable}"
	WEB_LOGGER "${LANG_009:=DHCP}" "${cmdargs}"

	cmdargs="enable=${CGI_enable} server=${CGI_server} ifname=${CGI_ifname} vlan=${CGI_vlan}"
	cmdargs="${cmdargs} ifname6=${CGI_ifname6} server6=${CGI_server6}"
	errmsg=`${FLOWEYE} dhcprelay config ${cmdargs}`
	[ $? - ne 0 ] && retjson 1 "DHCPмʧ:${errmsg}"
	
	sync_floweye "dhcprelay config ${cmdargs}"
	WEB_LOGGER "DHCPм" "${cmdargs}"
	retjson 0 "ɹ"
}


dhcpv6_server_list()
{
	${FLOWEYE}  dhcpsvr6 list type=pool | grep "${CGI_keyword}" | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"id\":\"%s\",", $(col++);
		printf "\"start\":\"%s\",", $(col++);
		printf "\"end\":\"%s\",", $(col++);
		printf "\"desc\":\"%s\"", $(col++);
		printf "}";
		if( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


add_dhcpsvrv6()
{

	[ "${CGI_desc}" != "" -a "${PALANG}" = "en" ] && CGI_desc=`echo ${CGI_desc} | sed -r 's/ /\%20/g'`
	if [ "${CGI_action}" = "edit_dhcpsvrv6" ]; then
        cmdargs="set newid=${CGI_newid}"
    else
        cmdargs="add"
    fi

	errmsg=`${FLOWEYE} dhcpsvr6 ${cmdargs} id=${CGI_id} start=${CGI_start} end=${CGI_end} desc=${CGI_desc}`

	if [ "$?" != "0" ]; then
		retjson 1 "${LANG_010:=ʧ}${errmsg}"
	else
		sync_floweye "dhcpsvr6 ${cmdargs}"
		WEB_LOGGER "${LANG_022:=Add DHCP address}" "${cmdargs}"
		retjson 0 "${LANG_005:=ɹ}"
	fi
}


rmv_dhcpsvrv6()
{
	errmsg=`${FLOWEYE} dhcpsvr6 remove id=${CGI_id}`

	if [ "$?" != "0" ]; then
		retjson 1 "${LANG_010:=ʧ}${errmsg}"
	else
		sync_floweye "dhcpsvr6 remove id=${CGI_id}"
		WEB_LOGGER "ɾDHCPv6" "id=${CGI_id}"
		retjson 0 "${LANG_005:=ɹ}"
	fi
}


load_dhcplog_list()
{
	case "${CGI_sort_key}" in
		"time")
			sort_key="-nk2"
			;;

		"mac")
			sort_key="-k5"
			;;
		*)
			sort_key="-nk2"
			;;
	esac

	[ "${CGI_sort_type}" = "desc" ] && sort_type="-r"

	[ "${CGI_page}" = "" ] && CGI_page=1
	[ "${CGI_limit}" = "" ] && CGI_limit=100

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

	${FLOWEYE} dhcpsvr list log=1 | grep "${CGI_keyword}" | sort ${sort_type} ${sort_key} | awk \
	-v start=${start} -v end=${end} \
	-v type=${CGI_type} \
	'BEGIN{
		row = 0;
		dot = "";
        printf "{\"data\":[";
	}{
		if(type != "" && $3 != type)
			next;

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

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"id\":\"%s\",", $(col++);
		printf "\"time\":\"%s\",", $(col++);
		printf "\"type\":\"%s\",", $(col++);
		printf "\"ip\":\"%s\",", $(col++);
		printf "\"mac\":\"%s\",", $(col++);
		printf "\"lan\":\"%s\",", $(col++);
		printf "}";
		if( dot == "" ) dot = ",";
	}END{
        printf "],\"total\":%s}", row;
	}'
}


set_dhcpsvr_logenable()
{
	[ "${CGI_dhcplog_enable}" = "on" ] && log=1 || log=0
	errmsg=`${FLOWEYE} dhcpsvr config log_enable=${log}`

	if [ $? -ne 0 ]; then
		retjson 1 "ʧ"
	else
		sync_floweye "dhcpsvr config log_enable=${log}"
		WEB_LOGGER "DHCP־" "log_enable=${log}"
		retjson 0 "ɹ"
	fi
}


export_dhcplog()
{
    file_name="DHCPSVR_RELEASE.log"
    file_path="${WEB_DOWNLOAD}/${file_name}"

    mkdir -p ${WEB_DOWNLOAD}

    ${FLOWEYE} dhcpsvr list log=1 > ${file_path}

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


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

	"set_dhcpsvr_config")
		action_check
		set_dhcpsvr_config
		;;

	"dhcp_server_list")
		retjson 0 "OK" "`dhcp_server_list`"
		;;

	"set_dhcpsvr")
		action_check
		set_dhcpsvr
		;;
	
	"list_dhcpsvr")
		retjson 0 "OK" "`list_dhcpsvr`"
		;;
		
	"enable_dhcpsvr")
		action_check
		enable_dhcpsvr
		;;

	"disable_dhcpsvr")
		action_check
		disable_dhcpsvr
		;;
	
	"btn_enable_dhcpsvr")
		action_check
		btn_enable_dhcpsvr
		;;

	"btn_disable_dhcpsvr")
		action_check
		btn_disable_dhcpsvr
		;;

	"list_dhcpsta")
		retjson 0 "OK" "`list_dhcpsta`"
		;;
	
	"add_dhcpsta")
		action_check
		add_dhcpsta
		;;

	"edit_dhcpsta")
		action_check
		edit_dhcpsta
		;;

	"delete_dhcpsta")
		action_check
		delete_dhcpsta
		;;

	"export_dhcpsta")
		action_check
		export_dhcpsta
		;;

	"import_dhcpsta")
		action_check
		import_dhcpsta
		;;

	"list_dhcpuser")
		retjson 0 "OK" "`list_dhcpuser`"
		;;

	"rmv_dhcpuser")
		action_check
		rmv_dhcpuser
		;;

	"dhcpv6_server_list")
		retjson 0 "OK" "`dhcpv6_server_list`"
		;;

	"add_dhcpsvrv6")
		action_check
		add_dhcpsvrv6
		;;
	
	"edit_dhcpsvrv6")
		action_check
		add_dhcpsvrv6
		;;
	
	"rmv_dhcpsvrv6")
		action_check
		rmv_dhcpsvrv6
		;;

	"load_dhcplog_list")
		retjson 0 "OK" "`load_dhcplog_list`"
		;;
		
	"get_dhcpsvr_stat")
		retjson 0 "OK" "`get_dhcpsvr_stat`"
		;;

	"set_dhcpsvr_logenable")
		action_check
		set_dhcpsvr_logenable
		;;

	"export_dhcplog")
		action_check
		export_dhcplog
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
