FROM php:8.2-fpm-bullseye

WORKDIR /var/www

RUN apt-get update && apt-get install -y \
      apt-utils \
      libpq-dev \
      libpng-dev \
      libzip-dev \
      zip unzip \
      git \
      libmagickwand-dev --no-install-recommends && \
      docker-php-ext-install bcmath gd zip && \
      docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql && \
      docker-php-ext-install pdo pdo_pgsql pgsql && \
      pecl install imagick && \
      docker-php-ext-enable imagick && \
      apt-get clean && \
      rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY _docker/app/php.ini /usr/local/etc/php/conf.d/php.ini
COPY _docker/app/www.conf /usr/local/etc/php-fpm.d/www.conf

COPY app            /var/www/app
COPY bootstrap      /var/www/bootstrap
COPY config         /var/www/config
COPY database       /var/www/database
COPY public         /var/www/public
COPY resources      /var/www/resources
COPY routes         /var/www/routes
COPY artisan        /var/www/artisan
COPY package.json   /var/www/package.json
COPY webpack.mix.js /var/www/webpack.mix.js

COPY composer.*  ./

# Install composer
ENV COMPOSER_ALLOW_SUPERUSER=1
RUN curl -sS https://getcomposer.org/installer | php -- \
    --filename=composer \
    --install-dir=/usr/local/bin

# alias
RUN echo "alias a='artisan'" >> /root/.bashrc

RUN curl -sL https://deb.nodesource.com/setup_18.x | bash -
RUN apt-get install -y nodejs

RUN composer install \
      --no-interaction \
      --no-plugins \
      --no-scripts \
      --no-autoloader \
      --prefer-dist

RUN mkdir -p /var/www/storage/app/public \
    /var/www/storage/framework/cache \
    /var/www/storage/framework/sessions \
    /var/www/storage/framework/views \
    /var/www/storage/logs && \
    composer dump-autoload --no-scripts --optimize --classmap-authoritative && \
    chown -R root:www-data /var/www && \
    chmod 755 -R /var/www && \
    chmod -R 775 /var/www/storage /var/www/bootstrap/cache

COPY _docker/app/entrypoint.sh /usr/local/bin/entrypoint.sh
RUN chmod +x /usr/local/bin/entrypoint.sh

WORKDIR /var/www

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