FROM rustdesk/rustdesk-server-s6:latest AS server

FROM alpine

ARG BUILDARCH
WORKDIR /app
RUN apk add --no-cache tzdata
COPY ./${BUILDARCH}/release /app/

COPY --from=server /init /init
COPY --from=server /etc/s6-overlay /etc/s6-overlay
COPY --from=server /package /package
COPY --from=server /usr/bin/healthcheck.sh /usr/bin/healthcheck.sh
COPY --from=server /usr/bin/hbbr /usr/bin/hbbr
COPY --from=server /usr/bin/hbbs /usr/bin/hbbs
COPY --from=server /usr/bin/rustdesk-utils /usr/bin/rustdesk-utils
COPY --from=server /command /command

RUN \
  mkdir -p /etc/s6-overlay/s6-rc.d/api && \
  echo -e "key-secret\nhbbs" > /etc/s6-overlay/s6-rc.d/api/dependencies && \
  echo "longrun" > /etc/s6-overlay/s6-rc.d/api/type && \
  echo "#!/command/with-contenv sh" > /etc/s6-overlay/s6-rc.d/api/run && \
  echo "cd /app" >> /etc/s6-overlay/s6-rc.d/api/run && \
  echo "./apimain" >> /etc/s6-overlay/s6-rc.d/api/run && \
  touch /etc/s6-overlay/s6-rc.d/user/contents.d/api && \
  echo "/package/admin/s6/command/s6-svstat /run/s6-rc/servicedirs/api || exit 1" >> /usr/bin/healthcheck.sh && \
  ln -s /run /var/run

ENV RELAY=relay.example.com
ENV ENCRYPTED_ONLY=0

VOLUME /data
VOLUME /app/data

EXPOSE 21114 21115 21116 21116/udp 21117 21118 21119

ENTRYPOINT ["/init"]
