# Piwigo Docker Image
# Based on official Piwigo image - minimal customization

FROM piwigo/piwigo:latest

# Install additional tools for maintenance (Alpine uses apk)
RUN apk add --no-cache \
    mariadb-client \
    rsync

# Set recommended PHP settings
RUN mkdir -p /usr/local/etc/php/conf.d && \
    echo 'memory_limit = 512M' > /usr/local/etc/php/conf.d/piwigo-custom.ini && \
    echo 'max_execution_time = 300' >> /usr/local/etc/php/conf.d/piwigo-custom.ini && \
    echo 'upload_max_filesize = 100M' >> /usr/local/etc/php/conf.d/piwigo-custom.ini && \
    echo 'post_max_size = 100M' >> /usr/local/etc/php/conf.d/piwigo-custom.ini && \
    echo 'max_file_uploads = 50' >> /usr/local/etc/php/conf.d/piwigo-custom.ini

# Set default timezone to avoid TZ error in s6-overlay
ENV TZ=UTC

# Create custom startup script in s6 init directory (runs during container init)
RUN mkdir -p /etc/cont-init.d && \
    printf '#!/bin/bash\n# Fix writable directories for Piwigo\nset -e\n\n# Ensure _data directory exists\nmkdir -p /var/www/html/_data\n\n# Fix permissions on all writable directories\nfor dir in /var/www/html/_data /var/www/html/upload /var/www/html/galleries; do\n    if [ -d "$dir" ]; then\n        chown -R www-data:www-data "$dir" 2>/dev/null || true\n        chmod -R 775 "$dir" 2>/dev/null || true\n    fi\n    mkdir -p "$dir"\n    chown -R www-data:www-data "$dir" 2>/dev/null || true\n    chmod -R 775 "$dir" 2>/dev/null || true\n    find "$dir" -type d -name "templates_c" -exec chmod -R 777 {} \\; 2>/dev/null || true\n    find "$dir" -type d -name "themes" -exec chmod -R 777 {} \\; 2>/dev/null || true\ndone\n\n# Also fix templates_c anywhere in _data\nfind /var/www/html/_data -type d -name "templates_c" -exec chmod -R 777 {} \\; 2>/dev/null || true\nfind /var/www/html/_data -type d -name "themes" -exec chmod -R 777 {} \\; 2>/dev/null || true\n\n# If config exists but no database, wait for DB\nif [ -f "/var/www/html/local/config/config.inc.php" ]; then\n    echo "Config found, checking database connection..."\n    \n    DB_HOST=$(grep -oP '"'"'"'"'"'"'db_host'"'"'"'"'"'"'\s*=>\s*'"'"'"'"'"'"'\K[^'"'"'"'"'"'"']+'"'"'"'"'"'"' /var/www/html/local/config/config.inc.php 2>/dev/null || echo "db")\n    DB_USER=$(grep -oP '"'"'"'"'"'"'db_user'"'"'"'"'"'"'\s*=>\s*'"'"'"'"'"'"'\K[^'"'"'"'"'"'"']+'"'"'"'"'"'"' /var/www/html/local/config/config.inc.php 2>/dev/null || echo "piwigo")\n    DB_PASS=$(grep -oP '"'"'"'"'"'"'db_password'"'"'"'"'"'"'\s*=>\s*'"'"'"'"'"'"'\K[^'"'"'"'"'"'"']+'"'"'"'"'"'"' /var/www/html/local/config/config.inc.php 2>/dev/null || echo "")\n    DB_NAME=$(grep -oP '"'"'"'"'"'"'db_name'"'"'"'"'"'"'\s*=>\s*'"'"'"'"'"'"'\K[^'"'"'"'"'"'"']+'"'"'"'"'"'"' /var/www/html/local/config/config.inc.php 2>/dev/null || echo "piwigo")\n    \n    for i in {1..30}; do\n        if mariadb -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASS" -e "SELECT 1" "$DB_NAME" >/dev/null 2>&1; then\n            echo "Database connection successful"\n            break\n        fi\n        echo "Waiting for database... ($i/30)"\n        sleep 2\n    done\nfi\n' > /etc/cont-init.d/99-custom-startup && \
    chmod +x /etc/cont-init.d/99-custom-startup

# Use base image's entrypoint (docker-php-entrypoint)
# The base image already handles permissions and Apache startup