FROM 594765360358.dkr.ecr.ca-central-1.amazonaws.com/tbi-php-85:latest

RUN curl -LO https://github.com/DataDog/dd-trace-php/releases/latest/download/datadog-setup.php && \
    php datadog-setup.php --php-bin=all
    
# Install Node.js, Git, and Zip
# tbi-php-85 is Debian 13. Keep its own apt indexes.
RUN curl -fsSL https://nodejs.org/dist/v18.20.8/node-v18.20.8-linux-x64.tar.xz | tar -xJ -C /usr/local --strip-components=1 && \
    apt-get update && \
    apt-get install -y git zip && \
    rm -rf /var/lib/apt/lists/*

# Create directories      
RUN mkdir /data && \
    mkdir /data/websites

# Copy project files
COPY . /data/websites/

# Install Composer
RUN curl -L https://github.com/composer/composer/releases/download/2.3.7/composer.phar > /usr/local/bin/composer && \
    chmod +x /usr/local/bin/composer

# Set working directory
WORKDIR /data/websites

# Define the user
USER root

# Change ownership
RUN chmod 777 -R storage bootstrap/cache public && \
    mkdir /root/.ssh/ && \
    mv id_jenkins /root/.ssh/id_rsa && \
    chmod 400 /root/.ssh/id_rsa

# Install dependencies and build assets
RUN composer install -n && \
    npm install --force && \
    npm update redlabel-web-resources --force && \
    npm run build

# Change working directory
WORKDIR /data

# Change ownership again (if needed)
RUN chown -R www-data:www-data /data/websites
