#!/bin/sh
#
# Basic main sws script written in shell-script
#
#  - pancake at phreaker.net

# Main Defines
SWS=../src/Script/sws
OS=`uname`
DATE=`date`

# Parse methods
if [ "${METHOD}" = "STATUS" ]; then
	echo "TcpWinSize:    $TCPWINSIZE"
	echo "ServerVersion: $VERSION"
	echo "RemoteIP:      $REMOTE_ADDR"
	echo "RemotePort:    $REMOTE_PORT"
	echo "ListenIP:      $LISTEN_ADDR"
	echo "ListenPort:    $LISTEN_PORT"
	echo "Family:        $FAMILY"
	echo "Method:        $METHOD"
else
if [ "${METHOD}" = "GET" ]; then
	ERR=0
	if [ -e "${PWD}/${FILE}" ]; then
		if [ -d "${PWD}/${FILE}" ]; then
			if [ -e "${PWD}/${FILE}/index.html" ]; then
				FILE=${FILE}/index.html
				FILETYPE="html"
			else
				ERR="1";
			fi
		fi
		if [ "${ERR}" = "0" ]; then
			if [ "1.1" = "${HTTP}" ]; then
				printf "HTTP/1.1 200 OK\r\n"
				printf "Date: ${DATE}\r\n"
				printf "Server: SWS/${VERSION} running on ${OS}\r\n"
				if [ "${FILETYPE}" = "html" ]; then
					printf "Content-Type: text/html; charset=iso-8859-1\r\n"
				fi
				printf "\r\n"
			fi
			if [ ! "${FILETYPE}" = "sws" ]; then
				SWS="cat"
			fi
			${SWS} "${PWD}/${FILE}";
		fi
	else
		ERR=1
	fi
	if [ "${ERR}" = "1" ]; then
		if [ "1.1" = "${HTTP}" ]; then
		printf "HTTP/1.1 404 Not Found\r\n"
		printf "Date: ${DATE}\r\n"
		#Mon, 30 Jun 2003 00:00:32 GMT\r\n"
		printf "Server: Screws/${VERSION} running on ${OS}\r\n"
		printf "Connection: close\r\n"
		#printf "Transfer-Encoding: chunked\r\n"
		printf "Content-Type: text/html; charset=iso-8859-1\r\n"
		# # this is chunked # #
		#printf "\r\n7\r\npancake\r\n\r\n0\r\n\r\n"
		fi
		printf "\r\n<html><b>The requested file '${FILE}' was not found</b></html>\r\n"
	fi
else
	printf "<html>File <i>\"${FILE}\"</i> not found</html>\r\n";
fi
fi
