#!/bin/sh

# shellver - a minimalistic www server written in bash(1) script
# written by Radovan Garabik
# version 0.0.2

www_root=/var/www  # without ending / 

read request
rfile=`echo $request | awk '{print $2;}' ` # this is incorrect - we assume
					   # that browser is sending GET
					   # which might not be true

file=$www_root$rfile

if [ -d $file ]; then
cd $file
 if [ -f index.html ]; then
   file=index.html  
 else
  if [ "`echo $file | grep /\$`" != "$file" ]; then
cat <<EOM
HTTP/1.0 301 Moved Permanently
Server: shellver 0.0.2
Content-Type: text/html
Location: $rfile/


<a href=$rfile/>$rfile</a> is a directory!

EOM

   exit 0
  fi
cat <<EOM
HTTP/1.0 200 OK
Server: shellver/0.0.2
Content-Type: text/html



<h1>$file is a directory!</h1>
<pre>
EOM
  for i in * ; do
   echo -n "<a href=\"$i\"> "; \ls -d -l $i;
   echo "</a>"
  done
  echo "</pre>"
  exit 0
 fi
fi

if [ -f $file ]; then
 if [ "`echo $file | grep .cgi\$`" = "$file" ]; then
cat <<EOM
HTTP/1.0 200 OK
Server: shellver/0.0.2
EOM
  $file
 else  
  cat $file
 fi
else
cat <<EOM
HTTP/1.0 404 Not found
Server: shellver/0.0.2
Content-Type: text/html



<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD>
<BODY><H1>404 Not Found</H1>
<center>The requested file $rfile is not here, or you do not have 
the rights to read it.</center>
</BODY></HTML>

EOM

fi

