#
# Copyright (C) 2021 Open Source Robotics Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

FROM osrf/lrauv:latest AS base

USER root

# Dependencies from LRAUV README
RUN apt-get -qq update && apt-get -q install -y \
 # LCM dependencies
    build-essential \
    libglib2.0-dev \
    cmake \
    git \
 # LRAUV dependencies
    lib32ncurses-dev \
    lib32z1 \
    libbz2-1.0:i386 \
    libssl-dev \
    libreadline-dev \
    libnetcdf-dev \
    libpcl-dev \
    lua5.1 \
    python2 \
 # More dependencies
    symlinks \
    autoconf \
 && apt-get -qq clean

# LRAUV requires g++-7
RUN apt-get update -qq && apt-get -q install -y gcc-7 g++-7
RUN update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 90 --slave /usr/bin/g++ g++ /usr/bin/g++-9 \
 && update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 70 --slave /usr/bin/g++ g++ /usr/bin/g++-7
RUN update-alternatives --set gcc /usr/bin/gcc-7

# lcm
# Build from source because ./configure from tarball has errors on macro
# AM_DISABLE_STATIC not found
# https://lcm-proj.github.io/build_instructions.html
RUN mkdir /root/build \
 && cd /root/build/ \
 && git clone https://github.com/lcm-proj/lcm.git \
 && cd lcm \
 && mkdir build && cd build && cmake .. && make -j && make install \
 && rm -rf /root/build

# hdf5
RUN mkdir /root/build \
 && cd /root/build/ \
 && wget "https://support.hdfgroup.org/ftp/HDF5/current18/src/hdf5-1.8.21.tar.bz2" \
 && tar xf hdf5-1.8.21.tar.bz2 \
 && cd hdf5-1.8.21 \
 && ./configure --prefix=/usr/local && make -j && make install \
 && rm -rf /root/build

# netcdf
RUN mkdir /root/build \
 && cd /root/build/ \
 && wget "ftp://ftp.unidata.ucar.edu/pub/netcdf/netcdf-4.4.0.tar.gz" \
 && tar xf netcdf-4.4.0.tar.gz \
 && cd netcdf-4.4.0 \
 && ./configure --prefix=/usr/local && make -j && make install \
 && rm -rf /root/build

ENV LD_LIBRARY_PATH ${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}/usr/local/lib
ENV PKG_CONFIG_PATH ${PKG_CONFIG_PATH:+$PKG_CONFIG_PATH:}/usr/local/lib/pkgconfig

USER $USERNAME
ENV MBARI_WS=/home/$USERNAME/mbari_ws
RUN mkdir -p ${MBARI_WS}

WORKDIR ${MBARI_WS}

FROM base AS private-lrauv-gz-sim-base

ENV LRAUV_APP=${MBARI_WS}/src/lrauv-application
RUN mkdir -p ${LRAUV_APP}
COPY --chown=$USERNAME . ${LRAUV_APP}

RUN cd ${LRAUV_APP}/lrauv-lcmtypes && make -j

RUN . /setup.sh && cd ${LRAUV_APP} && \
    make distclean && make -j gazebo && make -j unserialize
RUN mkdir -p ${LRAUV_APP}/Config/sim/root

FROM base AS lrauv-gz-sim

USER root

RUN apt-get -qq update \
  && apt-get -q install -y tmux tmuxinator python3-matplotlib \
  && echo "set -g mouse on" | tee /etc/tmux.conf \
  && rm -rf /var/lib/apt/lists/* \
  && apt-get -qq clean

USER $USERNAME

# There may be a more elegant way to copy these over, but explicit copying will do for now.
ENV LRAUV_APP=${MBARI_WS}/src/lrauv-application
RUN mkdir -p ${LRAUV_APP}

# copy over the compiled LRAUV binaries
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/bin/ ${LRAUV_APP}/bin/
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Modules/*.so ${LRAUV_APP}/Modules/

# copy over selected missions
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Missions/Default.xml ${LRAUV_APP}/Missions/Default.xml
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Missions/Startup.xml ${LRAUV_APP}/Missions/Startup.xml
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Missions/Engineering/circle_test.xml ${LRAUV_APP}/Missions/Engineering/circle_test.xml
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Missions/Science/profile_station.xml ${LRAUV_APP}/Missions/Science/profile_station.xml
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Missions/Science/circle_acoustic_contact.xml ${LRAUV_APP}/Missions/Science/circle_acoustic_contact.xml
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Missions/RegressionTests/GazeboTests ${LRAUV_APP}/Missions/RegressionTests/GazeboTests
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Missions/Insert/StandardEnvelopes.xml ${LRAUV_APP}/Missions/Insert/StandardEnvelopes.xml
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Missions/Insert/NeedComms.xml ${LRAUV_APP}/Missions/Insert/NeedComms.xml
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Missions/Insert/Science.xml ${LRAUV_APP}/Missions/Insert/Science.xml
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Missions/Engineering/HotBunking ${LRAUV_APP}/Missions/Engineering/HotBunking

# copy over SDF worlds for testing
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Source/simulatorModule/gzTests/worlds/*.sdf ${LRAUV_APP}/Source/simulatorModule/gzTests/worlds/

# copy over the basic-level Config variables, not vehicle-specific ones
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Config/regressionTests/gazeboTests ${LRAUV_APP}/Config/regressionTests/gazeboTests
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Config/sim ${LRAUV_APP}/Config/sim
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Config/*cfg ${LRAUV_APP}/Config/

# Create linked vehicle run folders in the image
COPY --from=private-lrauv-gz-sim-base --chown=$USERNAME ${LRAUV_APP}/Tools/gz ${LRAUV_APP}/Tools/gz
RUN ${LRAUV_APP}/Tools/gz/scripts/make_vehicles_folder ${MBARI_WS}/vehicles ${LRAUV_APP}

# Start debugging session by default
CMD tmuxinator start debug_and_plot -n debug_session \
    -p ${LRAUV_APP}/Tools/gz/debug_session.yml
