FROM debian:stable-slim

ENV DEBIAN_FRONTEND=noninteractive

# Default is to run cron job in the 10th minute of each hour
# You can override at runtime with -e CRON_MINUTE=xx
ENV CRON_MINUTE=10
# Default port for http server inside container.
# You can override at runtime with -e PORT=xxxx
ENV PORT=8080

# Install required packages: curl, cron, busybox (httpd), ca-certificates
RUN apt-get update \
	&& apt-get install -y --no-install-recommends curl cron busybox ca-certificates gnupg dirmngr \
	&& rm -rf /var/lib/apt/lists/*

# Install Ookla Speedtest CLI via packagecloud repository
RUN curl -s https://packagecloud.io/install/repositories/ookla/speedtest-cli/script.deb.sh | bash \
	&& apt-get update \
	&& apt-get install -y --no-install-recommends speedtest \
	&& rm -rf /var/lib/apt/lists/*

# Create web root and data directories
RUN mkdir -p /var/www && mkdir -p /var/www/data/speed
WORKDIR /var/www

# Copy web assets and scripts
COPY src/index.html /var/www/index.html
COPY src/favicon.ico /var/www/favicon.ico
COPY src/gem_nw_128.png /var/www/icon.png
COPY src/run_speed_test.sh /usr/local/bin/run_speed_test.sh
COPY src/start.sh /usr/local/bin/start.sh

# Ensure scripts are executable
RUN chmod +x /usr/local/bin/run_speed_test.sh /usr/local/bin/start.sh

# Expose default HTTP port and declare data volume
VOLUME ["/var/www/data/speed"]
EXPOSE 8080

# Start script will launch cron and the HTTP server
CMD ["/usr/local/bin/start.sh"]
