(uiop:define-package :lispusers/tfw/toy/lid-server
    (:mix :cl))

#|
Note that this repository is at
git@codeberg.org:tfw/lispusers # my clone
git@codeberg.org:j9000/lispusers # Central one

I guess my 100daystooffload #phloggersgarage (libera.chat)
gopherhole is gopher://tilde.club/1/~screwtape
and my gopherhole front door is
gopher://gopher.club/1/users/screwtape
and my gopherhole back door is
gopher://tilde.institute/1/~screwtape/
|#

(in-package :lispusers/tfw/toy/lid-server)
#|
(asdf:load-system :lispusers/tfw/toy/lid-server)
|#
#|
I'm going to make a toy out of the veilid server codes.

This intends to serve three purposes: A lambda calculus
description of some of veilid's behaviours at some scales
as documentation

A common lisp second implementation spanning much of the
veilid server.

An easy port to ACL2 computational logic, regarding this
as important to a cryptographic exchange protocol. The
primary porting solely being to replace special scope
assignment with single threaded objects: Probably the
loop facility and formatted output will need to be
separated.
|#

;;;;Possible command line arguments
(defvar *VEILID-server-keywords*
  '(:daemon :foreground :config_file :set_config :password :new_password
    :no_attach :logging :otlp :subnode_index :generate_key_pair :set_node_id
    :delete_protected_store :delete_table_store :delete_block_store
    :dump_config :dump_txt_record :emit_schema :bootstrap :panic :network_key
    :wait_for_debug :console))
(setf (documentation '*veilid-server-keywords* 'variable) "
A list of keywords being all the fields of the rust struct
CmdlineArgs in veilid/veilid-server/src/main.rs

in general --generate_key becomes :GENERATE_KEY. Short forms
are ignored.
")

#| parse-lid-arguments
Loosely analogous to parse() in main.rs: a list of strings becomes
a list of strictly members of allowed keywords, resulting in a
list suitable for lisp's #'APPLY.
values are left as strings.
|#
(defun parse-lid-arguments (string-list &optional (keyword-pairs ())) "
PARSE-LID-ARGUMENTS
ARGS:
	command-line-arguments
		a list of strings as from
		(uiop:command-line-arguments)
VALUES:
	List of (:this \"value\" :that \"thatvalue\") suitable for #'apply.

> (parse-lid-arguments '(\"--generate_key\" \"1\"))
'(:GENERATE_KEY 1)

NB
The reason this is like this is to have an implicit logically easy measure
of recursion (string-list becomes shorter and at least finishes at length 0)
and it doesn't do anything not easily shadowable in ACL2 computational logic
"
  (if (zerop (length string-list)) keyword-pairs
      (let* ((keyword-string (concatenate 'string ":"
					  (string-upcase
					   (subseq (car string-list) 2))))
	     (val-string (cadr string-list))
	     (rest (cddr string-list))
	     (membered (member keyword-string *VEILID-SERVER-KEYWORDS*
			       :key #'write-to-string
			       :test #'equal)))
	(cond ((not membered)
	       (error "~a~%not in *VEILID-SERVER-KEYWORDS*" keyword-string))
	      (membered
	       (parse-lid-arguments
		rest
		(append keyword-pairs (list (car membered) val-string))))))))

#|
I guess the really interesting function will be whatever
run_veilid_server(settings, server_mode, veilid_logs).await
does.
(
since veilid_core::Crypto::generate_keypair() is intended to be farmed out to
#:ironclad , which will be interesting too I guess

whatever config file will need to become an s-expression I guess.
)
|#

#|
If you have any thoughts, I would love to hear them
ideally at mastodon.sdf.org/@screwtape in two cases
1) If you, like me have no idea what you're talking about
2) If you, like me have some ideas you'd like to talk about
What the ideas are is secondary.

Since using veilid consists of running the server, I'm just walking through
whatever running the server does by way of figuring out the current state of
veilid.
|#

#|
WHAT WAS VEILID
veilid is a distributed hashtable with some elements of a different implementation
of onion routing. This means it's not clear to commercial data brokers which data
and transmissions belong to which people, so they cannot scrape it to sell about
you.

I think veilid is going to be a nice substrate for the gopher: While veilid basically
adds ed25519 tls as does gemini, instead veilid uses this to create an opaquely
distributed hash table (like IPFS / interplanetary filesystem), while also replacing
easily marked individual victims with a swarming flock of starlings, or flying
gophers if you will.
|#