# Use latest official Ubuntu image
FROM ubuntu:latest

# Set timezone
ENV TZ=Europe/Berlin

# Configure timezone data during build
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# Avoid interactive prompts during apt install
ARG DEBIAN_FRONTEND=noninteractive

# Install Apache, PHP and required extensions
RUN apt-get update && apt-get install -y \
  apache2 \
  apache2-utils \
  ca-certificates \
  php \
  libapache2-mod-php \
  php-curl \
  php-dom \
  php-gd \
  php-intl \
  php-json \
  php-mbstring \
  php-xml \
  php-zip && \
  apt-get clean && rm -rf /var/lib/apt/lists/*

# Ensure shared group ownership for collaborative editing
RUN groupadd -g 1111 kirbygroup && usermod -aG kirbygroup www-data

# Provide Apache vhost configuration and entrypoint
COPY default.conf /etc/apache2/sites-available/000-default.conf
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

# Remove default Apache placeholder page
RUN rm -rf /var/www/html/*

# Copy project into the image
COPY --chown=www-data:kirbygroup . /var/www/html

# Enable required Apache modules
RUN a2enmod headers rewrite

# Ensure collaborative permissions at runtime
RUN chmod -R g+rw /var/www/html && find /var/www/html -type d -exec chmod g+xs {} \;

# Expose web port
EXPOSE 80

# Allow UID remapping before Apache starts
ENTRYPOINT ["/entrypoint.sh"]

# Start Apache in foreground
CMD ["/usr/sbin/apache2ctl", "-DFOREGROUND"]
