
# INSTALL PHP COMPOSER DEPENDENCIES
FROM composer:2.7.9 AS composer-deps

WORKDIR /

# If your composer.json file defines scripts that run during dependency installation and
# reference your application source files, uncomment the line below to copy all the files
# into this layer.
# COPY composer.json composer.lock ./
COPY . .

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a bind mounts to composer.json and composer.lock to avoid having to copy them
# into this layer.
# Leverage a cache mount to /tmp/cache so that subsequent builds don't have to re-download packages.
RUN --mount=type=bind,source=composer.json,target=composer.json \
--mount=type=bind,source=composer.lock,target=composer.lock \
--mount=type=cache,target=/tmp/cache \
composer install --no-interaction --prefer-dist

# ========================================

# BUILD VUE APP
FROM node:20 AS build-vue

WORKDIR /usr/app

# COPY package.json tsconfig.json tailwind.config.js vite.config.js  ./
# COPY resources/ ./resources/
COPY . .
RUN mv .env.docker .env
COPY --from=composer-deps /vendor/ ./vendor
RUN mkdir -p public/build/ && npm i && npm run build

# ========================================

# RUN
FROM php:8.3-alpine AS final

ARG APP_ENV_FILE=.env.docker

# Install system dependencies
RUN apk update && apk add --no-cache \
    git \
    curl \
    libpng-dev \
    libxml2-dev \
    libzip-dev \
    zip \
    unzip \
    supervisor \
    nginx \
    openssl \
    linux-headers \
    supervisor \
    tesseract-ocr \
    ffmpeg \
    && rm  -rf /tmp/* /var/cache/apk/*

RUN docker-php-ext-configure zip && docker-php-ext-install zip
RUN docker-php-ext-install gd pdo pdo_mysql zip

# Tesseract-OCR module downloads
# Based on https://github.com/Franky1/docker-tesseract/blob/master/Dockerfile.main
RUN wget --no-check-certificate https://github.com/tesseract-ocr/tessdata/raw/refs/heads/main/eng.traineddata -P /usr/share/tessdata \
    && wget --no-check-certificate https://github.com/tesseract-ocr/tessdata/raw/refs/heads/main/fra.traineddata -P /usr/share/tessdata

# Install latest version of the linux binary of yt-dlp into /bin/yt-dlp
# Get the file from https://github.com/yt-dlp/yt-dlp-master-builds/releases/latest/download/yt-dlp
RUN curl -L https://github.com/yt-dlp/yt-dlp-master-builds/releases/latest/download/yt-dlp -o /bin/yt-dlp \
    && chmod +x /bin/yt-dlp

# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

ARG wd=/var/www
WORKDIR ${wd}

COPY . .
COPY --from=build-vue /usr/app/public/build ./public/build
RUN mv ${APP_ENV_FILE} .env

# DUSK
# RUN php artisan dusk:install && php artisan dusk:chrome-driver && mv ./undetectedChromedriver/chromedriver-linux ./vendor/laravel/dusk/bin/chromedriver-linux
# RUN php artisan dusk:install && php artisan dusk:chrome-driver
# RUN php artisan dusk:install

RUN composer install --no-interaction --prefer-dist

# Supervisor
COPY ./docker/datbrowserQueueWorker.conf /etc/supervisor/conf.d/datbrowserQueueWorker.conf
COPY ./docker/supervisord.conf /etc/supervisord.conf
# RUN supervisorctl reread && supervisorctl update

# CRON
RUN echo "* * * * * cd ${wd} && php artisan schedule:run >> /dev/null 2>&1" > /etc/crontabs/root

# Link the storage directory to the public directory.
RUN php artisan storage:link

RUN chmod +x ./dockerEntryPoint.sh

EXPOSE 80

CMD ["./dockerEntryPoint.sh"]
