Update db-use.eclass to upstream

BUG=chromium:1001245
TEST=preCQ

Change-Id: Ie55217770ffe7c02f349e91f8898346228f399a4
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/overlays/portage-stable/+/1832452
Tested-by: Chris McDonald <cjmcdonald@chromium.org>
Reviewed-by: Mike Frysinger <vapier@chromium.org>
Commit-Queue: Chris McDonald <cjmcdonald@chromium.org>
diff --git a/eclass/db-use.eclass b/eclass/db-use.eclass
index 8d81602..ed76073 100644
--- a/eclass/db-use.eclass
+++ b/eclass/db-use.eclass
@@ -1,11 +1,14 @@
-# Copyright 1999-2004 Gentoo Foundation
+# Copyright 1999-2018 Gentoo Foundation
 # Distributed under the terms of the GNU General Public License v2
-# $Header: /var/cvsroot/gentoo-x86/eclass/db-use.eclass,v 1.8 2009/11/24 05:24:20 robbat2 Exp $
 # This is a common location for functions that aid the use of sys-libs/db
 #
-# Bugs: pauldv@gentoo.org
+# Bugs: maintainer-needed@gentoo.org
 
-inherit versionator multilib
+# multilib is used for get_libname in all EAPI
+case "${EAPI:-0}" in
+	0|1|2|3|4|5|6) inherit eapi7-ver multilib ;;
+	*) inherit multilib ;;
+esac
 
 #Convert a version to a db slot
 db_ver_to_slot() {
@@ -18,11 +21,16 @@
 		done
 		return 1
 	fi
-	echo -n "${1/.0/}"
+	# 5.0.x uses 5.0 as slot value, so this replacement will break it;
+	# older sys-libs/db might have been using this but it's no longer
+	# the case, so make it work for latest rather than older stuff.
+	# echo -n "${1/.0/}"
+	echo -n "$1"
 }
 
 #Find the version that correspond to the given atom
 db_findver() {
+	has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
 	if [ $# -ne 1 ]; then
 		eerror "Function db_findver needs one argument" >&2
 		eerror "args given:" >&2
@@ -34,8 +42,8 @@
 	fi
 
 	PKG="$(best_version $1)"
-	VER="$(get_version_component_range 1-2 "${PKG/*db-/}")"
-	if [ -d /usr/include/db$(db_ver_to_slot "$VER") ]; then
+	VER="$(ver_cut 1-2 "${PKG/*db-/}")"
+	if [ -d "${EPREFIX}"/usr/include/db$(db_ver_to_slot "$VER") ]; then
 		#einfo "Found db version ${VER}" >&2
 		echo -n "$VER"
 		return 0
@@ -50,12 +58,13 @@
 # to test for, it will aim to find the library corresponding to it.
 
 db_includedir() {
+	has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
 	if [ $# -eq 0 ]; then
 		VER="$(db_findver sys-libs/db)" || return 1
 		VER="$(db_ver_to_slot "$VER")"
 		echo "include version ${VER}" >&2
-		if [ -d "/usr/include/db${VER}" ]; then
-			echo -n "/usr/include/db${VER}"
+		if [ -d "${EPREFIX}/usr/include/db${VER}" ]; then
+			echo -n "${EPREFIX}/usr/include/db${VER}"
 			return 0
 		else
 			eerror "sys-libs/db package requested, but headers not found" >&2
@@ -66,8 +75,8 @@
 		for x in $@
 		do
 			if VER=$(db_findver "=sys-libs/db-${x}*") &&
-			   [ -d "/usr/include/db$(db_ver_to_slot $VER)" ]; then
-				echo -n "/usr/include/db$(db_ver_to_slot $VER)"
+				[ -d "${EPREFIX}/usr/include/db$(db_ver_to_slot $VER)" ]; then
+				echo -n "${EPREFIX}/usr/include/db$(db_ver_to_slot $VER)"
 				return 0
 			fi
 		done
@@ -83,9 +92,10 @@
 # packages to test for, it will aim to find the library corresponding to it.
 
 db_libname() {
+	has "${EAPI:-0}" 0 1 2 && ! use prefix && EPREFIX=
 	if [ $# -eq 0 ]; then
 		VER="$(db_findver sys-libs/db)" || return 1
-		if [ -e "/usr/$(get_libdir)/libdb-${VER}.so" ]; then
+		if [ -e "${EPREFIX}/usr/$(get_libdir)/libdb-${VER}$(get_libname)" ]; then
 			echo -n "db-${VER}"
 			return 0
 		else
@@ -97,7 +107,7 @@
 		for x in $@
 		do
 			if VER=$(db_findver "=sys-libs/db-${x}*"); then
-				if [ -e "/usr/$(get_libdir)/libdb-${VER}.so" ]; then
+				if [ -e "${EPREFIX}/usr/$(get_libdir)/libdb-${VER}$(get_libname)" ]; then
 					echo -n "db-${VER}"
 					return 0
 				fi