# syntax=docker/dockerfile:1

# Builds omnisearch + libbeaker from upstream, then runs on a slim image.
#
# Notes:
# - This repo is written in C and links against libbeaker (shared library).
# - At runtime, libbeaker must be present in the dynamic linker path.
# - The server binds to 0.0.0.0:8087 by default in example-config.ini.

# --- build stage ---
FROM debian:trixie AS build

WORKDIR /src

# Toolchain + headers for building omnisearch and beaker
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    git \
    make \
    gcc \
    libc6-dev \
    libcurl4-openssl-dev \
    libxml2-dev \
    libssl-dev \
 && rm -rf /var/lib/apt/lists/*

# Fetch and build/install beaker from upstream
RUN git clone https://git.bwaaa.monster/beaker /tmp/beaker \
 && make -C /tmp/beaker \
 && make -C /tmp/beaker install \
 && rm -rf /tmp/beaker

# Copy omnisearch source
COPY . .

# Build omnisearch (produces bin/omnisearch)
RUN make

# --- runtime stage ---
FROM debian:trixie-slim AS runtime

WORKDIR /app

# Runtime shared libs + TLS roots
RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    libcurl4 \
    libxml2 \
    libssl3 \
 && rm -rf /var/lib/apt/lists/*

# Copy binary + assets
COPY --from=build /src/bin/omnisearch /usr/local/bin/omnisearch
COPY --from=build /src/templates /app/templates
COPY --from=build /src/static /app/static
COPY --from=build /src/config.ini /app/config.ini

# Bundle libbeaker from build stage and register likely library directories with the dynamic loader.
# Don't assume an exact SONAME/filename (it might be libbeaker.so.0, etc) or install location.
COPY --from=build /usr/local/lib/libbeaker.so* /usr/local/lib/
COPY --from=build /usr/lib/libbeaker.so* /usr/lib/
COPY --from=build /usr/lib/*-linux-gnu/libbeaker.so* /usr/lib/
RUN set -eux; \
    echo "/usr/local/lib" > /etc/ld.so.conf.d/omnisearch.conf; \
    echo "/usr/lib" >> /etc/ld.so.conf.d/omnisearch.conf; \
    echo "/usr/lib/x86_64-linux-gnu" >> /etc/ld.so.conf.d/omnisearch.conf; \
    echo "=== libbeaker debug listing ==="; \
    (ls -la /usr/local/lib | grep -i beaker) || true; \
    (ls -la /usr/lib | grep -i beaker) || true; \
    (ls -la /usr/lib/x86_64-linux-gnu | grep -i beaker) || true; \
    # Ensure an unversioned libbeaker.so exists somewhere in the loader path. \
    # Some installs only provide versioned files (libbeaker.so.0 / libbeaker.so.0.0.0). \
    if ! [ -e /usr/local/lib/libbeaker.so ] && ! [ -e /usr/lib/libbeaker.so ] && ! [ -e /usr/lib/x86_64-linux-gnu/libbeaker.so ]; then \
        v="$({ ls -1 /usr/local/lib/libbeaker.so.* /usr/lib/libbeaker.so.* /usr/lib/x86_64-linux-gnu/libbeaker.so.* 2>/dev/null || true; } | head -n1)"; \
        if [ -z "$v" ]; then \
            echo "ERROR: libbeaker not found in runtime image after COPY. Check build-stage install location."; \
            exit 1; \
        fi; \
        d="$(dirname "$v")"; \
        ln -s "$(basename "$v")" "$d/libbeaker.so"; \
    fi; \
    ldconfig

# Run as non-root
RUN useradd --system --home /app --shell /usr/sbin/nologin appuser \
 && chown -R appuser:appuser /app
USER appuser

EXPOSE 8087

# Many setups read config.ini from the working directory.
# If omnisearch uses an env var or CLI flag for config, adjust CMD accordingly.
CMD ["omnisearch"]
