FROM rust:1.70.0 as builder
WORKDIR /siren
ADD src ./src/
ADD Cargo.toml ./
RUN apt-get update && apt-get install -y cmake && \
  cargo build --release --bin siren

FROM debian:bullseye-slim as packages
WORKDIR /packages
RUN apt-get update && apt-get install -y curl tar xz-utils && \
  curl -L https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp_linux > yt-dlp && \
  chmod +x yt-dlp && \
  curl -L https://github.com/yt-dlp/FFmpeg-Builds/releases/download/latest/ffmpeg-master-latest-linux64-gpl.tar.xz > ffmpeg.tar.xz && \
  tar -xJf ffmpeg.tar.xz --wildcards */bin/ffmpeg --transform='s/^.*\///' && rm ffmpeg.tar.xz

# FROM debian:bullseye-slim as libraries
# WORKDIR /libraries
# RUN apt-get update && apt-get install -y unzip && \
#   curl -L https://download.pytorch.org/libtorch/cu117/libtorch-cxx11-abi-shared-with-deps-2.0.1%2Bcu117.zip > libtorch.zip && \
#   unzip libtorch.zip && rm libtorch.zip

FROM debian:bullseye-slim as runtime
WORKDIR /siren
RUN apt-get update && apt-get install -y libopus-dev libpq5 libpq-dev && apt-get auto-remove -y
COPY --from=builder /siren/target/release/siren /usr/local/bin/siren
COPY --from=packages /packages /usr/bin
# COPY --from=libraries /libraries /usr/lib

# ARG LIBTORCH=/usr/lib/libtorch
# ARG LD_LIBRARY_PATH=${LIBTORCH}/lib:${LD_LIBRARY_PATH}

# ADD migrations ./
CMD ["siren"]
