# Get busybox and dependencies from Alpine
FROM alpine:latest AS busybox_builder

# Create directories for busybox, and libraries
WORKDIR /busybox_deps
RUN mkdir bin lib

# Ensure busybox (which includes sh and nc)
RUN apk update && apk upgrade busybox busybox-extras

# Copy the busybox binary
RUN cp /bin/busybox bin/

# Create the necessary symlinks (sh, nc) using busybox
RUN /busybox_deps/bin/busybox --install -s /busybox_deps/bin

# Copy the musl dynamic loader (essential for running Alpine binaries)
RUN cp /lib/ld-musl-*.so.1 lib/
# Put the script into the builder and chmod it
COPY readiness-port-script.sh /bin/readiness-port-script.sh
RUN chmod +x /bin/readiness-port-script.sh

# Build the final image using cloaked search as the base(which is FROM scratch)
FROM gcr.io/ironcore-images/cloaked-search:2

# Copy the musl dynamic loader from the builder stage
COPY --from=busybox_builder /busybox_deps/lib /lib

# Copy executables from the builder
COPY --from=busybox_builder /busybox_deps/bin/busybox /bin/busybox
COPY --from=busybox_builder /busybox_deps/bin/sh /bin/sh
COPY --from=busybox_builder /busybox_deps/bin/nc /bin/nc
COPY --from=busybox_builder /bin/readiness-port-script.sh /bin/readiness-port-script.sh

# Set the PATH environment variable since the base is scratch
# This allows the system to find binaries in /bin
ENV PATH=/bin

CMD ["/app/cloaked-search-proxy", "/app/deploy.json"]


