#!/bin/bash

set -x

touch LOG_FILE

# We start by adding extra apt packages, since pip modules may required library
if [[ "$EXTRA_APT_PACKAGES" ]]; then
    echo "EXTRA_APT_PACKAGES environment variable found.  Installing." >> LOG_FILE
    apt update -y
    apt install -y $EXTRA_APT_PACKAGES
fi

if [[ -e "/opt/app/environment.yml" ]]; then
    echo "environment.yml found. Installing packages" >> LOG_FILE
    /opt/conda/bin/conda env update -f /opt/app/environment.yml
else
    echo "no environment.yml" >> LOG_FILE
fi

if [[ "$EXTRA_CONDA_PACKAGES" ]]; then
    echo "EXTRA_CONDA_PACKAGES environment variable found.  Installing $EXTRA_CONDA_PACKAGES" >> LOG_FILE
    /opt/conda/bin/conda install -y $EXTRA_CONDA_PACKAGES
else
    echo "no conda packages" >> LOG_FILE
fi

if [[ "$EXTRA_PIP_PACKAGES" ]]; then
    echo "EXTRA_PIP_PACKAGES environment variable found.  Installing $EXTRA_PIP_PACKAGES" >> LOG_FILE
    /opt/conda/bin/pip install $EXTRA_PIP_PACKAGES
else
    echo "no pip packages" >> LOG_FILE
fi

# Run extra commands
exec "$@"
