#!/bin/sh
#
# Written by mr & ag 2002
# version 0.1
#

d_head1='<font face=verdana size=+2>'
d_head2='</font>'
d_thead1='<center><b><font color=ffffff face=verdana>'
d_thead2='</font></b></center>'
d_col_tborder='ffffff'
d_col_theadbg='000000'
d_col_theadbg2='ffffff'
d_col_tdefbg='f0f0f0'
d_col_tgroup1bg='6060ff'
d_col_tgroup2bg='ffff40'
d_col_tgroup3bg='c0c0ff'
d_col_tgroup4bg='ffffc0'
myname=`basename $0`
refresh=300
thisScript=main_cipe.cgi
state_fwrd_num=0

version=v0.1

param_page=cipe

#parse variables...
for var in `echo "$QUERY_STRING" | tr '&' ' '`
do
 if echo "$var" | grep -v "=" > /dev/null ; then var="$var=yes" ; fi
 eval "param_$var"
done

# parameter:
#	page=[cipe]
#	cipe=[start,stop]

# get config variables - necessary if used in cgi-script
# . /etc/rc.cfg

# Print HTML Header
echo "Content-Type: text/html; charset=iso-8859-1"
echo "Cache-control: no-store"
echo "Pragma: no-cache"
echo "Expires: `date -Ru`"
echo ""
echo "
<html>
<head>
 <title>CGIConfig $version</title>
 <meta HTTP-EQUIV=\"Refresh\" CONTENT=\"$refresh;url=$thisScript?page=$param_page\">
  <style type=\"text/css\"><!--
   a { text-decoration: none }
   a:hover { color: #ff0000; text-decoration: underline}
   td { font-family: verdana ; color: #000000}
   td.header { font-family: verdana ; color: #ffffff ; background: #000000 ; font-weight:bold ; text-align:center}
   td.bake { text-align:center}
   body { font-family: verdana}
   center.header {font-family: verdana ; font-size : large; color: #000000 ; }
   img {border: 0}
   
   table {background: #f0f0f0 ; border-color: #ffffff ; padding: 3 ; border-style:solid}
   
  --></style>
</head>
<body link=0000a0 vlink=0000a0 alink=808080>
<br>
<center>
"

#------------------------------------------------------------------------------
# ACTION: cipe=[start|stop]
#------------------------------------------------------------------------------

if [ "$param_cipe" != "" ]
then
	/usr/local/bin/ciped.sh $param_cipe > /dev/null
fi


#------------------------------------------------------------------------------
# PAGE: CIPE
#------------------------------------------------------------------------------
if [ "$param_page" = "cipe" ]
then
	echo "
		<center class=\"header\"> cipe <br><br>
		<table width=640>
		<tr>
			<td class=\"header\" width=\"28%\"> peer </td>
	  		<td class=\"header\" width=\"18%\"> my cipe ip </td>
			<td class=\"header\" width=\"18%\"> my dns ip</td>
	  		<td class=\"header\" width=\"18%\"> ping peer dns</td>
	  		<td class=\"header\" width=\"18%\"> ping peer cipe</td>
		</tr>
	"
	
	/usr/local/bin/ciped.sh status | \
	while read line 
	do
		id=`echo $line | cut -f1 -d" "`
		target=`echo $line | cut -f2 -d" "`

		if [ `echo $line | cut -f3 -d" " | cut -f2 -d":"` = "DOWN" ] ; then
			mycipe=DOWN
			action=start
		else
			mycipe=`echo $line | cut -f3 -d" " | cut -f2 -d":"`
			action=stop
		fi
		
		if [ `echo $line | cut -f4 -d" " | cut -f2 -d":"` = "DOWN" ] ; then
			mydns=DOWN
		else
			mydns=`echo $line | cut -f4 -d" " | cut -f2 -d":"`
		fi

		if [ `echo $line | cut -f5 -d" " | cut -f2 -d":"` = "DOWN" ] ; then
			peerdns=DOWN
		else
			peerdns=`echo $line | cut -f5 -d" " | cut -f2 -d":"`ms
		fi

		if [ `echo $line | cut -f6 -d" " | cut -f2 -d":"` = "DOWN" ] ; then
			peercipe=DOWN
		else
			peercipe=`echo $line | cut -f6 -d" " | cut -f2 -d":"`ms
		fi
		
	   	echo "<tr>"
	   	echo "<td> $target </td>"
#	   	echo "<td class=\"bake\"><a href=\"$thisScript?page=cipe&cipe=$action\">$mycipe</a></td>"
	   	echo "<td class=\"bake\">$mycipe</td>"
	   	echo "<td class=\"bake\">$mydns</td>"
	   	echo "<td class=\"bake\">$peerdns</td>"
	   	echo "<td class=\"bake\">$peercipe</td>"
		echo "</tr>"
	
	done
fi

##############################################################################################################################
# end of script

echo "</center></table><br><br>"
echo "<A HREF=\"$thisScript?page=cipe\">REFRESH</A>&nbsp;&nbsp;"
echo "(<A HREF=\"$thisScript?page=cipe&cipe=start\">start</A>/"
echo "<A HREF=\"$thisScript?page=cipe&cipe=stop\">stop</A> cipe)"
echo "<br><p align=right><font face=verdana size=-2>cipe-gui version $version</p></body></html>"

exit 
