FROM lscr.io/linuxserver/code-server:4.98.2

# For phantomjs
ENV OPENSSL_CONF="/etc/ssl"
# For downloading thumbnails
ENV SSL_CERT_DIR="/etc/ssl/certs/"
# Working directory used at both build and run times:
ENV DEFAULT_WORKSPACE="/config/ytdl-sub-configs"

###############################################################################
# YTDL-SUB INSTALL

SHELL ["/bin/bash", "-c"]
COPY root/ /
RUN mkdir -p /config && \
    apt-get -y update && \
    apt-get -y upgrade && \
    apt-get install --no-install-recommends -y \
      software-properties-common && \
    apt-get -y update && \
    apt-get -y upgrade && \
    apt-get install --no-install-recommends -y \
      vim \
      g++ \
      nano \
      unzip \
      make \
      python3-pip \
      fontconfig \
      xz-utils \
      bzip2 \
      aria2 \
      python3-venv && \
    if [[ $(uname -m) == "x86_64" ]]; then \
      curl -L -o ffmpeg.tar.gz https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz && \
      tar -xf ffmpeg.tar.gz && \
      chmod +x ffmpeg-master-latest-linux64-gpl/bin/ffmpeg && \
      chmod +x ffmpeg-master-latest-linux64-gpl/bin/ffprobe && \
      mv ffmpeg-master-latest-linux64-gpl/bin/ffmpeg /usr/bin/ffmpeg && \
      mv ffmpeg-master-latest-linux64-gpl/bin/ffprobe /usr/bin/ffprobe && \
      rm ffmpeg.tar.gz && \
      rm -rf ffmpeg-master-latest-linux64-gpl/ ; \
    else \
      curl -L -o ffmpeg.tar.gz https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linuxarm64-gpl.tar.xz && \
      tar -xf ffmpeg.tar.gz && \
      chmod +x ffmpeg-master-latest-linuxarm64-gpl/bin/ffmpeg && \
      chmod +x ffmpeg-master-latest-linuxarm64-gpl/bin/ffprobe && \
      mv ffmpeg-master-latest-linuxarm64-gpl/bin/ffmpeg /usr/bin/ffmpeg && \
      mv ffmpeg-master-latest-linuxarm64-gpl/bin/ffprobe /usr/bin/ffprobe && \
      rm ffmpeg.tar.gz && \
      rm -rf ffmpeg-master-latest-linuxarm64-gpl/ ; \
    fi && \
    # Ensure ffmpeg is installed
    ffmpeg -version && \
    # Install phantomjs if using x86_64, ensure it is properly installed
    if [[ $(uname -m) == "x86_64" ]]; then \
      echo "installing phantomjs" && \
      tar -xjvf /defaults/phantomjs-2.1.1-linux-x86_64.tar.bz2 && \
      mv phantomjs-2.1.1-linux-x86_64/bin/phantomjs /usr/bin/phantomjs && \
      rm -rf phantomjs-2.1.1-linux-x86_64 && \
      rm /defaults/phantomjs-2.1.1-linux-x86_64.tar.bz2 && \
      echo "Phantom JS version:" && \
      phantomjs --version ; \
    fi && \
    # Install Deno, required for YouTube downloads
    curl -fsSL https://deno.land/install.sh | DENO_INSTALL=/usr/local sh -s -- -y --no-modify-path && \
    deno --help && \
    # Configure pip globally
    echo -e "[global]\nbreak-system-packages = true\nroot-user-action = ignore\nno-cache-dir = true" > /etc/pip.conf && \
    # Install ytdl-sub and yt-dlp dependencies, ensure they are installed properly
    python3 -m pip install ytdl_sub-*.whl curl-cffi yt-dlp-ejs && \
      ytdl-sub -h && \
    # Delete unneeded packages after install
    rm ytdl_sub-*.whl && \
    apt-get remove -y \
      g++ \
      make \
      xz-utils \
      bzip2 && \
    apt-get autoremove -y && \
    apt-get purge -y --auto-remove && \
    rm -rf /var/lib/apt/lists/* && \
    python3 -m pip --help

###############################################################################
# CONTAINER CONFIGS

ENV EDITOR="nano" \
HOME="/config" \
DOCKER_MODS=linuxserver/mods:universal-stdout-logs|linuxserver/mods:universal-cron \
CRON_SCRIPT="${DEFAULT_WORKSPACE}/cron" \
CRON_WRAPPER_SCRIPT="/config/.cron_wrapper" \
LOGS_TO_STDOUT=/config/.cron.log \
LSIO_FIRST_PARTY=false

VOLUME /config
WORKDIR "${DEFAULT_WORKSPACE}"
