# Lighttpd modules needed by WebMCP
server.modules += (
  "mod_cgi",
  "mod_alias",
  "mod_setenv",
  "mod_rewrite",
  "mod_redirect",
 )

# Enable CGI-Execution of *.lua files through lua binary
cgi.assign += ( ".lua" => "/__INSERT_LOCAL_FILE_PATH_TO_LUA_BINARY_HERE__/lua" )

# Connect external URLs to server static files and the webmcp cgi interface
alias.url += (
  "/webmcp-demo/static/" => "/__INSERT_LOCAL_FILE_PATH_TO_DEMO_APPLICATION_HERE__/static/",
  "/webmcp-demo/"        => "/__INSERT_LOCAL_FILE_PATH_TO_WEBMCP_FRAMEWORK_HERE__/cgi-bin/" )

# Configure environment for demo application    
$HTTP["url"] =~ "^/webmcp-demo/" {
  setenv.add-environment += (
    "WEBMCP_APP_BASEPATH" => "/__INSERT_LOCAL_FILE_PATH_TO_DEMO_APPLICATION_HERE__",
    "WEBMCP_CONFIG_NAME"  => "demo")
}

# URL beautification
url.rewrite-once += (

  # do not rewrite static URLs
      "^/webmcp-demo/static/(.*)$" =>
      "/webmcp-demo/static/$1",

  # base URL
      "^/webmcp-demo/(\?(.*))?$" =>
      "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=0&_webmcp_module=index&_webmcp_view=index&$2",

  # module base URLs
      "^/webmcp-demo/([^/\?]+)/(\?(.*))?$" =>
      "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=1&_webmcp_module=$1&_webmcp_view=index&$3",

  # actions
      "^/webmcp-demo/([^/\?]+)/([^/\.\?]+)(\?(.*))?$" =>
      "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=1&_webmcp_module=$1&_webmcp_action=$2&$4",

  # views without numeric id or string ident
      "^/webmcp-demo/([^/\?]+)/([^/\.\?]+)\.([^/\?]+)(\?(.*))?$" =>
      "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=1&_webmcp_module=$1&_webmcp_view=$2&_webmcp_suffix=$3&$5",

  # views with numeric id or string ident
      "^/webmcp-demo/([^/\?]+)/([^/\?]+)/([^/\.\?]+)\.([^/\?]+)(\?(.*))?$" =>
      "/webmcp-demo/webmcp-wrapper.lua?_webmcp_urldepth=2&_webmcp_module=$1&_webmcp_view=$2&_webmcp_id=$3&_webmcp_suffix=$4&$6",

)

# Redirects for URLs without trailing slashes
url.redirect += (
  # base URL without trailing slash
      "^/webmcp-demo$" => "/webmcp-demo/",
  # module base URL without trailing slash
      "^/webmcp-demo/([^/\?]+)$" => "/webmcp-demo/$1/",
)
