FROM ubuntu:latest AS base

ENV COMPOSER_ALLOW_SUPERUSER=1

ENV DEBIAN_FRONTEND noninteractive

# Install dependencies
RUN apt update
RUN apt install -y software-properties-common
RUN add-apt-repository -y ppa:ondrej/php
RUN apt update
RUN apt install -y php8.2\
    php8.2-cli\
    php8.2-common\
    php8.2-fpm\
    php8.2-mysql\
    php8.2-zip\
    php8.2-gd\
    php8.2-mbstring\
    php8.2-curl\
    php8.2-xml\
    php8.2-bcmath\
    php8.2-pdo\
    php8.2-intl

# Install php-fpm
RUN apt install -y php8.2-fpm php8.2-cli php-mbstring

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

# Install nodejs
RUN apt install -y ca-certificates gnupg
RUN mkdir -p /etc/apt/keyrings
RUN curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
ENV NODE_MAJOR 20
RUN echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
RUN apt update
RUN apt install -y nodejs

# Install nginx
RUN apt install -y nginx
RUN echo "\
    server {\n\
        listen 80;\n\
        listen [::]:80;\n\
        server_name  localhost;\n\
        root /var/www/s/public;\n\
        return 301 https://platform-dev.k8spaynance.com;\n\
        access_log /var/log/nginx/access.log;\n\
        error_log /var/log/nginx/error.log;\n\
        }\n\
        server {\n\
        listen [::]:443 ssl;\n\
        listen 443 ssl;\n\
        server_name localhost;\n\
        root /var/www/s/public;\n\
        access_log /var/log/nginx/access.log;\n\
        error_log /var/log/nginx/error.log;\n\
        ssl_certificate /etc/nginx/k8spaynance.crt;\n\
        ssl_certificate_key /etc/nginx/k8spaynance.key;\n\
        ssl_protocols       TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;\n\
        ssl_ciphers         HIGH:!aNULL:!MD5;\n\
        add_header X-Frame-Options \"SAMEORIGIN\";\n\
        add_header X-Content-Type-Options \"nosniff\";\n\
        index index.php;\n\
        charset utf-8;\n\
        location / {\n\
#            root /var/www/s/public;\n\
            try_files \$uri \$uri/ /index.php?\$query_string;\n\
        }\n\
        location = /favicon.ico { access_log off; log_not_found off; }\n\
        location = /robots.txt  { access_log off; log_not_found off; }\n\
        error_page 404 /index.php;\n\
        location ~ \.php$ {\n\
            fastcgi_pass unix:/run/php/php8.2-fpm.sock;\n\
            fastcgi_param SCRIPT_FILENAME \$realpath_root\$fastcgi_script_name;\n\
            include fastcgi_params;\n\
        }\n\
        location ~ /\.(?!well-known).* {\n\
            deny all;\n\
        }\n\
    }\n" > /etc/nginx/sites-available/platform-dev.k8spaynance.com

RUN ln -s /etc/nginx/sites-available/platform-dev.k8spaynance.com /etc/nginx/sites-enabled/platform-dev.k8spaynance.com
COPY certs/* /etc/nginx/
RUN rm /etc/nginx/sites-enabled/default

RUN echo "\
    #!/bin/sh\n\
    echo \"Starting services...\"\n\
    service php8.2-fpm start\n\
    nginx -g \"daemon off;\" &\n\
    echo \"Ready.\"\n\
    tail -s 1 /var/log/nginx/*.log -f\n\
    " > /start.sh

COPY . /var/www/s
WORKDIR /var/www/s

RUN chown -R www-data:www-data /var/www/s

RUN composer config allow-plugins.composer/installers true

RUN composer update
RUN composer install

EXPOSE 80 443

CMD ["sh", "/start.sh"]