FROM cloudron/base:4.0.0@sha256:31b195ed0662bdb06a6e8a5ddbedb6f191ce92e8bee04c03fb02dd4e9d0286df

RUN mkdir -p /app/code/server /app/pkg
WORKDIR /app/code/server

# https://github.com/Chocobozzz/PeerTube/blob/develop/support/doc/dependencies.md
# ARG NODEJS_VERSION=16.13.2
# RUN mkdir -p /usr/local/node-${NODEJS_VERSION} && \
#     curl -L https://nodejs.org/dist/v${NODEJS_VERSION}/node-v${NODEJS_VERSION}-linux-x64.tar.gz | tar zxf - --strip-components 1 -C /usr/local/node-${NODEJS_VERSION}
# ENV PATH /usr/local/node-${NODEJS_VERSION}/bin:$PATH

RUN apt update && apt install -y prosody && rm -rf /var/cache/apt /var/lib/apt/lists

ARG VERSION=5.2.0

# install peertube. the chown is required for some reason for ffmpeg. it fails with EROFS without it when transcoding
RUN curl -sL https://github.com/Chocobozzz/PeerTube/releases/download/v${VERSION}/peertube-v${VERSION}.tar.xz | tar --strip-components 1 -Jxvf - && \
    yarn install --production --pure-lockfile

RUN mkdir /app/code/cli && \
    curl -L https://github.com/Chocobozzz/PeerTube/archive/v${VERSION}.tar.gz | tar -zxvf - --strip-components=1 -C /app/code/cli && \
    cd /app/code/cli && \
    NOCLIENT=1 yarn install --pure-lockfile && \
    npm run setup:cli

# the json is created and managed by web ui. it overrides the yaml file (https://github.com/lorenwest/node-config/wiki/Configuration-Files)
RUN ln -sf /app/data/production.yaml /app/code/server/config/production.yaml && \
    ln -sf /app/data/local-production.json /app/code/server/config/local-production.json

# youtube-dl
RUN curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl && chmod +x /usr/local/bin/youtube-dl

# required when installing plugins/themes that use node-gyp
RUN rm -rf /home/cloudron/.cache && ln -s /run/peertube/cache /home/cloudron/.cache && \
    rm -rf /home/cloudron/.npm && ln -s /run/peertube/npm /home/cloudron/.npm

COPY production.yaml.example start.sh /app/pkg/

ENV NODE_ENV production
ENV NODE_CONFIG_DIR /app/code/server/config

RUN printf '#!/bin/bash\n\ncd /app/code/cli && node dist/server/tools/peertube.js $@\n' > /usr/bin/peertube && chmod +x /usr/bin/peertube

CMD [ "/app/pkg/start.sh" ]

