#!/usr/bin/env sh

# bail if python integration isn't enabled here
if [ "${_RS_TERMINAL_PYTHON_INTEGRATION_ENABLED_}" != "TRUE" ]; then
	unset _RS_TERMINAL_PYTHON_INTEGRATION_ENABLED_
	return 0
fi

unset _RS_TERMINAL_PYTHON_INTEGRATION_ENABLED_

# if we have a conda prefix, ensure it's still used
if [ -n "${_RS_CONDA_PREFIX}" ]; then

	# if we're running on WSL, we need to convert the path
	# here to a Linux-style path
	if command -v wslpath >/dev/null 2>&1; then
		_RS_CONDA_PREFIX=$(wslpath "${_RS_CONDA_PREFIX}")
	fi

	# ideally, we'd call conda activate here,
	# but users (especially on Windows) might not
	# have called 'conda init' with their shell,
	# so we fake the process here
	CONDA_PREFIX="${_RS_CONDA_PREFIX}"
	export CONDA_PREFIX

	# on Windows, we need to put a number of locations
	# on the PATH for conda utilities to function
	#
	# in particular, conda's crypto libraries need to
	# be on the PATH for 'conda install' to function
	case "$(uname -s)" in
	MINGW32*)
		
		if command -v cygpath >/dev/null 2>&1; then
			_CONDA_PREFIX=$(cygpath "${CONDA_PREFIX}")
		fi
		
		_CONDA_SUFFIXES=(
			bin
			Scripts
			Library/bin
			Library/usr/bin
			Library/mingw-w64/bin
		)
		
		for _CONDA_SUFFIX in ${_CONDA_SUFFIXES[@]}; do
			PATH="${_CONDA_PREFIX}/${_CONDA_SUFFIX}:${PATH}"
		done

		unset _CONDA_PREFIX
		unset _CONDA_SUFFIX
		unset _CONDA_SUFFIXES
	;;
	esac

	# unset CONDA_PROMPT_MODIFIER for project-local envs
	# (look for slash in environment variable value)
	case "${PS1}" in
	"${CONDA_PROMPT_MODIFIER}"*)
		PS1="${PS1:${#CONDA_PROMPT_MODIFIER}}"
		unset CONDA_PROMPT_MODIFIER
	;;
	esac

	unset _RS_CONDA_PREFIX

fi

if [ -f "${RETICULATE_PYTHON}" ] || [ -f "${RETICULATE_PYTHON_FALLBACK}" ]; then

   if [ -f "${RETICULATE_PYTHON}" ]; then
      _RS_PYTHON_BIN=$(dirname "${RETICULATE_PYTHON}")
   else
      _RS_PYTHON_BIN=$(dirname "${RETICULATE_PYTHON_FALLBACK}")
   fi

	# munge path for MINGW32 if necessary
	case "$(uname -s)" in
	MINGW32*)
		if command -v cygpath >/dev/null 2>&1; then
			_RS_PYTHON_BIN=$(cygpath "${_RS_PYTHON_BIN}")
		fi
	;;
	esac

	# if a Scripts sub-directory exists, place that on
	# the PATH as well (primarily for conda on Windows)
	if [ -d "${_RS_PYTHON_BIN}/Scripts" ]; then
		PATH="${_RS_PYTHON_BIN}/Scripts:${PATH}"
	fi

	# check for an activate script in the same directory
	# as the configured version of Python; if it exists,
	# use that to activate Python (mainly for venv)
	#
	# note that this might also discover a conda activate
	# script; unfortunately, running that isn't sufficient
	# to update the PATH so we make that check below as well
	if [ -f "${_RS_PYTHON_BIN}/activate" ]; then
		. "${_RS_PYTHON_BIN}/activate"
	fi

	case "${_RS_PYTHON_BIN}" in

	# if Python is located within a 'system' directory, copy the commonly-used
	# tools to a separate temporary directory, and place that on the PATH
	/usr/bin|/usr/local/bin|/opt/bin|/opt/local/bin|/opt/homebrew/bin)

		if [ -d "${R_SESSION_TMPDIR}" ]; then

			# create our custom terminal PATH location
			_RS_TERMINAL_PATH="${R_SESSION_TMPDIR}/rstudio/terminal"
			mkdir -p "${_RS_TERMINAL_PATH}"
			PATH="${_RS_TERMINAL_PATH}:${PATH}"

			# put common utilities on the PATH
			_RS_OWD="$(pwd)"
			cd "${_RS_PYTHON_BIN}"
			for _RS_PYTHON_TOOL in python python3 ipython ipython3 pip pip3 jupyter jupyter-*; do
				if [ -e "${_RS_PYTHON_TOOL}" ]; then
					ln -nfs "${_RS_PYTHON_BIN}/${_RS_PYTHON_TOOL}" "${_RS_TERMINAL_PATH}/${_RS_PYTHON_TOOL}"
				fi
			done

			# if only version-suffixed versions of tools are available, then
			# also create a symlink so that the user can still invoke python
			# with a plain 'python' call. note that we also create an alias
			# as it looks like python can be sensitive to how it was invoked;
			# e.g. it expects the binary to have a specific name.
			#
			# perhaps sort of like how a shell will run in a Bourne-shell
			# compatibility mode if it's placed at /bin/sh, I guess?
			cd "${_RS_TERMINAL_PATH}"
			for _RS_PYTHON_TOOL in ipython python pip; do
				if ! [ -e "${_RS_PYTHON_TOOL}" ]; then
					if [ -e "${_RS_PYTHON_TOOL}3" ]; then
						eval "alias ${_RS_PYTHON_TOOL}=${_RS_PYTHON_TOOL}3"
						ln -nfs "${_RS_PYTHON_TOOL}3" "${_RS_PYTHON_TOOL}"
					fi
				fi
			done

			# clean up when done
			cd "${_RS_OWD}"
			unset _RS_OWD
			unset _RS_PYTHON_TOOL
			unset _RS_TERMINAL_PATH

		fi

	;;

	# otherwise, use the directory as-is
	*)
		PATH="${_RS_PYTHON_BIN}:${PATH}"
	;;

	esac

	unset _RS_PYTHON_BIN

fi

