FROM httpd
RUN apt-get update && apt-get install -y python3 python3-pip && apt-get clean

# 2. Enable CGI module (CRITICAL)
RUN sed -i 's/#LoadModule cgid_module/LoadModule cgid_module/' /usr/local/apache2/conf/httpd.conf

# 3. Configure Apache
# Remove any existing ScriptAlias to prevent conflicts
RUN sed -i '/ScriptAlias \/cgi-bin\//d' /usr/local/apache2/conf/httpd.conf

# Set DocumentRoot and append the execution logic
RUN sed -i 's|DocumentRoot "/usr/local/apache2/htdocs"|DocumentRoot "/app"|' /usr/local/apache2/conf/httpd.conf && \
    echo '\n\
ScriptAlias /cgi-bin/ "/app/cgi-bin/"\n\
<Directory "/app/cgi-bin">\n\
    Options +ExecCGI\n\
    AddHandler cgi-script .py\n\
    Require all granted\n\
</Directory>\n\
\n\
<Directory "/app">\n\
    Options Indexes FollowSymLinks\n\
    AllowOverride None\n\
    Require all granted\n\
</Directory>' >> /usr/local/apache2/conf/httpd.conf

# Set Environment Variable so Python finds your root sources
ENV PYTHONPATH="/app"
WORKDIR /app

COPY requirements.txt /tmp
RUN pip install --break-system-packages -r /tmp/requirements.txt

ENV DATA_PATH_FIRMWARE_SERVER="/data"

