(defpackage 
    :apollo-config
    (:use :cl)
    (:export :get-config-value)
)

(in-package :apollo-config)

(defun get-config-value(name)
    (let
        (
            (path-to-config (merge-pathnames #P".apollo/config" (user-homedir-pathname)))
        )
        (with-open-file (stream path-to-config :direction :input)
            (file-position stream :start)
            (get-value-do name stream)
        )
    )
)

(defun get-word-before-equals-char-do (str i acc)
    (if (or (>= i (length str))
            (char= (char str i) #\=)
        )
        acc
        (progn
            (push (char str i) acc)
            (get-word-before-equals-char-do str (+ 1 i) acc)
        )
    )
)

(defun get-word-before-equals-char (str)
    (let
        (
            (i 0)
            (acc ())
        )
        (get-word-before-equals-char-do str i acc)
    )
)

(defun get-value-do(name stream)
    (let 
        (
            (key nil)
            (value nil)
            (line (read-line stream nil nil))
        )
        (when line
            (setf key (coerce (reverse (get-word-before-equals-char line)) 'string))
            (setf value (coerce (get-word-before-equals-char (reverse line)) 'string))
            (if (string= key name)
                value
                (get-value-do name stream)
            )
        )
    )
)

