;; SPDX-FileCopyrightText: 2026 Andrew Oberstar <andrew@ajoberstar.org>
;; SPDX-License-Identifier: AGPL-3.0-only

(in-package #:org.ajoberstar.quixote)

(defun main ()
  (let* ((data-dir (pathname (uiop:getenv "QUIXOTE_VAR_DIR")))
         (db-path  (merge-pathnames "quixote.db" data-dir))
         (connect-args (list :sqlite3
                             :database-name db-path
                             :busy-timeout 5000))
         (static-dir (merge-pathnames "static/"
                                      (uiop:getenv "QUIXOTE_USR_DIR"))))
    (server:start :address (or (uiop:getenv "QUIXOTE_ADDRESS") "::1")
                  :port (let ((env-port (uiop:getenv "QUIXOTE_PORT")))
                          (if env-port
                              (parse-integer env-port)
                              5000))
                  :connect-args connect-args
                  :data-dir data-dir
                  :static-dir static-dir)
    (db:with-connection (connect-args)
      (db:sql-migrate))
    (unwind-protect
         (handler-case
             (loop (sleep most-positive-fixnum))
           (sb-sys:interactive-interrupt ()
             (format t "~&Received interrupt, shutting down~%")))
      (server:stop))))
