#!/bin/sh
#
# Shell-script webserver
#
# Used for SMArT, but it can be used for anything.
#
# 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

# If you like this program, if you want to put together your own proggie,
# you're allowed to. Of course you are. But _please_ contact me first!
# But, who wants to use this???

WEBSERVE_CONF=/usr/lib/smart/etc/webserve.conf

. $WEBSERVE_CONF
export WEBSERVE_ROOT
export WEBSERVE_CONF

read INPUT
REQUEST=$INPUT
while [ "$INPUT" != "
" ] && [ -n "$INPUT" ]; do
	if [ "`echo $INPUT | cut -d: -f1`" = Authorization ]; then
		USER_AUTHORIZATION=`echo $INPUT | cut -d" " -f2-`
		if [ -z "$WEBSERVE_AUTHORIZATION" ]; then
			WEBSERVE_AUTHORIZATION=$USER_AUTHORIZATION
			echo WEBSERVE_AUTHORIZATION=\"$WEBSERVE_AUTHORIZATION\" >> $WEBSERVE_CONF;
		else
			if [ "$USER_AUTHORIZATION" != "$WEBSERVE_AUTHORIZATION" ]; then
				USER_AUTHORIZATION=""
			fi;
		fi;
	fi				
	read INPUT
	if [ "$INPUT" != "
" ] && [ "$INPUT" ] && [ -z "`eval echo $\`echo $INPUT | cut -d: -f1 | tr abcdefghijklmnopqrstuvwxyz- ABCDEFGHIJKLMNOPQRSTUVWXYZ_\``" ]; then
		export `echo $INPUT | cut -d: -f1 | tr abcdefghijklmnopqrstuvwxyz- ABCDEFGHIJKLMNOPQRSTUVWXYZ_`="`echo $INPUT | cut -d" " -f2-`";
	fi;
done

if [ -z "$USER_AUTHORIZATION" ] && [ "$WEBSERVE_SECURITY" = "on" ]; then
	echo HTTP/1.0 401 Authorization Required
	echo WWW-Authenticate: Basic realm=\"$WEBSERVE_AUTHO_REALM\"
	echo Connection: close
	echo Content-Type: text/html
	echo
	cat $WEBSERVE_ERROR_DIR/$WEBSERVE_ERROR_401
	exit;
fi

COMMAND=`echo $REQUEST|cut -f1 -d" "`
OPTS=`echo $REQUEST|cut -f2 -d" "|cut -f1 -d"
"`
if [ -z "`echo $OPTS | grep \?`" ]; then
	PARAMS="";
else
	PARAMS="`echo $OPTS|cut -f2- -d"?"`";
fi
OPTS_NONLOCAL=$OPTS
OPTS="$WEBSERVE_ROOT"`echo $OPTS|cut -f1 -d"?"`

if echo $OPTS | grep '\.\.' > /dev/null; then
	echo HTTP/1.0 500 Internal Server Error
	echo Date: `date -u`
	echo Server: $WEBSERVE_NAME
	echo Connection: close
	echo Content-Type: text/html
	echo
	cat $WEBSERVE_ERROR_DIR/$WEBSERVE_ERROR_500
	exit;
fi

case $COMMAND in
	GET | get )
		if [ -e $OPTS ]; then
			echo HTTP/1.0 200 OK
			echo Date: `date -u`
			echo Server: $WEBSERVE_NAME
			echo Connection: close
			if [ -d $OPTS ]; then
				echo Content-Type: text/html
				echo
				if [ -z `echo $OPTS | grep /\$` ]; then
					OPTS=$OPTS/;
					OPTS_NONLOCAL=$OPTS_NONLOCAL/;
				fi
				if [ -e $OPTS/$WEBSERVE_DEFAULTFILE ]; then
					cat $OPTS/$WEBSERVE_DEFAULTFILE;
				else
					echo -e \<HTML\>\\n\<HEAD\>\\n\<TITLE\>$OPTS\</TITLE\>\\n\<\HEAD\>\\n\<BODY\>\<H1\>Directory of $OPTS_NONLOCAL\</H1\>\<TT\>
					for i in `/bin/ls -aw 1 $OPTS`; do
						echo \<A HREF=\"$OPTS_NONLOCAL$i\"\>$i\</A\>\<BR\>;
					done
					echo -e \</TT\>\</PRE\>\\n\</BODY\>\\n\<\HTML\>;
				fi;
			else
				if [ -z "`echo $OPTS | grep cgi`" ]; then
					if [ -z "`echo $OPTS | grep \.htm`" ]; then
						echo Content-Type: image/gif
						echo;
					else
						echo Content-Type: text/html
						echo;
					fi
					cat $OPTS;
				else
					$OPTS $PARAMS;
				fi;
			fi;
		else
			echo HTTP/1.0 404 Not Found
			echo Date: `date -u`
			echo Server: $WEBSERVE_NAME
			echo Connection: close
			echo Content-Type: text/html
			echo
			cat $WEBSERVE_ERROR_DIR/$WEBSERVE_ERROR_404;
		fi
		;;
esac
