#!/bin/bash --login
# This script sets up SLM Lab for Linux Ubuntu

# Fail on the first error; killable by SIGINT
set -e
trap "exit" INT

echo "--- Installing system dependencies ---"
apt-get update && \
  apt-get install -y build-essential \
  curl nano git wget zip libstdc++6 \
  python3-dev zlib1g-dev libjpeg-dev cmake swig python-pyglet python3-opengl libboost-all-dev libsdl2-dev libosmesa6-dev patchelf ffmpeg xvfb && \
  rm -rf /var/lib/apt/lists/*

echo "--- Installing Conda ---"
if which conda >/dev/null; then
  echo "Conda is already installed"
else
  curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
  bash Miniconda3-latest-Linux-x86_64.sh -b -p ~/miniconda3
  rm Miniconda3-latest-Linux-x86_64.sh
  echo '. ~/miniconda3/etc/profile.d/conda.sh' >> ~/.bashrc
  source ~/.bashrc
fi

echo "--- Installing Conda environment ---"
if ! which conda >/dev/null; then
  # guard for when no Conda is found, e.g. in Colab
  export PATH=~/miniconda3/bin:$PATH
fi
if conda env list | grep "^lab " >/dev/null; then
  echo "conda env lab is already installed"
else
  conda create -n lab python=3.7.3 -y
fi

# remove for reset:
# conda deactivate
# conda env remove -n lab -y
# conda env export > environment.yml
echo "--- Updating Conda environment ---"
conda env update -f environment.yml

source ~/.bashrc
echo "--- Lab setup complete ---"
