#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"		#ʧ
	LANG002="Success"	#ɹ
	LANG003="Set_PPPOE_Accunt"		#û˺
	LANG004="Unbind_Account_MAC"	#PPPOE˺MAC
	LANG005="num"	#
	LANG006="Del_PPPOE_Account"		#ɾPPPOE˺
	LANG007="Enable_PPPOE_Account"	#PPPOE˺
	LANG008="Disable_PPPOE_Account"	#PPPOE˺
	LANG009="Set_PPPOE_Account"		#޸˺
	LANG010="Export_PPPOE_Account"	#PPPOE˺
	LANG011="Proxy_Account"			#˺
	LANG012="PPPOE_Account"			#˺
fi


list_pppoe_account()
{
	case "${CGI_sort_key}" in
		"pool_name")
			sort_key="-k1"
			;;

		"account")
			sort_key="-k3"
			;;

		"online_cnt")
			sort_key="-nk14"
			;;

		"start")
			sort_key="-k7"
			;;

		"end")
			sort_key="-nk9"
			;;

		"offtime")
			sort_key="-k10"
			;;

		*)
			sort_key="-nk9"
			;;
	esac

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

	[ "${CGI_acct_type}" = "" ] && CGI_acct_type="local"

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

	${FLOWEYE} pppoeacct list poolid=${CGI_pool_id} | sort ${sort_type} ${sort_key} | grep -i "${CGI_keyword}" | awk \
	-v start=${start} -v end=${end} -v type="${CGI_acct_type}" -v status="${CGI_status}" -v expire="${CGI_expire}" \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		if($17 == "NULL" && type == "proxy") next;
		if($17 != "NULL" && type == "local") next;
		if(status != "" && $11 != status ) next;
		
		if(expire == "expire" && $9 >= 0)
			next;
		else if(expire == "soon") {
			if( $9 > 2592000 || $9 < 0)
			next;
		}

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

		gsub("%20", " ", $20);

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"pool_id\":\"%s\",", $(col++);
		printf "\"pool_name\":\"%s\",",$(col++);
		printf "\"account\":\"%s\",", $(col++);
		printf "\"password\":\"%s\",", $(col++);
		printf "\"online_ip\":\"%s\",", $(col++);
		printf "\"online_mac\":\"%s\",", $(col++);
		printf "\"start\":\"%s\",", $(col++);
		printf "\"end\":\"%s\",", $(col++);
		printf "\"lef_time\":\"%s\",", $(col++);
		printf "\"offtime\":\"%s\",", $(col++);
		printf "\"disable\":\"%s\",", $(col++);
		printf "\"login_fail\":\"%s\",", $(col++);
		printf "\"bind_mac\":\"%s\",", $(col++);
		printf "\"online_cnt\":\"%s\",", $(col++);
		printf "\"max_online\":\"%s\",", $(col++);
		printf "\"bind_ip\":\"%s\",", $(col++);
		printf "\"out_if\":\"%s\",", $(col++);
		printf "\"out_vlan1\":\"%s\",", $(col++);
		printf "\"out_vlan2\":\"%s\",", $(col++);
		printf "\"more_info\":\"%s\",", $(col++);
		printf "\"acct_type\":\"%s\"", ($17 == "NULL" ? "local" : "proxy");
		printf "}";

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


