# -*- mode: shell-script -*-

function _pbench_pathins() {
    if [[ -d "${1}" ]] && [[ ":${PATH:-}:" != *":${1}:"* ]]; then
        PATH="${1}${PATH:+:${PATH}}"
    fi
}

_pbench_pathins "/usr/sbin"
_pbench_pathins "/usr/bin"

function _pbench_setup() {
    local prefix=/opt/pbench-agent

    if [[ ! -f ${prefix}/profile ]]; then
	if [[ -f ${PWD}/profile ]]; then
	    prefix=${PWD}
	else
	    # last ditch attempt
	    prefix=${TOP}
	fi
    fi

    if [[ ! -f ${prefix}/util-scripts/pbench-register-tool-set ]]; then
	echo "*** WARNING *** Something wrong in the pbench base profile:" >&2
	echo "                Setup has failed - could not determine the proper environment prefix: \"${prefix}\"" >&2
	echo "                You will need to fix it before working with pbench." >&2
    else
	local pdir
	local NL=$'\n'
	while read -r pdir; do
	    if [[ ":${PYTHONPATH:-}:" != *":${pdir}:"* ]]; then
		PYTHONPATH=${pdir}${PYTHONPATH:+:${PYTHONPATH}}
	    fi
	done <<< "${prefix}/lib${NL}$(ls -1d ${prefix}/lib*/python3.*/site-packages)"
	export PYTHONPATH
	export _PBENCH_AGENT_CONFIG=${prefix}/config/pbench-agent.cfg

	_pbench_pathins "${prefix}/bench-scripts"
	_pbench_pathins "${prefix}/util-scripts"
    fi
}

_pbench_setup

unset -f _pbench_pathins
unset -f _pbench_setup
