#!/usr/bin/env bash
#
# Returns Magento document root if sourced or prints if launched as standalone
#

mageRoot="$(dirname $(realpath "${0}"))"

while [[ ! -e ${mageRoot}/app/Mage.php && ! -e ${mageRoot}/bin/magento ]]
do
    mageRoot="$(dirname $(realpath ${mageRoot}))"
done

return "${mageRoot}" 2>/dev/null || printf '%s' "${mageRoot}" && exit