FROM openeuler/openeuler:24.03-lts

ARG DEPLOY_VERSION=42
ENV DEPLOY_VERSION=${DEPLOY_VERSION}

ENV PHP73_VERSION=7.3.33 \
    PHP73_PREFIX=/usr/local/php7.3 \
    PHP74_VERSION=7.4.33 \
    PHP74_PREFIX=/usr/local/php7.4 \
    PATH=/usr/local/php7.3/bin:/usr/local/php7.3/sbin:/usr/local/php7.4/bin:/usr/local/php7.4/sbin:$PATH \
    PKG_CONFIG_PATH=/usr/lib64/compat-openssl11/pkgconfig:/usr/lib64/pkgconfig:/usr/share/pkgconfig

# Base system setup, runtime dependencies, and build tooling
RUN set -eux; \
    for repo in /etc/yum.repos.d/*.repo; do \
        [ -f "$repo" ] || continue; \
        sed -i 's|http://repo\.openeuler\.org|https://repo.openeuler.org|g' "$repo"; \
    done; \
    dnf clean all; \
    dnf -y makecache; \
    dnf -y install --setopt=install_weak_deps=False --enablerepo=everything --enablerepo=EPOL \
        nginx \
        wget \
        curl \
        tar \
        gzip \
        which \
        ca-certificates \
        tzdata \
        shadow-utils \
        patch \
        make \
        autoconf \
        automake \
        libtool \
        bison \
        re2c \
        pkgconfig \
        gcc \
        gcc-c++ \
        gettext-devel \
        gmp-devel \
        krb5-devel \
        libxml2-devel \
        libcurl-devel \
        libzip-devel \
        compat-openssl11-devel \
        compat-openssl11-libs \
        bzip2-devel \
        libjpeg-turbo-devel \
        libpng-devel \
        freetype-devel \
        libxslt-devel \
        readline-devel \
        sqlite-devel \
        oniguruma-devel \
        openldap-devel \
        libicu-devel \
        libwebp-devel \
        mosquitto-devel; \
    dnf clean all; \
    rm -rf /var/cache/dnf/*

COPY patches/ /tmp/patches/

# Locale & timezone
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone

# Application user
RUN groupadd -r www && useradd -r -g www -d /home/www -s /sbin/nologin www && mkdir -p /home/www

# Build and install PHP 7.3 / 7.4
RUN set -eux; \
    fetch() { \
        url="$1"; \
        output="$2"; \
        rm -f "${output}"; \
        curl -fL --retry 5 --retry-delay 3 --retry-all-errors -C - "${url}" -o "${output}"; \
    }; \
    build_php() { \
        version="$1"; \
        prefix="$2"; \
        patch_file="${3:-}"; \
        image_opts=""; \
        extra_opts=""; \
        zip_opts=""; \
        intl_opts="--enable-intl"; \
        mkdir -p /usr/local/src; \
        cd /usr/local/src; \
        fetch "https://www.php.net/distributions/php-${version}.tar.gz" "php-${version}.tar.gz"; \
        tar -xzf "php-${version}.tar.gz"; \
        cd "php-${version}"; \
        if [ -n "${patch_file}" ]; then \
            patch -p1 < "${patch_file}"; \
        fi; \
        case "${version}" in \
            7.3.*) \
                image_opts="--with-jpeg-dir=/usr --with-png-dir=/usr --with-freetype-dir=/usr --with-webp-dir=/usr"; \
                extra_opts="--with-pdo-mysql --with-mysqli"; \
                zip_opts="--enable-zip --with-libzip"; \
                ;; \
            *) \
                image_opts="--with-jpeg=/usr --with-png=/usr --with-freetype=/usr --with-webp=/usr"; \
                extra_opts="--with-pdo-mysql --with-mysqli"; \
                zip_opts="--enable-zip --with-libzip"; \
                ;; \
        esac; \
        export CPPFLAGS="${CPPFLAGS:-} -I/usr/include/openssl"; \
        export LDFLAGS="-L/usr/lib64/compat-openssl11 ${LDFLAGS:-}"; \
        ./configure \
            --prefix="${prefix}" \
            --with-config_sxjmai.sql-file-path="${prefix}/etc" \
            --with-config_sxjmai.sql-file-scan-dir="${prefix}/etc/php.d" \
            --enable-fpm \
            --with-fpm-user=www \
            --with-fpm-group=www \
            --enable-cli \
            --enable-mbstring \
            --enable-bcmath \
            --enable-pcntl \
            --enable-sockets \
            --enable-shmop \
            --enable-sysvmsg \
            --enable-sysvsem \
            --enable-sysvshm \
            --enable-opcache \
            --enable-soap \
            --enable-calendar \
            --with-zlib \
            --with-bz2 \
            --with-curl \
            --with-gmp \
            --with-openssl \
            --with-gettext \
            --with-kerberos \
            --with-mhash \
            --with-pear \
            --with-xmlrpc \
            --with-xsl \
            --with-readline \
            --with-libdir=lib64 \
            --with-ldap \
            --with-ldap-sasl \
            ${extra_opts} \
            ${zip_opts} \
            ${intl_opts} \
            ${image_opts}; \
        make -j"$(nproc)"; \
        make install; \
        cp php.ini-production "${prefix}/etc/php.ini"; \
        mkdir -p "${prefix}/etc/php.d" "${prefix}/etc/php-fpm.d"; \
        cp sapi/fpm/php-fpm.conf "${prefix}/etc/php-fpm.conf"; \
        cp sapi/fpm/www.conf "${prefix}/etc/php-fpm.d/www.conf"; \
        cd /; \
    }; \
    install_extensions() { \
        prefix="$1"; \
        version="$2"; \
        phpize_bin="${prefix}/bin/phpize"; \
        php_config_bin="${prefix}/bin/php-config"; \
        fetch "https://pecl.php.net/get/Mosquitto-0.4.0.tgz" "/tmp/mosquitto-${version}.tgz"; \
        tar -xzf "/tmp/mosquitto-${version}.tgz" -C /tmp; \
        cd /tmp/Mosquitto-0.4.0; \
        "${phpize_bin}"; \
        ./configure --with-php-config_sxjmai.sql="${php_config_bin}"; \
        make -j"$(nproc)"; \
        make install; \
        fetch "https://pecl.php.net/get/redis-5.3.5.tgz" "/tmp/redis-${version}.tgz"; \
        tar -xzf "/tmp/redis-${version}.tgz" -C /tmp; \
        cd /tmp/redis-5.3.5; \
        "${phpize_bin}"; \
        ./configure --with-php-config_sxjmai.sql="${php_config_bin}" --enable-redis; \
        make -j"$(nproc)"; \
        make install; \
        cd /; \
        rm -rf /tmp/Mosquitto-0.4.0 "/tmp/mosquitto-${version}.tgz" /tmp/redis-5.3.5 "/tmp/redis-${version}.tgz" /tmp/pear; \
        echo "; Extensions enabled via ${prefix}/etc/php.d/zhct.ini" > "${prefix}/etc/php.d/00-readme.ini"; \
    }; \
    mkdir -p /usr/local/src; \
    build_php "${PHP73_VERSION}" "${PHP73_PREFIX}" "/tmp/patches/php7.3-icu74.patch"; \
    build_php "${PHP74_VERSION}" "${PHP74_PREFIX}"; \
    install_extensions "${PHP73_PREFIX}" "${PHP73_VERSION}"; \
    install_extensions "${PHP74_PREFIX}" "${PHP74_VERSION}"; \
    ln -s "${PHP73_PREFIX}" /usr/local/php; \
    rm -rf /usr/local/src/php-* /tmp/patches

# Move nginx configuration under /usr/local and prepare directories
RUN set -eux; \
    mkdir -p /usr/local/nginx; \
    mv /etc/nginx /usr/local/nginx/conf; \
    ln -s /usr/local/nginx/conf /etc/nginx; \
    mkdir -p /usr/local/nginx/logs /workspace/wwwlogs /workspace/zhct; \
    chown -R www:www /workspace/wwwlogs

# Copy configuration assets
COPY config/nginx/conf/ /usr/local/nginx/conf/
COPY config/php/php-fpm.conf /usr/local/php7.3/etc/php-fpm.conf
COPY config/php/php-fpm.d/ /usr/local/php7.3/etc/php-fpm.d/
COPY config/php/conf.d/ /usr/local/php7.3/etc/php.d/
COPY config/php74/php-fpm.conf /usr/local/php7.4/etc/php-fpm.conf
COPY config/php74/php-fpm.d/ /usr/local/php7.4/etc/php-fpm.d/
COPY config/php74/conf.d/ /usr/local/php7.4/etc/php.d/
COPY scripts/entrypoint.sh /usr/local/bin/entrypoint.sh

# Permissions
RUN set -eux; \
    chmod +x /usr/local/bin/entrypoint.sh; \
    chmod +x /usr/local/php7.3/sbin/php-fpm; \
    chmod +x /usr/local/php7.4/sbin/php-fpm; \
    mkdir -p /var/run; \
    ln -sf /usr/sbin/nginx /usr/local/sbin/nginx

RUN set -eux; \
    mkdir -p \
        /workspace/zhct/zhctproject/store \
        /workspace/zhct/zhctproject/ai_api \
        /workspace/zhct/zhctproject/ai_store \
        /workspace/zhct/zhctproject/ai_app \
        /workspace/zhct/zhctlogs/log/zhct/cron \
        /workspace/zhct/zhctlogs/log/aizhct/cron; \
    chown -R www:www /workspace/zhct/zhctlogs

WORKDIR /workspace/zhct

EXPOSE 80

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
