(in-package :cl-blog)

(defparameter *blog-pathname-root*
  (make-pathname :directory
                 (pathname-directory
                  (asdf:component-pathname
                   (asdf:find-component
		    nil
		    #+cl-modules
		    #.(cl-modules:current-module-name :downcase t)
		    #-cl-modules "cl-blog")))))

(define-configuration-variable *blog-configuration*
  (parse-configuration-file
   (merge-pathnames "blog.conf" *blog-pathname-root*)))

(define-configuration-variable *blog-template*
    (parse-xml-from-file (merge-pathnames (configuration-value *blog-configuration* :template :blog)
                                          *blog-pathname-root*)))

(define-configuration-variable *entry-template*
    (parse-xml-from-file (merge-pathnames (configuration-value *blog-configuration* :entry-template :blog)
                                          *blog-pathname-root*)))

(define-configuration-variable *blog-short-name*
  (configuration-value *blog-configuration* :name :blog))

(define-configuration-variable *administrator*
  (configuration-value *blog-configuration* :administrator-name :blog))

(defun md5-from-string (md5sum)
  (loop for i below (- (length md5sum) 1) by 2
     collect (parse-integer md5sum :start i :end (+ i 2) :radix 16)))

(define-configuration-variable *administrator-password*
    (md5-from-string (configuration-value *blog-configuration* :administrator-password :blog)))

(define-configuration-variable *authentication-timeout*
    (configuration-value *blog-configuration* :authentication-timeout :blog))

(define-configuration-variable *reverse-archives*
  (not (eql (car (configuration-values *blog-configuration* :reverse-archives :blog)) :false)))

(define-configuration-variable *owner-email*
  (configuration-value *blog-configuration* :owner-email :blog))

(define-configuration-variable *categories*
  (configuration-value *blog-configuration* :categories :blog))

(define-configuration-variable *blog-url-root*
  (parse-urlstring (configuration-value *blog-configuration*
                                        :url-root :blog)))

(define-configuration-variable *internal-http-port*
  (configuration-value *blog-configuration* :internal-http-port :blog))

(define-configuration-variable *xml-rpc-pings*
    (configuration-values *blog-configuration* :xml-rpc-ping :blog))

(defparameter *entry-path*
  (merge-pathnames (make-pathname :directory '(:relative "entries"))
                   *blog-pathname-root*))

(defparameter *trackback-path*
  (merge-pathnames (make-pathname :directory '(:relative "trackbacks"))
                   *blog-pathname-root*))

(defparameter *user-path*
  (merge-pathnames (make-pathname :directory '(:relative "users"))
                   *blog-pathname-root*))

(defparameter *entry-index*
  (merge-pathnames "index" *entry-path*))

(defvar *listener*
  #+single-http-listener single-http-listener:*listener*
  #-single-http-listener
  (let ((fwd-url (araneida:copy-url *blog-url-root*)))
    (setf (araneida:url-port fwd-url) *internal-http-port*)
    (make-instance #+sbcl
                   'araneida:serve-event-reverse-proxy-listener
                   #-sbcl
                   'araneida:threaded-reverse-proxy-listener
                   :translations
                   `((,(araneida:urlstring *blog-url-root*)
                      ,(araneida:urlstring fwd-url)))
                   :address #(127 0 0 1)
                   :port (araneida:url-port fwd-url))))

(defparameter *new-entry-url* (merge-url *blog-url-root* "new"))

(defparameter *display-entry-url* (merge-url *blog-url-root* "display/"))

(defparameter *archives-url* (merge-url *blog-url-root* "archives"))

(defparameter *rss-url* (merge-url *blog-url-root* "archives.rss"))

(defparameter *edit-entry-url* (merge-url *blog-url-root* "edit/"))

(defparameter *delete-entry-url* (merge-url *blog-url-root* "delete/"))

(defparameter *email-redirect-url* (merge-url *blog-url-root* "contact"))

(defparameter *trackback-url* (merge-url *blog-url-root* "trackback/"))

(defparameter *login-url* (merge-url *blog-url-root* "login"))

(defparameter *logout-url* (merge-url *blog-url-root* "logout"))

(defparameter *admin-url* (merge-url *blog-url-root* "admin"))

(defparameter *edit-user-url* (merge-url *blog-url-root* "edit-user"))

(defvar *authenticated-user*)