FROM debian:bookworm-slim AS builder
RUN apt-get update && apt-get install -y --no-install-recommends \
        meson ninja-build git build-essential ca-certificates \
        cmake \
    && rm -rf /var/lib/apt/lists/*

#RUN pip3 install --break-system-packages meson ninja

#RUN git clone https://github.com/moo-the-cow/librist && \
RUN git clone https://code.videolan.org/rist/librist.git && \
    cd librist && \
    mkdir build && \
    cd build && \
    meson .. --default-library=static --buildtype=release -Db_lto=true -Duse_mbedtls=true && \
    ninja
FROM busybox:glibc
COPY --from=builder /librist/build/tools/rist2rist /usr/bin/
#COPY --from=builder /librist/build/tools/ristreceiver /usr/bin/
#COPY --from=builder /librist/build/tools/ristsender /usr/bin/
COPY --from=builder /librist/build/tools/ristsrppasswd /usr/bin/
COPY banner.txt /
# Create entrypoint script that generates SRP file
RUN echo '#!/bin/sh' > /entrypoint.sh \
    && echo 'cat /banner.txt' >> /entrypoint.sh \
    && echo 'if [ ! -z "$SRP_USER" ] && [ ! -z "$SRP_PASS" ]; then' >> /entrypoint.sh \
    && echo '    echo "Generating SRP file for user $SRP_USER..."' >> /entrypoint.sh \
    && echo '    ristsrppasswd "$SRP_USER" "$SRP_PASS" 2>/dev/null > /tmp/auth.srp' >> /entrypoint.sh \
    && echo '    if [ $? -eq 0 ] && [ -s /tmp/auth.srp ]; then' >> /entrypoint.sh \
    && echo '        echo "SRP file created successfully"' >> /entrypoint.sh \
    && echo '        export RIST_SRP_FILE="/tmp/auth.srp"' >> /entrypoint.sh \
	&& echo '        echo "--- SRP file contents: ---"' >> /entrypoint.sh \
	&& echo '        while IFS= read -r line; do echo "$line"; done < /tmp/auth.srp' >> /entrypoint.sh \
	&& echo '        echo "--------------------------"' >> /entrypoint.sh \
    && echo '    else' >> /entrypoint.sh \
    && echo '        echo "ERROR: Failed to create SRP file"' >> /entrypoint.sh \
    && echo '        exit 1' >> /entrypoint.sh \
    && echo '    fi' >> /entrypoint.sh \
    && echo 'fi' >> /entrypoint.sh \
    && echo 'exec "$@"' >> /entrypoint.sh \
    && chmod +x /entrypoint.sh

# Set the entrypoint to the created script
ENTRYPOINT ["/entrypoint.sh"]