#!/bin/sh
#
# CGI-script for setting up device list in nwserv.conf
#
# Copyright 2000 Wilmer van der Gaast (lintux@dds.nl)
#

#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

. $WEBSERVE_ROOT/cgi-bin/functions
. $WEBSERVE_ROOT/smart.conf

if [ "$2" != "x" ]; then
	echo Content-Type: text/html
	echo;
fi

checkrefer
checkparams $*

declare -i i

function listitems
{
	read LINE
	i=0
	while [ "$LINE" ]; do
		i=i+1
		echo -n \<TR\>
		echo \<TD\>\<INPUT TYPE=RADIO NAME=\"DEVICE\" VALUE=\"$i\"\>
		echo -n \<TD\>
		echo "$LINE" | cut -d" " -f2
		echo -n \<TD\>
		echo "$LINE" | cut -d" " -f3
		echo -n \<TD\>
		echo "$LINE" | cut -d" " -f4
		echo -n \<TD\>
		echo "$LINE" | cut -d" " -f5
		read LINE
	done;
}

case $1 in
	dev-flags )
		declare -i CURRENT F1 F2 F4
		CURRENT=`getconfig 5 | cut -d" " -f2`
		F1="1 - (($CURRENT & 1) / 1)"
		F2="($CURRENT & 2) / 2"
		F4="($CURRENT & 4) / 4"
		WDOG="`getconfig 310 | cut -d" " -f2`"
		$GENFORM < $SMART_ROOT/forms/dev-flags "$F1" "$F2" "$F4" "$WDOG"
;;
	dadd )
		$GENFORM < $SMART_ROOT/forms/device
;;
	list )
cat<<EOF
<HTML>
<HEAD>
<TITLE>SMArT - Network devices</TITLE>
</HEAD>
<BODY BGCOLOR=#C0C0C0>
<FORM ACTION="/cgi-bin/device" METHOD=GET>
<TABLE BORDER>
<TR>
<TH COLSPAN=5 BGCOLOR=$HEADER_BACK ALIGN=LEFT>
<TABLE BORDER=0 FRAME=0 CELLSPACING=0 CELLPADDING=0>
<TR>
<TD><IMG SRC="/smart_icon.jpg"></TD>
<TD><FONT COLOR=$HEADER_BACK>-</FONT></TD>
<TH><FONT COLOR=$HEADER_FORE>IPX-devices</FONT></TH>
</TR>
</TABLE>
<TR>
<TH>Sel
<TH>Network number
<TH>Network-interface
<TH>Frame Type
<TH>Timing
`getconfig 4 | listitems
`<TR>
<TD COLSPAN=5 ALIGN=CENTER>
<A HREF="/cgi-bin/device?dadd">Add device</A>
<INPUT TYPE=SUBMIT NAME="EDIT" VALUE="Edit">
<INPUT TYPE=SUBMIT NAME="DELETE" VALUE="Delete">
</TABLE>
</BODY>
</HTML>
EOF
;;
	* )
		export `echo $* | cut -d\& -f1`
		export `echo $* | cut -d\& -f2`
		if [ "$DELETE" ]; then
			grep -Fv "`getconfig_clean 4 | getentry $DEVICE`" < $MARS_CONFIG > $MARS_CONFIG.tmp
			mv $MARS_CONFIG.tmp $MARS_CONFIG
			$0 list x
			exit;
		elif [ "$EDIT" ]; then
			getconfig 4 | read X NETWORK_NUMBER INTERFACE FRAME_TYPE TICKS
			$GENFORM < $SMART_ROOT/forms/device `getconfig 4 | getentry $DEVICE | cut -d" " -f2-`;
		fi
;;
esac
