FROM nginx:alpine

# Install bash, curl, openssl, coreutils (for date parsing), and cron
RUN apk update && apk add bash curl openssl coreutils dcron

# Clear the default Nginx welcome page first
RUN rm -rf /usr/share/nginx/html/*

# Setup workspace
COPY dashboard.html /usr/share/nginx/html/index.html
COPY monitor.sh /usr/local/bin/monitor.sh
RUN chmod +x /usr/local/bin/monitor.sh

RUN touch /usr/share/nginx/html/data.csv && \
    chown -R nginx:nginx /usr/share/nginx/html && \
    chmod -R 755 /usr/share/nginx/html
    
# Add cron job (runs every minute)
RUN echo "* * * * * /usr/local/bin/monitor.sh" | crontab -

# Custom entrypoint to start cron in background, then nginx in foreground
RUN echo '#!/bin/sh' > /entrypoint.sh && \
    echo 'crond -b -l 8' >> /entrypoint.sh && \
    echo 'nginx -g "daemon off;"' >> /entrypoint.sh && \
    chmod +x /entrypoint.sh

EXPOSE 80
CMD ["/entrypoint.sh"]