FROM php:8.2-apache-buster
WORKDIR /var/www/html/
COPY . /var/www/html
#COPY .  /var/www/html/
# Surpresses debconf complaints of trying to install apt packages interactively
# https://github.com/moby/moby/issues/4032#issuecomment-192327844

ARG DEBIAN_FRONTEND=noninteractive

# Update
RUN apt-get -y update --fix-missing && \
    apt-get upgrade -y && \
    apt-get --no-install-recommends install -y apt-utils && \
    rm -rf /var/lib/apt/lists/*


# Install useful tools and install important libaries
# Update the package repository and install necessary packages
RUN apt-get update && \
    apt-get -y --no-install-recommends install \
        nano wget dialog libsqlite3-dev libsqlite3-0 && \
    apt-get -y --no-install-recommends install \
        default-mysql-client zlib1g-dev libzip-dev libicu-dev && \
    apt-get -y --no-install-recommends install --fix-missing \
        apt-utils build-essential git curl libonig-dev && \
    apt-get install -y iputils-ping && \
    apt-get -y --no-install-recommends install --fix-missing \
        libcurl4 libcurl4-openssl-dev zip openssl && \
    rm -rf /var/lib/apt/lists/*

# Install Redis extension using pecl and enable it
RUN pecl install redis-5.3.3 && \
    docker-php-ext-enable redis

# Install imagick
RUN apt-get update && \
    apt-get -y --no-install-recommends install --fix-missing libmagickwand-dev && \
    rm -rf /var/lib/apt/lists/*


# install php-imap
RUN apt update && apt install -y libc-client-dev libkrb5-dev && rm -r /var/lib/apt/
RUN docker-php-ext-configure imap --with-kerberos --with-imap-ssl && docker-php-ext-install imap

#install node
# Allow Composer to run as superuser (though not recommended for production)
ENV COMPOSER_ALLOW_SUPERUSER=1

# Update the package repository and install curl
RUN apt-get update && \
    apt-get -y install curl

# Add the Node.js official repository and install Node.js
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && \
    apt-get -y install nodejs

# Download and verify Composer installer
RUN curl -sS https://getcomposer.org/installer -o /tmp/composer-setup.php && \
    curl -sS https://composer.github.io/installer.sig -o /tmp/composer-setup.sig && \
    echo "$(cat /tmp/composer-setup.sig) /tmp/composer-setup.php" | sha384sum -c - && \
    php /tmp/composer-setup.php --install-dir=/usr/local/bin --filename=composer && \
    rm -f /tmp/composer-setup.php /tmp/composer-setup.sig

# Add Composer global bin directory to PATH (if necessary)
#ENV PATH="/var/www/.composer/vendor/bin:${PATH}"
RUN chown -R www-data:www-data /var/www && \
    chmod -R 775 /var/www

ARG IMAGICK_COMMIT="661405abe21d12003207bc8eb0963fafc2c02ee4"

RUN cd /usr/local/src && \
    git clone https://github.com/Imagick/imagick && \
    cd imagick && \
    git checkout ${IMAGICK_COMMIT} && \
    phpize && \
    ./configure && \
    make && \
    make install && \
    cd .. && \
    rm -rf imagick && \
    docker-php-ext-enable imagick

# Other PHP8 Extensions

RUN docker-php-ext-install pdo_mysql && \
    docker-php-ext-install pdo_sqlite && \
    docker-php-ext-install mysqli && \
    docker-php-ext-install curl && \
    docker-php-ext-install zip && \
    docker-php-ext-install -j$(nproc) intl && \
    docker-php-ext-install mbstring && \
    docker-php-ext-install gettext && \
    docker-php-ext-install calendar && \
    docker-php-ext-install exif


# Install Freetype
RUN apt-get -y update && \
    apt-get --no-install-recommends install -y libfreetype6-dev \
libjpeg62-turbo-dev \
libpng-dev && \
    rm -rf /var/lib/apt/lists/* && \
    docker-php-ext-configure gd --enable-gd --with-freetype --with-jpeg && \
    docker-php-ext-install gd
# Switch to a non-root user
#RUN a2enmod ssl && a2enmod rewrite && a2enmod proxy && a2enmod proxy_http

# Enable apache modules
#RUN a2enmod rewrite headers

# Cleanup

#USER www-data

# Change ownership of the application directory
# Note: This should generally be done prior to switching to non-root user if possible
#RUN chown -R www-data:www-data /var/www/html

#RUN mkdir /var/www/html/bootstrap/cache

RUN composer install --ignore-platform-reqs
#RUN php artisan migrate
RUN mkdir -p /etc/apache2/ssl

# Enable SSL support
RUN a2enmod ssl && a2enmod rewrite && a2enmod proxy && a2enmod proxy_http

# Enable apache modules
RUN a2enmod rewrite headers

# Cleanup
RUN rm -rf /usr/src/*


# Workarround until imagick is available in pecl with php8 support
# Imagick Commit to install
# https://github.com/Imagick/imagick


# Insure an SSL directory exists
#RUN mkdir -p /etc/apache2/ssl

# Enable SSL support

#RUN php artisan serve

