# PHP with Apache, tag kept current by Renovate (Phorge requires PHP 7.2.25+)
FROM php:8.5-apache

WORKDIR /app

ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/

RUN install-php-extensions mbstring iconv mysqli curl pcntl gd zip apcu opcache

RUN apt update && apt install -y git default-mysql-client python3-pygments

# Enable Apache modules
RUN a2enmod rewrite

# Clone Phorge and Arcanist from GitHub mirrors (per guide)
RUN git clone https://github.com/phorgeit/phorge.git
RUN git clone https://github.com/phorgeit/arcanist.git
RUN git config --system --add safe.directory /app/phorge
RUN git config --system --add safe.directory /app/arcanist

# Set up Apache and PHP for Phorge
COPY phorge.conf /etc/apache2/conf-enabled/phorge.conf
COPY phorge.ini /usr/local/etc/php/conf.d/phorge.ini

# Copy and set up entrypoint script
COPY entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

# Copy initialization scripts (main + lib modules)
COPY init-phorge.sh /usr/local/bin/init-phorge.sh
COPY lib/ /usr/local/bin/lib/
RUN chmod +x /usr/local/bin/* /usr/local/bin/lib/*

# Create configuration directory and storage directories
RUN mkdir -p /app/phorge/conf/local /app/repo /app/files && \
    chown -R www-data:www-data /app/phorge /app/repo /app/files

# Expose port 80
EXPOSE 80

# Entry point
ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
