FROM multiarch/qemu-user-static:x86_64-aarch64 as qemu
FROM debian:buster
# Copy Qeum binaries
COPY --from=qemu /usr/bin/qemu-aarch64-static /usr/bin

MAINTAINER Majid Geravand "majidgeravand@gfuse.it"

# setup multiarch enviroment
RUN dpkg --add-architecture arm64 \
    && echo "deb-src http://deb.debian.org/debian buster main" >> /etc/apt/sources.list \
    && echo "deb-src http://deb.debian.org/debian buster-updates main" >> /etc/apt/sources.list \
    && echo "deb-src http://security.debian.org buster/updates main" >> /etc/apt/sources.list \
    && DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y sudo locales git tar git-lfs fakeroot devscripts cmake\
    dh-make dh-exec pkg-kde-tools device-tree-compiler:arm64 bc cpio parted dosfstools mtools libssl-dev:arm64 \
    g++-aarch64-linux-gnu dpkg-dev meson debhelper pkgconf \
    && apt-get update && apt-get build-dep -y -a arm64 libdrm xorg-server \
    && rm -rf /var/lib/apt/lists/*

#clone into the binary files and install bloom in chroot
RUN git clone https://github.com/Pilot-Labs-Dev/binary.git \
    && cd binary && tar -zxvf binary.tar.gz -C /opt/ && cd ../ && rm -r -f binary \
    && mkdir -p /opt/binary/home/linaro/ros/src/obstacle_avoidance \
    && cat /etc/resolv.conf > /opt/binary/run/resolvconf/resolv.conf


RUN chroot /opt/binary/ /bin/bash -c "rm /etc/resolv.conf \
    && ln -s /run/resolvconf/resolv.conf /etc/resolv.conf \
    && curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | apt-key add - \
    && source /opt/ros/melodic/setup.bash \
    && DEBIAN_FRONTEND=noninteractive apt-get update && apt-get install -y python-bloom \
    python-rosdep fakeroot python-rospkg dh-make\
    && cd /home/linaro/ros/ "



COPY . /opt/binary/home/linaro/ros/src/obstacle_avoidance/

RUN apt-get update && chroot /opt/binary/ /bin/bash -c "source /opt/ros/melodic/setup.bash \
        && apt-get install -y build-essential cmake && rosdep init && rosdep update \
        && cd /home/linaro/ros/src/obstacle_avoidance/ \
        && echo -e 'DISTRIB_ID=ubuntu\nDISTRIB_RELEASE=18.04\nDISTRIB_CODENAME=bionic\nDISTRIB_DESCRIPTION=\"Ubuntu 18.04.4 LTS\"' > /etc/lsb-release \
        && bloom-generate rosdebian --os-name ubuntu --os-version bionic --ros-distro melodic\
        && fakeroot debian/rules binary \
        && cp ../ros-melodic-tangent-planner_1.0.0-0bionic_arm64.deb /ros-melodic-tangent-planner_1.0.0-0bionic_arm64.deb"

ENTRYPOINT ["/bin/bash", "-c", "cp /opt/binary/ros-melodic-tangent-planner_1.0.0-0bionic_arm64.deb /home/"]
