#!/bin/sh
#
# CGI-script for general settings 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

echo Content-Type: text/html
echo

checkrefer
checkparams $*

case $* in
	name )
		NAME=`getconfig 2|cut -d" " -f2`
		$GENFORM < $SMART_ROOT/forms/servername "$NAME"
;;
	internalnet )
		NUM=`getconfig 3|cut -d" " -f2`
		$GENFORM < $SMART_ROOT/forms/internalnet "$NUM"
;;
	version )
		declare -i BURST
		CURRENT=`getconfig 6 | cut -d" " -f2`
		BURST=`getconfig 6 | cut -d" " -f3`
		BURSTREAD=`getconfig 30 | cut -d" " -f2`
		BURSTWRITE=`getconfig 30 | cut -d" " -f3`
		$GENFORM < $SMART_ROOT/forms/version "$CURRENT" "$BURST" "$BURSTREAD" "$BURSTWRITE"
;;
	tests )
		CURRENT=`getconfig 16 | cut -d" " -f2`
		$GENFORM < $SMART_ROOT/forms/tests "$CURRENT"
;;
	dirs )
		CURRENT=`getconfig 4[012567] | cut -d" " -f2`
		$GENFORM < $SMART_ROOT/forms/dirs $CURRENT
;;
	smart )
function gop()
{
	while [ "$OPT" != "?" ]; do
		getopts "S:U:P:" OPT
		case $OPT in
			S ) export SERVERNAME=$OPTARG ;;
			U ) export SUPERVISOR=$OPTARG ;;
			P ) export PASSWORD=$OPTARG ;;
		esac;
	done;
}
		export `cat $WEBSERVE_CONF | grep "^WEBSERVE_SECURITY="` 2> /dev/null > /dev/null
		gop $SERVER_LOGIN
		$GENFORM < $SMART_ROOT/forms/smart "$MARS_CONFIG" "$NWSERV_BIN" "`digit $WEBSERVE_SECURITY`" "$SERVERNAME" "$SUPERVISOR" "$PASSWORD"
;;	
	configh )
		MAXCONN=`getconfig 60 | cut -d" " -f2`
		MAXVOLS=`getconfig 61 | cut -d" " -f2`
		MAXDBES=`getconfig 63 | cut -d" " -f2`
		SERIAL=`getconfig 70 | cut -d" " -f2`
		APPLIC=`getconfig 71 | cut -d" " -f2`
		MMAP=`getconfig 68 | cut -d" " -f2`
		SAP=`getconfig 69 | cut -d" " -f2`
		$GENFORM < $SMART_ROOT/forms/configh "$MAXCONN" "$MAXVOLS" "$MAXDBES" \
			"$SERIAL" "$APPLIC" "$MMAP" "$SAP"
;;
	timing )
		$GENFORM < $SMART_ROOT/forms/timing "`getconfig 210 | cut -d\" \" -f2`" \
			"`getconfig 211 | cut -d\" \" -f2`"
;;	
	mode )
	# Aargh... Pretty messy:
		declare -i DIR DUR DUW DUX DGR DGW DGX DOR DOW DOX FILE FUR FUW FUX FGR FGW FGX FOR FOW FOX
		DIR="0`getconfig 9 | cut -d" " -f2`"
		FILE="0`getconfig 9 | cut -d" " -f3`"
		if [ "$DIR" = "0" ]; then
			O_DIR="DSTD";
		elif [ "$DIR" = "-1" ]; then
			O_DIR="DINH";
		else
			O_DIR="DXXX"
			DUR="$DIR & 0400"; DUW="$DIR & 0200"; DUX="$DIR & 0100"
			DGR="$DIR & 0040"; DGW="$DIR & 0020"; DGX="$DIR & 0010"
			DOR="$DIR & 0004"; DOW="$DIR & 0002"; DOX="$DIR & 0001";
		fi
		if [ "$FILE" = "0" ]; then
			O_FILE="FSTD";
		else
			O_FILE="FXXX"
			FUR="$FILE & 0400"; FUW="$FILE & 0200"; FUX="$FILE & 0100"
			FGR="$FILE & 0040"; FGW="$FILE & 0020"; FGX="$FILE & 0010"
			FOR="$FILE & 0004"; FOW="$FILE & 0002"; FOX="$FILE & 0001";
		fi
		$GENFORM < $SMART_ROOT/forms/mode \
			"$O_DIR" "$DUR" "$DUW" "$DUX" "$DGR" "$DGW" "$DGX" "$DOR" "$DOW" "$DOX" \
			"$O_FILE" "$FUR" "$FUW" "$FUX" "$FGR" "$FGW" "$FGX" "$FOR" "$FOW" "$FOX"
;;
	guest )
		declare -i i
		CURRENT_GID="`getconfig 10 | cut -d" " -f2`"
		CURRENT_UID="`getconfig 11 | cut -d" " -f2`"
	{	i=0; for g in `cat /etc/group | cut -d: -f3`; do i=$i+1; done
		echo L,GROUP,Group name,$i
		cat /etc/group | sort | \
	{	read LINE
		while [ "$LINE" ]; do
			NAME="`echo $LINE | cut -d: -f1`"
			_GID="`echo $LINE | cut -d: -f3`"
			echo $_GID,$NAME
			if [ "$_GID" = "$CURRENT_GID" ]; then
				CURRENT_GID=$_GID\_$NAME;
			fi
			read LINE;
		done;
	}
		i=0; for g in `cat /etc/passwd | cut -d: -f3`; do i=$i+1; done
		echo L,USER,User name,$i
		cat /etc/passwd | sort | \
	{	read LINE
		while [ "$LINE" ]; do
			NAME="`echo $LINE | cut -d: -f1`"
			_UID="`echo $LINE | cut -d: -f3`"
			echo $_UID,$NAME
			if [ "$_UID" = "$CURRENT_UID" ]; then
				CURRENT_UID=$_UID\_$NAME;
			fi
			read LINE;
		done;
	}
	}	| $REPLACE $SMART_ROOT/forms/guest | $GENFORM "$CURRENT_GID" "$CURRENT_UID"
;;
esac
