FROM php:8.1-cli

# --- BEGIN AUTO MIRRORS ---
RUN if [ -d /etc/apt ]; then \
      sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true; \
      sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true; \
      sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true; \
      sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list /etc/apt/sources.list.d/*.list 2>/dev/null || true; \
      if [ -d /etc/apt/sources.list.d ]; then \
        sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list.d/*.sources 2>/dev/null || true; \
        sed -i 's/security.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list.d/*.sources 2>/dev/null || true; \
        sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/*.sources 2>/dev/null || true; \
        sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/*.sources 2>/dev/null || true; \
      fi; \
    fi

# --- php mirrors ---

RUN command -v php >/dev/null 2>&1 && \
    curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
    composer config -g repos.packagist composer https://mirrors.aliyun.com/composer/ || true
# --- END AUTO MIRRORS ---

ENV COMPOSER_ALLOW_SUPERUSER=1

RUN apt-get update && apt-get install -y --no-install-recommends \
    curl \
    git \
    libonig-dev \
    libcurl4-openssl-dev \
    libfreetype6-dev \
    libjpeg62-turbo-dev \
    libpng-dev \
    libxml2-dev \
    unzip \
    zip \
    && rm -rf /var/lib/apt/lists/*


# --- BEGIN DOCKER_ENV_FETCH ---
ARG SOURCE_URL
ARG SOURCE_STRIP_COMPONENTS=1
ARG FETCH_SOURCE=true
ARG BUILD_MODE=app
COPY docker-env/common.sh docker-env/fetch_source.sh /docker-env/
RUN chmod +x /docker-env/*.sh && \
    DEST_DIR="/app" STRIP_COMPONENTS="${SOURCE_STRIP_COMPONENTS}" \
    bash /docker-env/fetch_source.sh
# --- END DOCKER_ENV_FETCH ---

RUN docker-php-ext-configure gd --with-freetype --with-jpeg

RUN docker-php-ext-install -j$(nproc) \
    ctype \
    curl \
    dom \
    gd \
    iconv \
    mbstring \
    simplexml

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

WORKDIR /app

RUN composer install --no-dev --optimize-autoloader --no-interaction
