#!/bin/bash
#
# Converts a request with a corresponding .[ch] file to html
#

file=${REQUEST_URI#*/src/}
export BASILISK=${PWD%%/cgi-bin}
export BASILISK_URL=http://$HTTP_HOST

file="$BASILISK/$file"
basename=${file##*/}
dirname=${file%/*}

debug()
{
    cat<<EOF
Content-type: text/html

<pre>
file: $file
basename: $basename
dirname: $dirname
BASILISK: $BASILISK
BASILISK_URL: $BASILISK_URL
</pre>
EOF
}

if cd $dirname &&  \
   make -f $BASILISK/cgi-bin/Makefile $basename.html > $basename.log 2>&1; then
    echo -e "Content-type: text/html\n"
    cat $basename.html
else
    cat <<EOF
Status: 500 Internal Server Error
Content-Type: text/html

<h1>500 Internal Server Error</h1>
While retrieving URL $REQUEST_URI.
<p>
$dirname/$basename.log:
EOF
    echo "<pre>"
    cat $basename.log
    echo "</pre>"
fi
rm -f $basename.log
