#!/usr/bin/env bash
#

set -eu -o pipefail

spack_ver="${SPACK_VER:-0.22.3}"

cd /opt
if [[ -d spack && ! -f spack_${spack_ver} ]]
then
    rm -rf spack /usr/bin/spack $HOME/.spack/
fi
if [ ! -d spack ]
then
  # download
  curl -sOL https://github.com/spack/spack/archive/refs/tags/v${spack_ver}.tar.gz
  tar -xf v${spack_ver}.tar.gz && rm v${spack_ver}.tar.gz
  mv spack-${spack_ver} spack
  touch spack_${spack_ver}
fi

# install
ln -s /opt/spack/bin/spack /usr/bin/spack

# add binary mirror
#spack mirror add E4S https://cache.e4s.io
#spack buildcache keys -it

# find compilers & external packages
#spack compiler find
#spack external find

# accessible by regular CI user
chmod a+rwx -R /opt/spack
#chmod a+rwx -R $HOME/.spack/
