# Build stage
FROM rust:1.88-slim AS builder

RUN apt-get update && apt-get install -y --no-install-recommends \
    cmake \
    make \
    gcc \
    musl-tools \
    pkg-config \
    && rm -rf /var/lib/apt/lists/*

RUN rustup target add x86_64-unknown-linux-musl

WORKDIR /app

# Copy manifests and fetch dependencies (cached layer)
COPY Cargo.toml Cargo.lock ./
RUN mkdir src && echo 'fn main() {}' > src/main.rs && echo '' > src/lib.rs

RUN cargo fetch --target x86_64-unknown-linux-musl

# Copy real source and build static binary
# magic feature excluded (requires shared libmagic; fallback uses `file` command)
COPY src/ src/
RUN cargo build --release --target x86_64-unknown-linux-musl \
    --no-default-features --features lz4,gzip \
    && strip target/x86_64-unknown-linux-musl/release/keep

# Runtime stage - scratch since binary is fully static
FROM scratch

COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/keep /keep

EXPOSE 21080

ENTRYPOINT ["/keep"]
