#!/usr/bin/env bash

if [[ -r "@GENTOO_PORTAGE_EPREFIX@"/lib/gentoo/functions.sh ]]; then
	. "@GENTOO_PORTAGE_EPREFIX@"/lib/gentoo/functions.sh
elif [[ -r "@GENTOO_PORTAGE_EPREFIX@"/etc/init.d/functions.sh ]]; then
	. "@GENTOO_PORTAGE_EPREFIX@"/etc/init.d/functions.sh
elif [[ -r "@GENTOO_PORTAGE_EPREFIX@"/sbin/functions.sh ]]; then
	. "@GENTOO_PORTAGE_EPREFIX@"/sbin/functions.sh
fi

einfo "entering chained prefix @GENTOO_PORTAGE_EPREFIX@ ..."

# inform profiles very early of us beeing interactive (or at least,
# we will _start_ an interactive shell, so we want the according bits.)
set -i

. "@GENTOO_PORTAGE_EPREFIX@"/etc/profile

# if there is no local portage, foreign portage instances need to know 
# where to merge to - and thus we need to set EPREFIX.
# the other way round, if there is a local portage, EPREFIX may not
# point anywhere else than to our local EPREFIX or be unset...

# DISABLED: this is done by etc/profile now, to be able to start a
#           prefix environment by sourcing etc/profile.

#if [[ -x "@GENTOO_PORTAGE_EPREFIX@"/usr/bin/emerge ]]; then
#	if [[ -n "${EPREFIX}" ]] && [[ "${EPREFIX}" != "@GENTOO_PORTAGE_EPREFIX@" ]]; then
#		ewarn "EPREFIX is set. this will render portage unusable, unsetting"
#		unset EPREFIX
#	fi
#else
#	export EPREFIX="@GENTOO_PORTAGE_EPREFIX@"
#fi

# a common problem: the child has no less installed, but baselayout insists
# on setting PAGER to EPREFIX/usr/bin/less. everything gets back to normal
# when unsetting PAGER in this case.
if [[ ! -x "${PAGER}" ]]; then
	unset PAGER
fi

bash --noprofile
einfo "leaving chained prefix @GENTOO_PORTAGE_EPREFIX@ ..."

