export DIRACOS=/cvmfs/dirac.egi.eu/dirac/v8.0.57/Linux-x86_64
export MAMBA_ROOT_PREFIX=$DIRACOS

# Some sites set variables which are actively harmful
unset GFAL_CONFIG_DIR GFAL_PLUGIN_DIR GLEXEC_LOCATION GLITE_LOCATION_VAR
unset GLITE_LOCATION GLOBUS_LOCATION JAVA_HOME LCG_LOCATION
unset LD_LIBRARY_PATH LESSOPEN MANPATH PERL5LIB PYTHONPATH SRM_PATH

# Initialise the conda environment in a way which ignores other conda installations
unset CONDA_SHLVL
eval "$(PS1="${PS1:-}" ${DIRACOS}/bin/micromamba shell hook -s bash)"
micromamba activate "$DIRACOS"

# Silence python warnings
export PYTHONWARNINGS=ignore

# Davix options (will be default in the future)
export DAVIX_USE_LIBCURL=1
# Disable caching redirection to avoid asking storage nodes instead of head nodes
# in setups like EOS
export DAVIX_DISABLE_REDIRECT_CACHING=1

# XROOTD options to avoid internal retry leading to lost files
# https://its.cern.ch/jira/browse/EOS-6195
export XRD_WRITERECOVERY=0

# Set up the X509 variables

# Function check if folder exist and contains files
function checkDir () {
  if [ -z "${1}" ]; then
    return 1
  fi
  resolvedDir=$(realpath -q "${1}")
  if [ -z "${resolvedDir}" ]; then
    return 1
  fi
  if [ -n "$(ls -A "${resolvedDir}" 2>/dev/null)" ]; then
    return 0
  fi
  return 1
}

# Add sanity check for X509_CERT_DIR variable
if ! checkDir "${X509_CERT_DIR:-}" ; then
  export X509_CERT_DIR="/etc/grid-security/certificates"
  if ! checkDir "$X509_CERT_DIR" ; then
    export X509_CERT_DIR='/cvmfs/dirac.egi.eu/dirac/v8.0.57/Linux-x86_64/etc/grid-security/certificates'
  fi
fi

# Add sanity check for X509_VOMS_DIR variable
if ! checkDir "${X509_VOMS_DIR:-}" ; then
  export X509_VOMS_DIR='/cvmfs/dirac.egi.eu/dirac/v8.0.57/Linux-x86_64/etc/grid-security/vomsdir'
fi

# Add sanity check for X509_VOMSES variable
if ! checkDir "${X509_VOMSES:-}" ; then
  export X509_VOMSES='/cvmfs/dirac.egi.eu/dirac/v8.0.57/Linux-x86_64/etc/grid-security/vomses'
fi