#!/bin/sh
#This script is created by ssparser automatically. The parser first created by MaoShouyan
printf "Content-type: text/html
Cache-Control: no-cache

"
echo -n "";
. ../common/common.sh 
myself="/cgi-bin/Protocol/`basename $0`"
if [ "${CGI_appid}" = "" ]; then
	for appid in `${FLOWEYE} app showtree all | grep " usrapp" | awk '{print $3}'`
	do
		CGI_appid="${appid}"
		break
	done
fi

echo -n "
<script type=\"text/javascript\" src=\"/img/common.js\"></script>
<script languate=javascript>
function onAddApp() 
{
	window.location.href = \"/cgi-bin/Protocol/usrapp_add\";
}
function onEditApp(appid, appname) 
{
	window.location.href = \"/cgi-bin/Protocol/usrapp_edit?appid=\" + appid + \"&appname=\" + appname;
}
function onDeleteApp(appid)
{
	if (confirm(\"ɾЭ飬ôͬйصĲԶɾ,ȷҪɾ?\")) 
		window.location.href = \"/cgi-bin/Protocol/usrapp_list?action=delete\" + \"&appid=\" + appid;
}
</script>
";
if [ "${CGI_action}" = "delete" ]; then
	operator_check "${myself}"
	errmsg=`${FLOWEYE} app remove ${CGI_appid}`
	if [ "$?" != "0" ]; then
		afm_dialog_msg "ִ:${errmsg}"
	else
		afm_dialog_msg "ɹ!"
		CGI_appid=""
		for appid in `${FLOWEYE} app list2 | grep "^custom" | awk '{print $2}'`
		do
			CGI_appid="${appid}"
			break
		done
	fi
fi

echo -n "
<body>
"; cgi_show_title "->ԶЭ" 
echo -n "
<br>
<table width=900 border=0 cellspacing=1 cellpadding=1>
<tr id=tblhdr height=22>
	<td width=120 align=center>Э</td>
        <td width=240 align=center>TCP˿</td>
	<td width=240 align=center>UDP˿</td>
	<td width=120 align=center>ڵ</td>
        <td width=*   align=center><b><a style=\"color:#0000ff\" href=\"javascript:onAddApp()\">Э>></a></b></td>
</tr>
";
tdid="row1"
${FLOWEYE} app showtree all | while read tmp1 tmp2 appid appname type 
do
	[ "${type}" != "usrapp" ] && continue
	output=`${FLOWEYE} app get ${appid}`
	if [ "${output}" != "" ]; then
		CGI_appname=""
		CGI_appcname=""
		CGI_flowttl=""
		CGI_tports=""
		CGI_uports=""
		for nameval in ${output}; do
			name=`echo ${nameval} | cut -d'=' -f1`
			value=`echo ${nameval} | cut -d'=' -f2`
			case "${name}" in
			"name")
				CGI_appname="${value}"
				;;
			"cname")
				CGI_appcname="${value}"
				;;
			"flowttl")
				CGI_flowttl="${value}"
				;;
			"nodettl")
				CGI_nodettl="${value}"
				;;
			"tports")
				CGI_tports="${value}"
				[ "${CGI_tports}" = "NONE" ] && CGI_tports=""
				;;
			"uports")
				CGI_uports="${value}"
				[ "${CGI_uports}" = "NONE" ] && CGI_uports=""
				;;
			esac
		done 
	
		echo "<tr id=${tdid}>"	
		echo "<td id=${tdid} align=center><a style=\"color:#0000ff\" href=\"javascript:onEditApp('${appid}','${appname}')\">${CGI_appcname}</a></td>"
		echo "<td id=${tdid} align=center>${CGI_tports}</td>"
		echo "<td id=${tdid} align=center>${CGI_uports}</td>"
		echo "<td id=${tdid} align=center>${CGI_nodettl}</td>"
		echo "<td id=${tdid} align=center>&nbsp;&nbsp;&nbsp;<a style=\"color:#0000ff\" href=\"javascript:onEditApp('${appid}','${appname}')\">༭</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a style=\"color:#0000ff\" href=\"javascript:onDeleteApp('${appid}')\">ɾ</a></td>"
		echo "</tr>"
		if [ "${tdid}" = "row1" ]; then
			tdid="row2"
		else
			tdid="row1"
		fi
	fi
done

echo -n "
</table>
</body>
</html>
";