#!/bin/bash

set -e

. ./venv/bin/activate

apt-get update
apt-get install -y --no-install-recommends \
    curl \
    build-essential \
    git \
    autoconf \
    autoconf-archive \
    automake \
    libtool \
    pkg-config \
    nasm \
    cmake

cd /tmp

curl -L https://downloads.xiph.org/releases/opus/opus-1.4.tar.gz \
  | tar -xz
cd opus-1.4
CFLAGS=-O2 ./configure \
  --prefix=/opt/venv \
  --enable-shared \
  --disable-static \
  --disable-doc \
  --disable-extra-programs
make -j install
cd ..
rm -rf opus-1.4

curl -L https://sourceforge.net/projects/lame/files/lame/3.100/lame-3.100.tar.gz \
  | tar -xz
cd lame-3.100
CFLAGS=-O2 ./configure --prefix=/opt/venv --enable-shared --disable-static --enable-nasm
make -j install
cd ..
rm -rf lame-3.100

curl -L https://sourceforge.net/projects/soxr/files/soxr-0.1.3-Source.tar.xz \
  | tar -xJ
cd soxr-0.1.3-Source
mkdir build
cd build
cmake -Wno-dev -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/opt/venv -DWITH_OPENMP:BOOL=OFF ..
make -j install
cd ../..
rm -rf soxr-0.1.3-Source

git clone --depth=1 https://git.ffmpeg.org/ffmpeg.git ffmpeg
cd ffmpeg
PKG_CONFIG_PATH=/opt/venv/lib/pkgconfig ./configure --prefix=/opt/venv \
  --enable-nonfree \
  --enable-shared \
  --disable-static \
  --disable-doc \
  --disable-ffplay \
  --enable-ffprobe \
  --disable-htmlpages \
  --disable-manpages \
  --disable-podpages \
  --disable-txtpages \
  --enable-libmp3lame \
  --enable-libopus \
  --enable-libsoxr \
  --extra-ldflags=-L/opt/venv/lib \
  --extra-cflags=-I/opt/venv/include
make -j install
cd ..
rm -rf ffmpeg

git clone --depth=1 https://github.com/chirlu/sox.git
cd sox
autoreconf -i
./configure --prefix=/opt/venv --enable-shared --disable-static --disable-openmp
make -j install
cd ..
rm -rf sox

pip install \
  torch==2.1.0 \
  torchaudio==2.1.0 \
  --index-url https://download.pytorch.org/whl/cu121

pip install \
  redis==4.5.5 \
  ffmpeg-python==0.2.0 \
  boto3==1.28.38

pip cache purge
