# static settings when using spack
function spack_env() {
export SPACK_SETUP_ENV=$SPACK_ROOT/share/spack/setup-env.sh
}

function _install_spack() {
spack_env

if [ ! -f ${SPACK_ROOT} ]; then
  git clone ${SPACK_URL} -b ${SPACK_BRANCH} ${SPACK_ROOT}
fi

. ${SPACK_SETUP_ENV}

if [[ $(spack compiler info ${SPACK_COMPILER}) ]]; then
  echo "Found Compiler $(spack compiler info ${SPACK_COMPILER})"
else
  echo "could not find compiler, will now try to install it... this may take a while"
  spack install ${SPACK_COMPILER}
  spack compiler add $(spack location --install-dir $SPACK_COMPILER)
fi
}

function load_spack() {
export SPACK_ROOT=$1
export SPACK_URL=$2
export SPACK_BRANCH=$3
export SPACK_COMPILER=$4
export SPACK_DISABLE_LOCAL_CONFIG=$5
export SPACK_USER_CACHE_PATH=$6
if [ ! -z "$SPACK_USER_CACHE_PATH" ]; then
  echo "setting SPACK_USER_CACHE_PATH=$SPACK_USER_CACHE_PATH"
fi
spack_env
if [ ! -f ${SPACK_ROOT} ]; then _install_spack; fi
. ${SPACK_SETUP_ENV}
}

