#!/bin/sh

# set -x
CR="$(printf '\r')"

# REQUEST_METHOD  PATH_INFO  SERVER_PROTOCOL
read method pfad protocol

read kopfzeile
while test "$kopfzeile" != "" -a "$kopfzeile" != "$CR"; do
  read kopfzeile
done

status="500 OK"
test   -d "$pfad" || test -f "$pfad" && status="200 OK"
test ! -r "$pfad" && status="403 Forbidden"
test ! -e "$pfad" && status="404 Not Found"
test   -b "$pfad" && status="200 OK"

printf %s\\r\\n "HTTP/1.1 $status" "Content-Type: text/plain; charset=utf-8" ""


case "$status" in
  403*) echo Verbohohohoten!
        exit 1
        ;;
  404*) 
    echo Page not found or nevermore, quoth the server: 404
    ;;
  "200 OK")
    [ -f "$pfad" ] && cat -- "$pfad"
    [ -d "$pfad" ] && ls -lh -- "$pfad"
    [ -b "$pfad" ] && echo "Der $pfad ist ein Blockgerät, frisch ausm Block"
    ;;
  *) echo "Hups! irgendwas war jetzt blöd"
    exit 2
    ;;
esac
