FROM php:8.5-apache

RUN php -r 'if (version_compare(PHP_VERSION, "8.5.12", "<")) { fwrite(STDERR, "PHP 8.5.12 or newer is required\n"); exit(1); }'

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        ca-certificates \
        git \
        iputils-ping \
        wakeonlan && \
    rm -rf /var/lib/apt/lists/* && \
    chmod u+s "$(command -v ping)"

RUN git clone --depth 1 --branch 3.5.0 https://github.com/sciguy14/Remote-Wake-Sleep-On-LAN-Server.git /tmp/rwsols && \
    cp -a /tmp/rwsols/www/html/. /var/www/html/ && \
    rm -rf /tmp/rwsols && \
    rm -f /var/www/html/config_sample.php /var/www/html/index.html && \
    sed -i '/curl_close(/d' /var/www/html/index.php

COPY 000-default.conf /etc/apache2/sites-available/000-default.conf
COPY ports.conf /etc/apache2/ports.conf
COPY php-security.ini /usr/local/etc/php/conf.d/zz-security.ini
COPY config.php /usr/local/share/rwsols-config.php
COPY entrypoint.sh /entrypoint.sh

RUN chmod +x /entrypoint.sh && \
    a2enmod headers && \
    sed -i 's/^ServerTokens .*/ServerTokens Prod/; s/^ServerSignature .*/ServerSignature Off/' /etc/apache2/conf-available/security.conf && \
    echo 'ServerName localhost' >> /etc/apache2/apache2.conf

ENV APACHE2_PORT=80
ENV PASSPHRASE=admin
ENV RWSOLS_MAX_PINGS=15
ENV RWSOLS_SLEEP_TIME=5
ENV RWSOLS_COMPUTER_NAME=
ENV RWSOLS_COMPUTER_MAC=
ENV RWSOLS_COMPUTER_IP=
ENV RWSOLS_SLEEP_PORT=7760
ENV RWSOLS_SLEEP_CMD=suspend

ENTRYPOINT ["/entrypoint.sh"]