add_user_account()
{
	[ "${CGI_acct_type}" = "proxy" ] &&acct_type="${LANG011:=˺}" || acct_type="${LANG012:=˺}"
	[ "${CGI_action}" = "add_user_account" ] && action="add" || action="set"

	CGI_cname=`echo ${CGI_cname} | sed -r 's/ /\%20/g'`
	CGI_other=`echo ${CGI_other} | sed -r 's/ /\%20/g'`

	[ "${CGI_maxonline}" = "" ] && CGI_maxonline="1"
	[ "${CGI_bindmac}" = "" ] && CGI_bindmac="00-00-00-00-00-00"
	[ "${CGI_bindip}" = "" ] && CGI_bindip="0.0.0.0"

	cmdargs="poolid=${CGI_poolid} name=${CGI_name} start=${CGI_start} expire=${CGI_expire}"

	if [ "${CGI_acct_type}" = "local" ]; then
		cmdargs="${cmdargs} password=${CGI_passwd} maxonline=${CGI_maxonline} bindmac=${CGI_bindmac} bindip=${CGI_bindip} outvlan=${CGI_outvlan}"
	else
		cmdargs="${cmdargs} password=_123456_ outif=${CGI_outif} outvlan=${CGI_outvlan} outvlan1=${CGI_outvlan1}"
	fi

	cmdargs="${cmdargs} phone=${CGI_phone} card=${CGI_card} cname=${CGI_cname} other=${CGI_other}"

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

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		sync_floweye "pppoeacct ${action} ${cmdargs}"
		WEB_LOGGER "${LANG003:=û˺}" "action=${action} type=${acct_type} name=${CGI_name}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


addm_user_account()
{
	num=${CGI_start_num}
	today=`date +"%Y-%m-%d"`

	for line in `echo ${CGI_items} | tr "+" " "`
	do					
		name=`echo ${line} | cut -d ";" -f1`
		pswd=`echo ${line} | cut -d ";" -f2`
		pool=`echo ${line} | cut -d ";" -f3`
		expire=`echo ${line} | cut -d ";" -f4`
		cname=`echo ${line} | cut -d ";" -f5`
		idcard=`echo ${line} | cut -d ";" -f6`
		telphone=`echo ${line} | cut -d ";" -f7`
		other=`echo ${line} | cut -d ";" -f8`

		cmdargs="name=${name} poolid=${pool} start=${today} expire=${expire} password=${pswd}"
		cmdargs="${cmdargs} phone=${phone} card=${card} cname=${cname} other=${other}"

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

		if [ $? -ne 0 ]; then
			if [ "${errmsg}" = "ACCOUNT_EXIST" ]; then
				[ "${CGI_exist}" = "cover" ] && ${FLOWEYE} pppoeacct set ${cmdargs}
			else
				fail=$((${num}+1))
				retjson 1 "${fail}${errmsg}ѳɹ${num}"
			fi
		fi

		sync_floweye "pppoeacct add ${cmdargs}"
		num=$((${num} + 1))
	done

	retjson 0 "OK"
}


unbind_mac()
{
	num=0
	cmdargs=""

	for acct in `echo ${CGI_accounts} | tr "+" " "`
	do
		cmdargs="${cmdargs} unbindmac=${acct}"
		num=$((${num}+1))
	done

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

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		sync_floweye "pppoeacct config ${cmdargs}"
		WEB_LOGGER "${LANG004:=PPPOE˺MAC}" "${LANG005:=}=${num}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


delete_acct()
{
	num=0
	cmdargs=""

	for acct in `echo ${CGI_accounts} | tr "+" " "`
	do
		cmdargs="${cmdargs} name=${acct}"
		num=$((${num}+1))
	done

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

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		sync_floweye "pppoeacct remove ${cmdargs}"
		WEB_LOGGER "${LANG006:=ɾPPPOE˺}" "${LANG005:=}=${num}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


enable_acct()
{
	num=0
	cmdargs=""

	for acct in `echo ${CGI_accounts} | tr "+" " "`
	do
		cmdargs="${cmdargs} accten=${acct}"
		num=$((${num}+1))
	done

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

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		sync_floweye "pppoeacct config ${cmdargs}"
		WEB_LOGGER "${LANG007:=PPPOE˺}" "${LANG005:=}=${num}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


disable_acct()
{
	num=0
	cmdargs=""

	for acct in `echo ${CGI_accounts} | tr "+" " "`
	do
		cmdargs="${cmdargs} acctdis=${acct}"
		num=$((${num}+1))
	done
	
	errmsg=`${FLOWEYE} pppoeacct config ${cmdargs}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		sync_floweye "pppoeacct config ${cmdargs}"
		WEB_LOGGER "${LANG008:=PPPOE˺}" "${LANG005:=}=${num}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


edit_acctm()
{
	num=0

	[ "${CGI_poolid}" != "" ] && cmdargs="${cmdargs} poolid=${CGI_poolid}"
	[ "${CGI_maxonline}" != "" ] && cmdargs="${cmdargs} maxonline=${CGI_maxonline}"
	[ "${CGI_expire}" != "" ] && cmdargs="${cmdargs} expire=${CGI_expire}"

	for acct in `echo ${CGI_accounts} | tr "+" " "`
	do
		num=$((${num}+1))
		
		for line in `${FLOWEYE} pppoeacct get name=${acct}`
		do
			eval "L_${line}"
		done
		cmdargs="${cmdargs} password=${L_passwd} bindmac=${L_bindmac} bindip=${L_bindip}"
		cmdargs="${cmdargs} outvlan=${L_outvlan} phone=${L_phone} card=${L_card} cname=${L_cname} other=${L_other}"

		errmsg=`${FLOWEYE} pppoeacct set name=${acct} ${cmdargs}`
		[ $? -ne 0 ] && retjson 1 "${num}${errmsg}"
		sync_floweye "pppoeacct set name=${acct} ${cmdargs}"
	done

	WEB_LOGGER "${LANG009:=޸˺}" "${LANG005:=༭}=${num}"
	retjson 0 "${LANG002:=ɹ}"
}


export_account()
{
	account_conf="${PGETC}/account.conf"

	mkdir -p ${WEB_DOWNLOAD}

	if [ "${CGI_type}" = "csv" ]; then
		file_name="account.csv"
	else
		file_name="account.conf"
	fi
	
	download_path="${WEB_DOWNLOAD}/${file_name}"

	if [ ! -f "${account_conf}" ]; then
		echo "" > ${download_path}
		retjson 0 "OK" "{\"file_name\":\"${file_name}\"}"
	fi

	if [ "${CGI_type}" = "local" ]; then
		cp ${account_conf} ${download_path}
		WEB_LOGGER "${LANG010:=PPPOE˺}"
		retjson 0 "OK" "{\"file_name\":\"${file_name}\"}"
	fi

	#g="" n="" p="" b="" e="" s="" mac="" maxonline="" ip="" m=""
	if [ "${PALANG}" = "en" ]; then
		echo "\"Group\",\"Account\",\"Password\",\"Birth\",\"Expire\",\"Start\",\"Max_Online\",\"Bind_Mac\",\"Bind_IP\",\"More\"" > ${download_path}
	else
		echo "\"û\",\"˺\",\"\",\"ʱ\",\"ʱ\",\"Чʱ\",\"\",\"MAC\",\"IP\",\"Ϣ\"" > ${download_path}
	fi

	cat ${account_conf} | grep "^u " | while read line
	do
		g=""
		n=""
		p=""
		e=""
		s=""
		mac=""
		maxonline=""
		ip=""
		m=""

		for val in `echo ${line}`
		do
			if [ "${tag}" = "" ]; then
				tag=1
				continue
			fi
			export ${val}
		done

		mac=`echo ${mac} | tr "," ";"`
		echo "${g},${n},${p},${b},${e},${s},${maxonline},${mac},${ip},${m}" >> ${download_path}
	done

	WEB_LOGGER "${LANG010:=PPPOE˺}"
	retjson 0 "OK" "{\"file_name\":\"${file_name}\"}"
}


query_app()
{
	status="uninstall"
	[ -d "${RAMDISK}/app/adsyn/" ] && status="install"
	
	retjson 0 "${status}"
}


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

	"add_user_account"|"edit_user_account")
		action_check
		add_user_account
		;;

	"addm_user_account")
		action_check
		addm_user_account
		;;

	"unbind_mac")
		action_check
		unbind_mac
		;;
	
	"delete_acct")
		action_check
		delete_acct
		;;

	"enable_acct")
		action_check
		enable_acct
		;;
		
	"disable_acct")
		action_check
		disable_acct
		;;
		
	"edit_acctm")
		action_check
		edit_acctm
		;;

	"export_account")
		action_check
		export_account
		;;
		
	"query_app")
		query_app
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
