DEST=${1:-/etc/skel}

# Install our public GPG key to trusted store
curl https://repo.anaconda.com/pkgs/misc/gpgkeys/anaconda.asc | gpg --dearmor > conda.gpg
install -o root -g root -m 644 conda.gpg /usr/share/keyrings/conda-archive-keyring.gpg

# Check whether fingerprint is correct (will output an error message otherwise)
gpg --keyring /usr/share/keyrings/conda-archive-keyring.gpg --no-default-keyring --fingerprint 34161F5BF5EB1D4BFBBB8F0A8AEB4F8B29D82806

# Add our Debian repo

echo "deb [arch=amd64 signed-by=/usr/share/keyrings/conda-archive-keyring.gpg] https://repo.anaconda.com/pkgs/misc/debrepo/conda stable main" | sudo tee /etc/apt/sources.list-available/conda.list

ln -s /etc/apt/sources.list-available/conda.list /etc/apt/sources.list.d/conda.list


# Install it!
apt update
apt install -y conda

cat <<-EOF | sudo tee "${DEST}"/.zshrc.pre-plugins.d/008_conda.zsh >/dev/null
	# when using conda from the vim plugin it expects to be called from base conda environment.
	## >>> conda initialize >>>
	## !! Contents within this block are managed by 'conda init' !!
	__conda_setup="$('/opt/conda/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
	if [ $? -eq 0 ]; then
	    eval "$__conda_setup"
	else
	    if [ -f "/opt/conda/etc/profile.d/conda.sh" ]; then
		. "/opt/conda/etc/profile.d/conda.sh"
	    else
		export PATH="/opt/conda/bin:$PATH"
	    fi
	fi
	unset __conda_setup
	## <<< conda initialize <<<

	# switch to the default conda environment when leaving a condaenv
	AUTOSWITCH_DEFAULT_CONDAENV="base"
	# AUTOSWITCH_SILENT
EOF

# We add autoswitching for conda by adding the following to .zgen-local-plugins:
# zgenom load bckim92/zsh-autoswitch-conda
# see 020_zsh_quickstart.sh

# see: https://github.com/ubaldot/vim-conda-activate for vim support


# Check to see if the installation is successful:
. /opt/conda/etc/profile.d/conda.sh
conda -V
