--
-- little helper function
--
function path_exists(path)
   local ret=false
   local f = io.open(path, "r")
   if (f) then
     ret=true
     -- kind cleanup :)
     f:close()
    end
    return ret
end

-- the magic ;)
if (not path_exists(lighty.env["physical.path"])) then
    -- file does not exist. check if we have a cached version
    lighty.env["physical.path"] = lighty.env["physical.path"] .. ".html"

    if (not path_exists(lighty.env["physical.path"])) then
        -- file still missing. pass it to the fastcgi backend
        lighty.env["uri.path"] = "/dispatch.fcgi"
        lighty.env["physical.rel-path"] = lighty.env["uri.path"]
        lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
    end
end
-- fallthrough will put it back into the lighty request loop
-- that means we get the 304 handling for free. ;)

-- debugging code
-- print ("final file is " ..  lighty.env["physical.path"])
