#!/bin/sh -e

##########################################################################
#   Script description:
#       Install miniconda on a FreeBSD system
#       
#   History:
#   Date        Name        Modification
#   2018        Jason Bacon Begin
##########################################################################

usage()
{
    printf "Usage: $0\n"
    exit 1
}


##########################################################################
#   Main
##########################################################################

if [ $(whoami) = root ]; then
    cat << EOM

miniconda-installer should not be run as root.  Conda is designed to allow
non-root users to install software packages for their own use.

Many of the community channels contain packages with little or no quality
control or security checks.

EOM
    exit 1
fi

if [ $# != 0 ]; then
    usage
fi

if [ -e /usr/local/sbin/node-type ] && [ 0$(/usr/local/sbin/node-type) != 0compute ]; then
    printf "Error: $0 should only be run on compute nodes.\n"
    exit 1
fi

for dir in proc dev sys; do
    if [ ! -e /compat/linux/$dir ]; then
	cat << EOM

Missing /compat/linux/$dir.  Run "pkg install auto-admin" and
"auto-install-linux_base" as root to complete the Linux compatibility setup.

EOM
	exit 1
    fi
done
if [ ! -e /usr/local/bin/python3.8 ]; then
    cat << EOM

This miniconda requires python3.8.  Install lang/python38 and try again.

EOM
    exit 1
fi

if [ ! -e ~/miniconda3 ]; then
    script=Miniconda3-latest-Linux-x86_64.sh
    if [ ! -e $script ]; then
	fetch https://repo.anaconda.com/miniconda/$script
    fi
    /compat/linux/bin/sh Miniconda3-latest-Linux-x86_64.sh
    read -p "Remove $script? [y]/n " remove
    if [ 0"$remove" != 0n ]; then
	rm -f $script
    fi

    cat << EOM

To use the conda environment, run "conda-shell".

EOM
    while [ 0"$resp" != 0"got it" ]; do
	read -p 'Type "got it" to confirm. ' resp
    done
else
    printf "miniconda3 already present.  Not reinstalling.\n"
fi
