ARG BUILD_BASE_IMAGE

FROM ${BUILD_BASE_IMAGE}

ARG BUILD_MAINTAINER
ARG BUILD_TZ
ARG BUILD_APP_NAME
ARG BUILD_LANG
ARG BUILD_LANGUAGE
ARG BUILD_LC_ALL
ARG BUILD_COUNTRY

LABEL maintainer="${BUILD_MAINTAINER}"
LABEL org.opencontainers.image.title="${BUILD_APP_NAME}"

ENV TZ=${BUILD_TZ}
ENV LANG=${BUILD_LANG}
ENV LANGUAGE=${BUILD_LANGUAGE}
ENV LC_ALL=${BUILD_LC_ALL}
ENV COUNTRY=${BUILD_COUNTRY}
ENV DEBIAN_FRONTEND=noninteractive

COPY packages.txt /tmp/packages.txt

RUN sed -i 's/^Types: deb$/Types: deb deb-src/' /etc/apt/sources.list.d/debian.sources && \
    apt-get update && apt-get install -y --no-install-recommends \
    $(cat /tmp/packages.txt) \
    firefox-esr-l10n-${BUILD_COUNTRY} \
    thunderbird-l10n-${BUILD_COUNTRY} && \
    apt-get build-dep -y pulseaudio && \
    cd /tmp && \
    apt-get source pulseaudio && \
    PULSE_DIR=$(find /tmp -maxdepth 1 -type d -name "pulseaudio-*" | head -n 1) && \
    cd $PULSE_DIR && \
    meson setup build \
        -Dman=false \
        -Dtests=false \
        -Ddoxygen=false && \
    cd /tmp && \
    git clone https://github.com/neutrinolabs/pulseaudio-module-xrdp.git && \
    cd pulseaudio-module-xrdp && \
    ./bootstrap && \
    ./configure PULSE_DIR=$PULSE_DIR && \
    make && \
    make install && \
    cd / && \
    rm -rf /tmp/pulseaudio-* /tmp/pulseaudio-module-xrdp && \
    apt-get remove -y build-essential dpkg-dev meson ninja-build autoconf libtool && \
    apt-get autoremove -y && \
    rm -rf /var/lib/apt/lists/* && \
    echo "${LANG} UTF-8" > /etc/locale.gen && \
    locale-gen ${LANG} && \
    update-locale LANG=${LANG} LANGUAGE=${LANGUAGE} && \
    ln -fs /usr/share/zoneinfo/${TZ} /etc/localtime && \
    dpkg-reconfigure -f noninteractive tzdata && \
    printf 'XKBMODEL="pc105"\nXKBLAYOUT="%s"\nXKBVARIANT=""\nXKBOPTIONS=""\nBACKSPACE="guess"\n' "${COUNTRY}" > /etc/default/keyboard && \
    dpkg-reconfigure -f noninteractive keyboard-configuration && \
    rm -rf /var/lib/apt/lists/* /tmp/packages.txt

RUN sed -i '/%users/s/^/# /' /etc/sudoers && \
    echo "sudoers: files sss" >> /etc/nsswitch.conf && \
    echo "session required pam_mkhomedir.so skel=/etc/skel/ umask=0077" >> /etc/pam.d/common-session && \
    mkdir -p /etc/skel/.config/lxqt && \
    cat <<'EOF' > /etc/skel/.config/lxqt/lxqt.conf
[General]
__userfile__=true

[Appearance]
icon_theme=Adwaita
EOF

RUN chmod 440 /etc/sudoers && \
    mkdir -p /etc/sssd && chown root:root /etc/sssd && chmod 755 /etc/sssd && \
    mkdir -p /home && chmod 755 /home && \
    chown xrdp:xrdp /etc/xrdp/key.pem /etc/xrdp/cert.pem /etc/xrdp/rsakeys.ini && \
    chmod 600 /etc/xrdp/key.pem /etc/xrdp/rsakeys.ini && \
    chmod 644 /etc/xrdp/cert.pem && \
    echo "LANG=${LANG}" >> /etc/environment && \
    echo "LANGUAGE=${LANGUAGE}" >> /etc/environment && \
    echo "LC_ALL=${LC_ALL}" >> /etc/environment && \
    echo "#!/bin/sh" > /etc/xrdp/startwm.sh && \
    echo "export LANG=${LANG}" >> /etc/xrdp/startwm.sh && \
    echo "export LANGUAGE=${LANGUAGE}" >> /etc/xrdp/startwm.sh && \
    echo "export LC_ALL=${LC_ALL}" >> /etc/xrdp/startwm.sh && \
    echo "export XDG_SESSION_TYPE=x11" >> /etc/xrdp/startwm.sh && \
    echo "setxkbmap ${COUNTRY}" >> /etc/xrdp/startwm.sh && \
    echo "pulseaudio --start" >> /etc/xrdp/startwm.sh && \
    echo "pactl load-module module-xrdp-sink.so" >> /etc/xrdp/startwm.sh && \
    echo "pactl load-module module-xrdp-source.so" >> /etc/xrdp/startwm.sh && \
    echo "exec dbus-launch --exit-with-session startlxqt" >> /etc/xrdp/startwm.sh && \
    chmod +x /etc/xrdp/startwm.sh

EXPOSE 3389

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
CMD ["/entrypoint.sh"]