#!/bin/bash

function usage() {
    echo "usage: source mmf_setup [-v]    OR    . mmf_setup [-v]"
    echo
    echo "  -v : show variables that are set"
    echo
    echo "For initial setup on sage.mathcloud.com projects:"
    echo
    echo "   mmf_setup smc"
    echo
}

BIN_DIR="$(dirname $0)"

for i in "$@"
do
    case $i in
        -v)
            echo "mmf_setup environment:"
            echo "$(mmf_setup_bash.py)"
            ;;
        smc)
            DATA_DIR="$(python -c 'import mmf_setup;print(mmf_setup.DATA)')"
            echo "Seting up config files for Sage Mathcloud..."
            shift # move to the next argument
            exec "$BIN_DIR/mmf_initial_setup" \
                 -v --src "$DATA_DIR/config_files/smc" $*
            ;;
        *)
            usage
            exit 1
            ;;
    esac
    shift # move to the next argument
done

# Ensure that this script is sourced, not executed
if [[ -n $BASH_VERSION ]] && [[ "$(basename "$0" 2> /dev/null)" == "mmf_setup" ]];
then
    >&2 echo "Error: mmf_setup must be sourced. Run 'source mmf_setup' or '. mmf_setup' instead of 'mmf_setup'"
    usage
    exit 1
fi

eval "$(mmf_setup_bash.py)"
