FROM lscr.io/linuxserver/baseimage-kasmvnc:ubuntunoble

# Set REAPER version (can be updated via build-arg)
ARG REAPER_VERSION="773"

# Title of the KasmVNC web tab
ENV TITLE="Reaper-Web"

# Install dependencies for REAPER, Audio, and UI
RUN apt-get update && apt-get install -y \
    libgl1 \
    libgl1-mesa-dri \
    libasound2t64 \
    libgdk-pixbuf2.0-0 \
    libgtk-3-0 \
    libxml2 \
    libcurl4 \
    libxcb-cursor0 \
    libxcb-keysyms1 \
    libxcb-icccm4 \
    libxcb-render-util0 \
    libxcb-xinerama0 \
    pipewire-jack \
    pulseaudio-utils \
    pavucontrol \
    wget \
    xz-utils \
    python3-xdg \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Download and install REAPER
RUN wget https://www.reaper.fm/files/7.x/reaper${REAPER_VERSION}_linux_x86_64.tar.xz -O /tmp/reaper.tar.xz && \
    tar -xvf /tmp/reaper.tar.xz -C /tmp && \
    /tmp/reaper_linux_x86_64/install-reaper.sh \
        --install /opt \
        --integrate-user-desktop && \
    rm -rf /tmp/reaper*

# Environment variables for low-latency PipeWire/JACK
ENV PIPEWIRE_LATENCY="128/48000"
ENV LD_LIBRARY_PATH="/usr/lib/x86_64-linux-gnu/pipewire-0.3/jack:$LD_LIBRARY_PATH"
ENV PULSE_RUNTIME_PATH="/config/.pulse"

# Set up autostart for REAPER and high-quality KasmVNC wrapper
RUN mkdir -p /defaults && \
    sed -i 's/show_control_bar=true/show_control_bar=true\&preset=lossless/g' /kclient/public/index.html && \
    sed -i 's/http.listen(6900);/http.listen(6900, "0.0.0.0");/g' /kclient/index.js && \
    sed -i 's/<script src="public\/js\/pcm-player.js"><\/script>//g' /kclient/public/index.html && \
    sed -i 's|/defaults/mic.sock|/config/.pulse/mic.sock|g' /etc/s6-overlay/s6-rc.d/svc-kclient/run /kclient/index.js && \
    sed -i 's|/defaults/pid|/config/.pulse/pid|g' /etc/s6-overlay/s6-rc.d/svc-kclient/run && \
    sed -i "s/<\/body>/<script>document.addEventListener('click', function initAudio() { if (!('audioCtx' in player) || !player.audioCtx) { audio(); mic(); } document.removeEventListener('click', initAudio); }, { once: true });<\/script><\/body>/g" /kclient/public/index.html && \
    sed -i "s/(duration > .05)/(duration > .15)/g" /kclient/public/js/kclient.js && \
    sed -i "s/aio = socketIO(http, {path: SUBFOLDER + 'audio\/socket.io'});/aio = socketIO(http, {path: SUBFOLDER + 'audio\/socket.io', maxHttpBufferSize: 200000000, perMessageDeflate: false});/g" /kclient/index.js && \
    sed -i "s/load-module module-udev-detect/load-module module-udev-detect tsched=0/g" /etc/pulse/default.pa && \
    sed -i 's/<\/applications>/  <application class="*">\n    <maximized>true<\/maximized>\n  <\/application>\n<\/applications>/g' /etc/xdg/openbox/rc.xml
COPY autostart.sh /defaults/autostart
RUN chmod +x /defaults/autostart

# Bake in optimized REAPER configuration
COPY config /custom-defaults
COPY seed-config.sh /custom-cont-init.d/99-seed-config.sh
RUN chmod +x /custom-cont-init.d/99-seed-config.sh

# Expose ports for KasmVNC (HTTP/HTTPS)
EXPOSE 3000 3001

# REAPER config is stored in /config/.config/REAPER by default in this image
VOLUME /config
