#!/bin/bash
set -e

FILENAME=Miniconda3-4.5.12-Linux-x86_64.sh
URL=https://repo.continuum.io/miniconda/$FILENAME
EXPECTED_MD5SUM=866ae9dff53ad0874e1d1a60b1ad1ef8

if [ $# -lt 1 ]; then
    echo "Usage: $0 /path/to/install/env/"
    echo ""
    echo "Installs Miniconda Python to the given directory"
    exit
fi
INSTALL_PATH=$1

wget -nc $URL
ACTUAL_MD5SUM=$(md5sum $FILENAME | awk '{print $1}')

if [ $ACTUAL_MD5SUM != $EXPECTED_MD5SUM ]; then
    echo "Failed to download the expected Miniconda install script from: $URL"
    echo "Check download and try again"
    rm -f $FILENAME
    exit
fi

chmod +x ./$FILENAME
mkdir -p $INSTALL_PATH

echo "Beginning Miniconda install to path $INSTALL_PATH"
./$FILENAME -u -b -p $INSTALL_PATH

# NOTE: The liberal Miniconda team jumped to Python 3.7 while the conservative
#   Tensorflow team was still on 3.5; pick 3.6 for a wide range of compatibility
echo "Editing Python version..."
yes | $INSTALL_PATH/bin/conda install python=3.6
echo "Python downgraded to 3.6"

echo "Miniconda install complete"
