(defmodule nemoptera_config
(export all))
(defun default-conf-path () "data/nemoptera.conf")
(defun listen-port () 8731)
(defun load ()
(load (default-conf-path)))
(defun load (path)
(case (file:consult path)
(`#(ok ,terms)
(let ((conf (maps:from_list terms)))
(persistent_term:put 'nemoptera_conf conf)
`#(ok ,conf)))
(`#(error ,reason)
`#(error ,(tuple 'consult-failed path reason)))))
(defun reload ()
(persistent_term:erase 'nemoptera_conf)
(load))
(defun put (conf)
(persistent_term:put 'nemoptera_conf conf))
(defun get (key default)
(case (maps:find key (current))
(`#(ok ,value) value)
('error default)))
(defun db-path ()
(get 'db_path "data/nemoptera.db"))
(defun repo-dir ()
(get 'repo_dir "data/repos"))
(defun fossil-bin ()
(case (maps:find 'fossil_bin (current))
(`#(ok ,bin) bin)
('error
(case (which-fossil (path-dirs))
(`#(ok ,path) path)
('error 'fossil)))))
(defun write (pairs)
(let ((path (default-conf-path)))
(filelib:ensure_dir path)
(let ((body (lists:map
(lambda (pair)
(lists:flatten (io_lib:format "~p.~n" (list pair))))
pairs)))
(file:write_file path (lists:flatten body)))))
(defun current ()
(case (persistent_term:get 'nemoptera_conf 'error)
('error
(case (load)
(`#(ok ,conf) conf)
(_ #M())))
(conf conf)))
(defun path-dirs ()
(string:split (os:getenv "PATH" "") ":" 'all))
(defun which-fossil (dirs)
(case dirs
('() 'error)
(`(,dir . ,rest)
(let ((candidate (filename:join dir "fossil")))
(case (file:read_file_info candidate)
(`#(ok ,info)
(if (executable? info)
`#(ok ,candidate)
(which-fossil rest)))
(_ (which-fossil rest)))))))
(defun executable? (info)
(/= 0 (band (element 8 info) 511)))