#!/bin/sh
set -e
alias log="echo '[setup.sh]'"


dist="${1:-"./dist"}"
dist="$(realpath "$dist")"


rm -fr "$dist"
mkdir -p "$dist/lustre-v4" "$dist/lustre-v6"


log "downloading"
    wget -qO "$dist/lustre-v4.tgz" https://www-verimag.imag.fr/DIST-TOOLS/SYNCHRONE/lustre-v4/distrib/archives/lustre-v4-III-e-linux64.tgz &
    wget -qO "$dist/lustre-v6.tgz" https://www-verimag.imag.fr/DIST-TOOLS/SYNCHRONE/lustre-v6/pre-compiled/x86_64-Linux-lv6-bin-dist.tgz   &
    wait

log "extracting"
    tar -xvf "$dist/lustre-v4.tgz" --directory="$dist/lustre-v4" > /dev/null &
    tar -xvf "$dist/lustre-v6.tgz" --directory="$dist/lustre-v6" > /dev/null &
    wait
    rm "$dist/lustre-v4.tgz" "$dist/lustre-v6.tgz"


log "installing env"
    echo '#!/bin/env -S echo "should be sourced :"
    
    dir="'"$dist"'"
    
    export LUSTRE_INSTALL="$dir/lustre-v4/lustre-v4-III-e0-linux64"
    export PATH=$PATH:$LUSTRE_INSTALL/bin
    export PATH=$PATH:~/Lustre/Lustre-v6/bin
    ' > "$dist/env.sh"
    
log "installed at '$dist'"
