# Multi-arch base (Ubuntu 20.04)
FROM ubuntu:20.04

# Make tzdata noninteractive for Docker builds
ENV DEBIAN_FRONTEND=noninteractive

# ---- Time zone (fix echo to expand $TZ) ----
ENV TZ=America/Detroit
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime \
 && echo "$TZ" > /etc/timezone

# ---- Select proper Ubuntu mirror per arch ----
# TARGETARCH is set automatically by docker buildx (e.g., amd64 or arm64)
ARG TARGETARCH

# Rebuild /etc/apt/sources.list with the right base URL and optional [arch=...] tags
# - amd64  -> archive.ubuntu.com
# - arm64  -> ports.ubuntu.com/ubuntu-ports
RUN set -eux; \
    if [ "$TARGETARCH" = "arm64" ]; then \
      BASE_URL="http://ports.ubuntu.com/ubuntu-ports"; ARCH_TAG="[arch=arm64]"; \
    else \
      BASE_URL="http://archive.ubuntu.com/ubuntu"; ARCH_TAG="[arch=amd64]"; \
    fi; \
    cat > /etc/apt/sources.list <<EOF
deb ${ARCH_TAG} ${BASE_URL} focal main restricted universe multiverse
deb ${ARCH_TAG} ${BASE_URL} focal-updates main restricted universe multiverse
deb ${ARCH_TAG} ${BASE_URL} focal-backports main restricted universe multiverse
deb ${ARCH_TAG} ${BASE_URL} focal-security main restricted universe multiverse
EOF

RUN apt-get update

# unminimize ubuntu
RUN yes | unminimize

# config CN environment
RUN apt install language-pack-zh-hans -y

RUN echo LANG="zh_CN.UTF-8" >> /etc/environment
RUN echo LANGUAGE="zh_CN:zh:en_US:en" >> /etc/environment

RUN echo LANG="zh_CN.UTF-8" >> /etc/profile
RUN echo LANGUAGE="zh_CN:zh:en_US:en" >> /etc/profile

RUN echo LANG="zh_CN.UTF-8" >> ~/.bashrc
RUN echo LANGUAGE="zh_CN:zh:en_US:en" >> ~/.bashrc

RUN locale-gen
RUN /bin/bash -c "source ~/.bashrc"

# install xfce4
ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get install -y xfce4 xfce4-terminal

RUN apt install dbus-x11 -y
RUN apt install fonts-wqy-microhei -y
RUN apt install -y \
    gnome-user-docs-zh-hans \
    language-pack-gnome-zh-hans \
    fcitx \
    fcitx-pinyin \
    fcitx-table-wubi \
    vim \
    less \
    ca-certificates

# install ROS
RUN sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
RUN apt-get install curl -y
RUN apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
RUN apt-get update
RUN apt install ros-noetic-desktop-full -y
RUN echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
RUN /bin/bash -c "source ~/.bashrc"

# install SAD dependence
RUN apt-get install -y \
    ros-noetic-pcl-ros \
    ros-noetic-velodyne-msgs \
    libopencv-dev \
    libgoogle-glog-dev \
    libeigen3-dev \
    libsuitesparse-dev \
    libpcl-dev\
    libyaml-cpp-dev \
    libbtbb-dev \
    libgmock-dev \
    pcl-tools \
    libspdlog-dev \
    libqglviewer-dev-qt5 \
    git

# Add build + Pangolin deps (incl. epoxy)
RUN apt-get update && \
    apt-get install -y software-properties-common && \
    add-apt-repository -y ppa:ubuntu-toolchain-r/test && \
    apt-get update && \
    DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        gcc-11 g++-11 \
        build-essential cmake git pkg-config \
        libeigen3-dev \
        libjpeg-turbo8-dev libpng-dev libtiff-dev libopenexr-dev \
        liblz4-dev libzstd-dev \
        libgl1-mesa-dev libegl1-mesa-dev libgles2-mesa-dev \
        libxkbcommon-dev libwayland-dev wayland-protocols \
        libepoxy-dev


WORKDIR /root/software

# Clone Pangolin repository and checkout v0.9.4
# Hang add git checkout v0.9.4
#RUN git clone https://github.com/stevenlovegrove/Pangolin.git && \
#    cd Pangolin && \
#    git checkout v0.9.4 && \
#    mkdir build && \
#    cd build && \
#    cmake .. && \
#    make -j8 && \
#    make install && \
#    ldconfig

# Build with GCC 11, and still sidestep EXR
RUN git clone https://github.com/stevenlovegrove/Pangolin.git && \
    cd Pangolin && \
    git checkout v0.9.4 && \
    mkdir -p build && cd build && \
    cmake -DCMAKE_BUILD_TYPE=Release \
          -DCMAKE_C_COMPILER=gcc-11 -DCMAKE_CXX_COMPILER=g++-11 \
          -DCMAKE_DISABLE_FIND_PACKAGE_OpenEXR=ON \
          -DBUILD_EXAMPLES=OFF -DBUILD_TOOLS=OFF \
          .. && \
    make -j2 && \
    make install && \
    ldconfig

# set up vnc
RUN apt-get install tigervnc-standalone-server x11vnc -y
WORKDIR /root/.vnc
COPY ./docker/xstartup ./
RUN chmod u+x ~/.vnc/xstartup

# set up noVNC
WORKDIR /usr/lib
RUN git clone https://github.com/novnc/noVNC.git -o noVNC
WORKDIR /usr/lib/noVNC/utils
RUN git clone https://github.com/novnc/websockify.git -o websockify

WORKDIR /
COPY ./docker/startup.sh ./
RUN chmod u+x startup.sh
ENTRYPOINT ["./startup.sh"]
# ENTRYPOINT ["tail","-f","/dev/null"]






