# =========================================================
# Dockerfile - WebGIS PHP Application
# Nginx + PHP-FPM on Alpine (managed by entrypoint shell script)
# =========================================================

FROM php:8.2-fpm-alpine

# Install Nginx and required PHP extensions
RUN apk add --no-cache nginx \
    && docker-php-ext-install mysqli pdo pdo_mysql \
    && docker-php-ext-enable mysqli pdo_mysql

# CRITICAL: Allow ENV vars from Coolify (DB_HOST, DB_PASS, etc.)
# to be readable via getenv() in PHP. PHP-FPM strips env vars by default.
RUN set -eux; \
    if grep -qE '^[;[:space:]]*clear_env[[:space:]]*=' /usr/local/etc/php-fpm.d/www.conf; then \
        sed -i 's/^[;[:space:]]*clear_env[[:space:]]*=.*/clear_env = no/' /usr/local/etc/php-fpm.d/www.conf; \
    else \
        printf '\nclear_env = no\n' >> /usr/local/etc/php-fpm.d/www.conf; \
    fi

# Copy Nginx configuration
COPY nginx.conf /etc/nginx/nginx.conf

# Create nginx runtime directories
RUN mkdir -p /run/nginx && \
    mkdir -p /var/log/nginx

# Copy entrypoint script
# Fix Windows CRLF line endings (caused by git autocrlf) then make executable
COPY entrypoint.sh /entrypoint.sh
RUN sed -i 's/\r$//' /entrypoint.sh \
    && chmod +x /entrypoint.sh

# Set working directory
WORKDIR /var/www/html

# Copy all application files
COPY . .

# Set correct permissions
RUN chown -R www-data:www-data /var/www/html \
    && chmod -R 755 /var/www/html

# Expose port 80
EXPOSE 80

# Run entrypoint script
CMD ["/entrypoint.sh"]
