FROM debian:bookworm-slim

# Install SBCL, ripgrep, and build dependencies
RUN apt-get update && \
    apt-get install -y sbcl build-essential curl git ripgrep libsqlite3-dev lynx python3 python3-pip && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Install Quicklisp globally
RUN curl -O https://beta.quicklisp.org/quicklisp.lisp && \
    sbcl --non-interactive \
         --load quicklisp.lisp \
         --eval '(quicklisp-quickstart:install :path "/opt/quicklisp")' \
         --eval '(ql-util:without-prompting (ql:add-to-init-file))' && \
    rm quicklisp.lisp

# Set up the working directory
WORKDIR /app

# Copy source code and system definition
COPY opencortex.asd /app/
COPY src/ /app/src/

# Ensure we aren't using a stale binary from the host
RUN rm -f /app/opencortex-server

# Build the standalone binary natively inside the container
# This ensures GLIBC compatibility with the runtime environment.
RUN sbcl --non-interactive \
         --eval '(push "/app/" asdf:*central-registry*)' \
         --eval '(ql:quickload :opencortex)' \
         --eval '(asdf:make :opencortex)'

# Ensure the binary is executable
RUN chmod +x /app/opencortex-server

# Expose the communication protocol and Web Dashboard ports
EXPOSE 9105 8080

# The app expects the memex to be mounted here
VOLUME /memex

# Run the natively compiled standalone daemon
CMD ["./opencortex-server"]
