(module hwiki mzscheme
        (require (planet "instaweb.ss" ("schematics" "instaweb.plt" 1)))
        (require (lib "getinfo.ss" "setup"))
        (require (lib "file.ss"))
        (require "util.scm")
        (provide hwiki)
        (define (hwiki . port)
          (let ((_port    (if (null? port) 
                              (car port)))
                (hwiki-dir (car (find-relevant-directories '(hwiki)))))
            (if (not (file-exists? "rewrite-rules.scm"))
                (display (string-append
                          "Warning: No rewrite rules defined.\n"
                          "Servlet will be available through '/servlets/hwiki.scm/<page.html>'\n\n"))
            (if (not (file-exists? "hwiki-plugins.scm"))
                (error (format
                         "Error: 'hwiki-plugins.scm' is mandatory. You need to\n"
                         "configure the plugins to use with hwiki.\n"
                         "Default can be found in '~a/examples/hwiki-plugins.scm'\n\n"
            (if (not (file-exists? "hwiki-sqli.scm"))
                  (display (format
                             "Warning: For hwiki you can provide a database provider for SQLID.\n"
                             "PostgreSQL is recommended. Please provide a sqld provider that \n"
                             "calls sqld-<driver>-new in 'hwiki-sqli.scm', function 'sqli-provider').\n"
                             "This function must return a valid sqli object that is connected to.\n"
                             "a database or return #f, if no database is present. HWiki will not have\n"
                             "all functionality if no sqli provider is given. This sqli provider must\n"
                             "handle threads\n"
                             "An example can be found in '~a/examples/hwiki-sqli.scm'\n\n")
                  (let ((fh (open-output-file "hwiki-sqli.scm" 'replace)))
                    (write '(module hwiki-sqli mzscheme (provide sqli-provider) (define (sqli-provider) #f)) fh)
                    (close-output-port fh))))
            ;;;; Create configuration
            (let ((file (build-path hwiki-dir "shwiki.scm")))
              ;; servlets
              (copy-to file "hwiki.scm")
              (putenv "HWIKI_TINYMCE"  (path->string (build-path hwiki-dir "tinymce")))
              (putenv "HWIKI_PLUGINS"  (path->string (build-path (current-directory) "hwiki-plugins.scm")))
              (putenv "HWIKI_REWRITES" (path->string (build-path (current-directory) "rewrite-rules.scm")))
              (putenv "HWIKI_DATA"     (path->string (build-path (current-directory) "default-web-root" "hwiki-data")))
              (putenv "HWIKI_HTDOCS"   (path->string (build-path (current-directory) "default-web-root" "htdocs")))
              (putenv "HWIKI_SQLI"     (path->string (build-path (current-directory) "hwiki-sqli.scm")))
              (putenv "HWIKI_SRCDIR"   (path->string (build-path hwiki-dir)))
              (let ((logs (path->string (build-path (current-directory) "logs"))))
                (putenv "HWIKI_LOGS"     logs)
                (make-directory* logs))
            (instaweb "hwiki.scm" _port)))